Design Patterns & Principles Flashcards
STRATEGY design pattern
- Defines a family of algorithms, encapsulates each one, and makes them interchangeable through client code at runtime.
- Involves using interfaces like Comparators and making different classes for each strategy.
ITERATOR design pattern
Provide a way to access elements of an aggregate object without exposing its underlying encapsulation using ITERABLE and ITERATOR
Interface segregation principle
NULL OBJECT design pattern
- only applicable when there is a type hierarchy
- create a class to represent a null object
FLYWEIGHT design pattern
- used to ensure uniqueness of objects of a class
- components:
1. private constructor
2. static flyweight store: to store collection of flyweight objects
3. static access method: returns unique flyweight object that corresponds to identification key. Usually checks whether it already exists in the store, creates one if it does not.
SINGLETON design pattern
- used to ensure there is only one instance of a given class at any point in the execution of code.
- components:
1. private constructor
2. private static final global variable: to hold reference to single instance
3. static accessor method: usually called instance() that returns the singleton instance.
COMPOSITE design pattern
- composite class that implements the interface which contains an aggregate of that interface -> it can store other classes that implement that interface
- two ways to specify which instances of the interface form the aggregate:
1. add method in composite class
2. initialize through constructor in composite class
DECORATOR design pattern
- when we want to decorate some object with additional features while being able to treat the decorated object like any other object of the undecorated type
- Decorator class implements the interface and contains an aggregate of instances of that interface
- the field that stores a reference to the decorated object is final and initialized in the constructor
PROTOTYPE design pattern
- relying on a polymorphic copying mechanism and create new instances of an object of interest by copying a prototype object
- when you need to create objects whose type may not be known at compile time
- use dependency injection to inject an object into a class via its constructor
COMMAND design pattern
- have a Command interface with methods like execute(), undo(), getDescription()
- define specific commands as objects/classes
TEMPLATE METHOD design pattern
put all common code in the superclass and define some hooks to allow subclasses to provide specialized functionality where needed
- declare template method to be final(cannot be overidden by subclasses)
- the method implemented by subclasses is protected and abstract
OBSERVER design pattern
- create an Observer interface
- model holds an aggregate of Observers
- when a method is called that changes the model, a notification method is called to notify specific observers of the change through their callback methods.
VISITOR design pattern