Design Patterns Flashcards
What is a Singleton?
A singleton is a class that wraps itself attempting to ensure that only one instance of the class is ever used during runtime.
Singletons contain an instance property that is an instance of itself, alongside a static method that gets that instance of itself. The singleton class then contains any other functionality the class required.
Despite violating the Single responsibility principle, can be useful when needing a global object accessible across the environment.
What is a factory?
A factory is a function/method that simplifies class instantiation where a standard operation would be repeated (e.g. type = button).
When To Use: When repeated instantiation, especially with consistent specification/constructor arguments, occurs.
NON-FACTORY: new Element() + element.type = "button" FACTORY: Element.CreateButton();
What is an abstract factory?
An abstract factory is a factory - a method that simplifies class instantiation - but without specifying concrete classes - instead abstract classes or interfaces. It is an abstraction of a factory
Element.CreateButton(AndroidButton)
What is a factory method?
Factory Method provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created (by inheritance)
What is the difference in approach between an abstract factory & a factory method
Composition vs. inheritance.
Both are factories - simplifying object instantiation. However the abstraction changes - an abstract factory relies on an interface for interchangable classes that share interfaces (public method definitions) using composition. A factory method uses inheritance of a high-level abstract class that is inherited from.
What is a Builder pattern?
Builders construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
When To Use: When many multiple classes inherit a base class and require complex constructors and/or manual steps after initialisation.
What is a Prototype pattern?
Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
When To Use: When deep copies of objects are required.
What is an Adapter class?
An Adapter class should be used when a class or interface does not match another but need to be used in the context of another hole. For example to fit a square peg in a round hole, use a square peg adapter.
What is a Bridge?
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
For example if there are 2 circle and 2 square classes, and each has a colour assingned, that is four classes. However instead a bridge can be used - a shape class and a colour bridge.
What is a Composite?
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
What is a (class) Decorator?
Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects.
What is a Facade?
Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.