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.
What pattern may you use if you want to dynamically add functionality to an object over its runtime?
Decorator
We want to design a coffee shop that creates Coffee objects with different toppings. What pattern may we use?
Decorator
How may we create a Decorator pattern object?
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.
How does a Decorator pattern add functionality?
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.
What pattern may you use if you want to securely allow an object to communicate with another?
Command
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?
Command
How may we implement a Command pattern object?
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.