Design Patterns Flashcards
What are the 3 categories of design patterns?
- Creational
- Structural
- Behavioural
What are creational design patterns?
Creational design patterns are patterns that control how objects are created
What are structural design patterns?
Creational design patterns are patterns that are concerned with how classes and objects can be composed, to form larger structures.
What are behavioural design patterns?
Behavioural patterns are common patterns for facilitating elegant communication between objects.
What are examples of creational design patterns?
- The Singleton pattern, for ensuring only a single instance of a class can exist
- The Abstract Factory pattern, for creating an instance of several families of classes
- The Prototype pattern, for starting out with an instance that is cloned from an existing one
What are examples of structural design patterns?
- The Adapter pattern, for creating an interface to enable classes that normally can’t work together, to work together.
- The Bridge pattern, for splitting a class that should actually be one or more, into a set of classes that belong to a hierarchy, enabling the implementations to be developed independently of each other.
- The Decorator pattern, for adding responsibilities to objects dynamically.
What are examples of behavioural design patterns?
- The Template pattern, for deferring the exact steps of an algorithm to a subclass.
- The Mediator pattern, for defining the exact communication channels allowed between classes.
- The Observer pattern, for enabling classes to subscribe to something of interest, and to be notified when a change occurred.
What is the Observer design pattern?
Observer is a behavioural design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
Also known as: Event-Subscriber, Listener
An Observer (usually inherited from an abstract base class) keeps a list of subscribers, which are notified on changes. The subscribers itself also inherit from an abstract base class together with the update() method.
What is the Adapter design pattern?
Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
Also known as: Wrapper
Use the Adapter class when you want to use some existing class, but its interface isn’t compatible with the rest of your code. The Adapter pattern lets you create a middle-layer class that serves as a translator between your code and a legacy class, a 3rd-party class or any other class with a weird interface.