Design Patterns Flashcards
What are design patterns?
Design patterns are patterns intended each to provide a general reusable solution to one type of problem during software development.
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?
Factory
How may a factory pattern be realised?
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.
We want to represent the ability for a user to order pizza from different pizza shops. What pattern may we use?
Factory
What design may we use when we want to create a family of related objects without revealing the concrete classes they come from?
Abstract Factory
What is the difference between a Factory and an Abstract Factory?
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.
What pattern may you use if you want to generate a complex object in steps?
Builder
We want to create a complex Pizza object ingredient-per-ingredient. What pattern may we use?
Builder
Why may we use a Builder pattern?
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.
What pattern may you use if you want there to only be a single instance of an object?
Singleton
We want a object to be created that is singular, and manages a set of smaller objects. What pattern may we use?
Singleton
Why may we use a Singleton pattern?
We may want this, for example, for a file system or window manager, to store state based information on the program in one location.
What is the difference between eager and lazy initialisation?
HINT: Singletons
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 may we create a Singleton pattern object?
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 may we create a Builder pattern object?
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.