Creational Flashcards
What is a creational design pattern?
Patterns concerned with the method of creating objects.
These patterns have decisions to be made at the time of instantiation of the object.
What are the types of creational design patterns
Factory Method,
Abstract Factory,
Singleton,
Prototype,
Builder,
Object Pool
What is a Factory Method Pattern?
The pattern is defining an interface or abstract class for creating an object, AND the sub-classes decide how to instantiate. [ Inheritance Model ]
This pattern promotes loose-coupling, the code interacts solely with the interface or abstract class.
[For example] if a utility company needs a database with multiple rate plans, the abstract class would have a #rate property, and an undefined getRate() method. The subclasses [ Commercial, Residential, etc. ] would have their own instantiation of each getRate().
What is an Abstract Factory?
An Abstract Factory pattern defines an interface or abstract class for creating families or objects. AKA “kit”
[ Composition model ]
It promotes consistency among objects. It is a good fit if the system needs to be independent of how its objects are created and composed. Or, if the family of objects need to be used together.
What is a singleton?
A singleton is a class that only has one instance and provides a global point to access it.
The class must ensure that only a single instance should be created and only a single object can be used.
Singletons save memory because the object is not created at each request.
Mostly used in database or multi-threaded applications.
What are the two forms of a singleton?
Easy instantiation: the creation of the instance at load time
Lazy instantiation: the creation of the instance when required
What is a Prototype?
The prototype pattern is the cloning of an existing object instead of creating a new instance. The clone can be customized as per the requirement.
Prototypes reduce the need for sub-classing, hides complexity, and enables adding and removing objects at runtime.
Useful for when creating objects is expensive or you need to keep the number of classes to a minimum.
What is a builder?
A builder pattern constructs a complex object from simple objects using a step-by-step approach.
This pattern provides clear separation between the construction and representation of the object, it provides better control over construction, and it supports change to the internal representation of objects.
An example would be a pizza order application where the client can request various toppings and other options.
What is an Object Pool?
An object pool pattern is a container that holds a specific amount of objects, when an object is taken from the pool, it is not available until it is put back.
Objects in the pool have a lifecycle: creation, validation, and destruction.
An object pool helps manage resources in a better way: it boosts performance, it manages connections a provides a way to reuse and share them, and it can provide a limit for maximum amount of objects.
What Creational Patterns are rarely seen in modern programming?
Abstract Factory, Factory Method, Prototype, and Singleton
Why is a Factory Method unpopular?
Uses inheritance to add one method. Alone an Abstract Factory (composition) would be preferable.
They make your code inflexible by defining the classes at compile time.
In languages with higher order functions, a function could just be passed in as an argument.
Why is a prototype pattern unpopular?
A data structure such as a hashmap could be passed in to configure the object instead.
Instead of trying to create from multiple pre-defined child classes. Much less code to write if there is just one configurable object.
Why is a Singleton Pattern unpopular?
Even as singular global objects are used frequently. Forcing an object to know and enforce that it is the only instance can make testing more difficult.
And it is extra logic to enforce that there is only one.
What are the most useful creational pattern?
The Builder Pattern, it provides a single object whose API builds and returns an object hierarchy for us.
And Object Pool