Design Patterns Flashcards
What is the purpose of Design Patterns?
Create an architecture that enables the re-use of code blocks to create customised objects.
What are the 5 design pattern principles?
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.
What are the 3 design pattern categories?
Factorial (Object Creation)
Structural (Object composition)
Behavioural (Object interaction)
CDP: When should we use the singleton pattern?
When we only need one instance of the class.
CDP: How do we create a Singleton Object?
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.
CDP: What are the 2 types of initialisation of a Singleton Object?
Eager Instantiation : Create object when class is loaded.
Lazy Instantiation : Create object only when you need.
CDP: Why might multithreaded programs cause issues with Singleton instantiation? How can we resolve this?
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.
CDP: What is the most effective way of implementing thread management in the Singleton Class?
Create a condition in the instantiation method which only instantiates the reference if the object is currently null.
CDP: Define the Builder Pattern…
Separates the construction process from the objects representation. This enables isolation and encapsulation of production, meaning many representations can be created.
CDP: What are the 3 qualities that BDP enables?
- Separate construction and representation code.
- Vary the products internal representation.
- Finer control over the production process.
CDP: What are the 4 components of the BDP?
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.
CDP: What are the 3 advantages of BDP?
Encapsulate construction of complex object.
Allows multi-step construction = Customisation.
Hides the internal representation of the product from the client.
CDP: When do we use FDP?
When we need to create varying types of the same super object.
CDP: What are the 4 components of FDP?
Creator; ConcreteCreator; ConcreteProduct; Product.
CDP: What is the role of the Concrete Classes in FDP?
They are responsible for defining the varying implementations of objects.