Types: creational, structural and behavioral
Patterns: solutions to recurring design problems
Creational: addresses complexity of object creation
Structural: deals with composition of objects
Behavioral: deals with interaction between objects
Creational Design Patterns
Prototype: cloned or initial setup, fully initialized object e.g. Chessboard
Builder: separates object construction from it’s representation e.g. buffet meal builder, coffee builder. Implementation: inner static class
Singleton: single instance per JVM e.g. president of a country. Implementation: private constructor,
Factory Method: create a family of object types, adding new types to family is easier e.g. car factory. Implementation: a class with method getCar(make, model), Car abstract class, Altima class, Camry class etc
Structural Design Patterns
Proxy: an object representing another object, hides complexity in communicating with real object e.g. proxy setting, redirect URL, credit card, ejb remote objects
Decorator: add responsibilities to objects dynamically e.g. make pizza with toppings decorator, discount, Java I\O classes
Facade: a single class that represents an entire sub systems. Minimizes network calls, defines transactions boundary ( online order – invoice, payment etc)
Adapter: match interfaces of different object e.g mobile charger (US, India), Java to JSON or XML
Flyweight: A flyweight is an object that minimizes memory usage by sharing as much data as possible with other similar objects e.g. jdbc connection pool, string intern
Behavioral Design Patterns
Chain of responsibility: a way of passing a request between a chain of objects e.g. loan approval process, exceptions handling multi-catch
Iterator: sequentially access the elements of a collection
State: alter an object behavior when it’s state changes e.g. fan speed. (Next) state is decided based on current state.
Strategy: encapsulate an algorithm inside a class e.g. compare() different types of sorting algorithms
Observer: a way of notifying a change to a number of objects e.g. payment received, notify() in object class. Notifier class with register(a) method.
Visitor: define operations to class without changing code ( extend but no modifications) open/closed principle.
Clients traverse the object structure and call a dispatching operation accept(visitor) on an element — that “dispatches” (delegates) the request to the “accepted visitor object”. The visitor object then performs the operation on the element (“visits the element”).
Template: defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. E.g. AbstractController.handleRequestInternal() class in Spring MVC
Command: An object is used to encapsulate all information needed to perform an action or trigger an event at a later time. Four terms always associated with the command pattern are command, receiver, invoker and client. E.g. order meal in restaurant (paper with order detailse is the command). E.g. HttpRequest, Runnable interface,
Memento: capture and restore an objects internal state e.g. Undo\Redo operations, Deserialization
Mediator: defines simplified communication between classes e.g. Air Traffic Controller, ESB bus,