Design Patterns Summary
I) Creational Patterns
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code
- used in place of direct instantiation with constructors
- make creation adapatable and dynamic
- flexibility with which and how objects are created and how they are initialized
a) Factory
- object creation is centralised
- decide specific subclass at runtime
- client uses interface for object creation
b) Abstract Factory
- another layer of abstraction over group of factories
c) Builder
- construct complex objects with only necessary blocks
- avoid unused parameters
- build different types of immutable objects
d) Prototype -> objects that perform cloning
- lets copy existing objects without making class code dependent(coupling?)
Problem: - can’t copy private members in object directly
- has to know the exact concrete class used by original object
Solution: - objects perform the cloning through a common interface
- pre-configured objects can be used instead of sub-classing
e) Singleton
- only one instance of the class exists at any given time (per JVM??)
- global access to this single instance
- constructor is private and creation is facilitated through static getInstance() method -> makes
new
unusable
II) Structural Patterns
shows how to assemble objects and classes into large structures while keeping them flexible and extensible
- lets changing one part without affecting entire appln structure
a) Adapter
allows objects with incompatible interfaces to collaborate
- example: xml to json converter
- hides complexity of conversion
- can also be two-way adapter
Q: “help objects with different interfaces collaborate” how ??
b) Bridge
split a large class or set of closely related classes into seperate hierarchies - abstraction and implementation
developed independently of each other
- allows loose coupling
Q: object composition??
c) Composite
takes a group of objects into a single object
- lets you compose objects into tree structures and then work with these structures as if they were individual objects
d) Decorator
allows adding additional features to a particular instance of a class without affecting remaining instances of same class
e) Facade
provide simple interface to a complex object
f) Flyweight
reduces the cost of complex object models
- reduce ram space by sharing common state between multiple objects
g) Proxy
placeholder interface to an underlying object to control access, reduce complexity
III) Behavioral Patterns
concerned with algorithms and assignment of responsibilities between objects
a) Chain of Responsibility
- chain of request handlers
- process if needed and pass to next handler
b) Command
- turn a request into an object with all its info
- pass requests as method arguments
Q: support undoable operations?
c) Iterator
- traverse elements of collection without exposing its underlying representation (list, stack, tree, etc.)
d) Mediator
- restrict direct comm between objects and forces via mediator
e) Memento
- store/restore state of object
f) Observer
- mechanism to notify multiple objects about an event / state change in another object
g) State
- object alters its behaviour on its internal state changes
- asif object changes its class
h) Strategy
- family of algorithms into seperate class but make their objects interchangable
- class with specifc taks in diffent ways(multiple algos) -> seperate classes called strategies
- original main class -> context
- context delegates work to linked strategy objects chosen by client
i) Template method
- abstract superclass defines skeleton of an algorithm with default behaviour and necessary methods
- subclass overrides specific steps
j) Visitor
separate algorithms from the object on which they operate