Design Patterns Flashcards

1
Q

What are design patterns?

A

Design patterns are patterns intended each to provide a general reusable solution to one type of problem during software development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which pattern may we use if we want a way to delegate the creation of an certain type of object to different classes depending on our needs?

A

Factory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How may a factory pattern be realised?

A

We create a main class, which represents the base for the Factory. This main class may have an abstract method, which we may fill out by creating subclasses that inherit from it, implementing that method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

We want to represent the ability for a user to order pizza from different pizza shops. What pattern may we use?

A

Factory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What design may we use when we want to create a family of related objects without revealing the concrete classes they come from?

A

Abstract Factory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a Factory and an Abstract Factory?

A

A Factory pattern uses inheritance to give you a complete object in one shot, while an Abstract Factory uses composition and returns a family of related classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What pattern may you use if you want to generate a complex object in steps?

A

Builder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

We want to create a complex Pizza object ingredient-per-ingredient. What pattern may we use?

A

Builder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why may we use a Builder pattern?

A

The Builder pattern encapsulates the way we construct a complex object, and allows the object to be made in a multistep and varying process.

If we wanted a pizza without dough, for example, we just don’t call the dough method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What pattern may you use if you want there to only be a single instance of an object?

A

Singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

We want a object to be created that is singular, and manages a set of smaller objects. What pattern may we use?

A

Singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why may we use a Singleton pattern?

A

We may want this, for example, for a file system or window manager, to store state based information on the program in one location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between eager and lazy initialisation?

HINT: Singletons

A

Eager initialisation creates the instance when the Singleton class loaded via the JVM.

Lazy initialisation only creates the instance the first time getInstance() is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How may we create a Singleton pattern object?

A

We create a class that contains a private static Singleton instance variable, which may or may not be instantiated at class loading depending on your preferred initialisation method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How may we create a Builder pattern object?

A

We create an interface, which contains all the steps necessary. Then, we create a Builder object that implements that interface, getting the relevant parts for the object we are trying to generate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What pattern may you use if you want to dynamically add functionality to an object over its runtime?

A

Decorator

17
Q

We want to design a coffee shop that creates Coffee objects with different toppings. What pattern may we use?

A

Decorator

18
Q

How may we create a Decorator pattern object?

A

We create an interface, which contains all relevant methods needed for the object. Then, we can create any number of objects that implement that Interface, which contain a private Beverage instance variable, storing the object it is wrapping.

19
Q

How does a Decorator pattern add functionality?

A

We “wrap” the object in other objects, and each object calls its own method and the object below it’s method, almost like a recursive call.

20
Q

What pattern may you use if you want to securely allow an object to communicate with another?

A

Command

21
Q

We want to design a Person class that uses a remote control to tell a GarageDoor to close. What pattern allows us to do this most effectively?

A

Command

22
Q

How may we implement a Command pattern object?

A

We create a Command interface, which contains an execute() and an optional undo() method. We may then have any class invoke a ConcreteCommand object that implements the Command interface, which calls the method on the object for you, encapsulated.