Design Patterns Flashcards

1
Q

What is the purpose of Design Patterns?

A

Create an architecture that enables the re-use of code blocks to create customised objects.

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

What are the 5 design pattern principles?

A

Encapsulation of dynamic code; Program to an interface or abstract class; Favour object composition over class inheritance; Objects should be open for extension, closed for modification; Depend on abstractions, not on concrete classes.

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

What are the 3 design pattern categories?

A

Factorial (Object Creation)
Structural (Object composition)
Behavioural (Object interaction)

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

CDP: When should we use the singleton pattern?

A

When we only need one instance of the class.

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

CDP: How do we create a Singleton Object?

A

Create a static reference to the class; Create a private constructor to instantiate the reference; Create a static public method (getInstance) to access the object.

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

CDP: What are the 2 types of initialisation of a Singleton Object?

A

Eager Instantiation : Create object when class is loaded.
Lazy Instantiation : Create object only when you need.

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

CDP: Why might multithreaded programs cause issues with Singleton instantiation? How can we resolve this?

A

Because we need to use thread management to ensure only 1 thread instantiates the singleton. We can try to resolve this through synchronizing the method. However, this means threads will wait idly outside the locked object.

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

CDP: What is the most effective way of implementing thread management in the Singleton Class?

A

Create a condition in the instantiation method which only instantiates the reference if the object is currently null.

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

CDP: Define the Builder Pattern…

A

Separates the construction process from the objects representation. This enables isolation and encapsulation of production, meaning many representations can be created.

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

CDP: What are the 3 qualities that BDP enables?

A
  • Separate construction and representation code.
  • Vary the products internal representation.
  • Finer control over the production process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CDP: What are the 4 components of the BDP?

A

Builder : Specifies the interface for construction of the complex object.
ConcreteBuilder : Defines the parts of the builder interface.
Director : Object that takes responsibility of the object construction, but delegates actual construction to ConcreteBuilder class.
Product : Complex object created by the builder.

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

CDP: What are the 3 advantages of BDP?

A

Encapsulate construction of complex object.
Allows multi-step construction = Customisation.
Hides the internal representation of the product from the client.

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

CDP: When do we use FDP?

A

When we need to create varying types of the same super object.

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

CDP: What are the 4 components of FDP?

A

Creator; ConcreteCreator; ConcreteProduct; Product.

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

CDP: What is the role of the Concrete Classes in FDP?

A

They are responsible for defining the varying implementations of objects.

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

CDP: What is an abstract FDP?

A

Architecture that uses re-usable code to create multiple objects.

17
Q

CDP: What are the 2 differences between FDP and AFDP?

A

FDP creates one object whereas AFDP creates multiple.
FDP uses inheritance whereas AFDP uses composition.

18
Q

What type of pattern is the Command Design Pattern?

A

Behavioural Pattern.

19
Q

Define the Command Design Pattern?

A

Architecture in which requests are encapsulated in objects. Mostly used in GUI.

20
Q

What are the 5 components of the Command Design Pattern?

A

Invoked
Command interface
ConcreteCommand
Receiver
Client

21
Q

Define Decorator Pattern…

A

A structural design pattern. Behaviours of the object are added after compilation, at run time. This prevents class explosion.

22
Q

What is Class Explosion?

A

The overuse of different classes to implement object behaviours or attributes. It leads to expensive overheads.

23
Q

What is a solution to Class Explosion? What might be an issue with this solution?

A

Create instance variables of all the potential behaviours of an object, and set boolean values in the superclass to distinguish which behaviours are needed for that specific object.

An issue might be the need to recompile whenever a behaviour is added.

24
Q

What are the 2 principles for the Decorator Pattern?

A

Open - Close
Favour object composition over class inheritence.

25
Q

What is the difference between decorator objects and the concrete object?

A

Decorator objects are behaviours added to the concrete object.

26
Q

How does the delegation model operate when calling methods in the decorator pattern?

A

The delegation model enables methods in the decorators to be called, and delegate all the way back to the concrete object.