Defines a one to many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
Java's built in Observer pattern
Subject(Object under observation) needs to implement "Observable"
This subject can notify its dependent by calling following methods sequentially
1.setChanged()
2.NotifyObserver() / notifyObservers()
Observer's needs to implement "Observer" interface and register / unregister to the observable as
addObserver(Observable)
removeObserver(Observable)
2.Decorator Pattern
Attaches additional responsibilities to an object dynamically.
Decorator provides a flexible alternative to subclassing for extending functionality.
Diagrammatically it is represented as
3.Factory Method Pattern
Defines an interface for creating an object but lets subclasses decide which class to instantiate.
(Factory method lets class defer instantiation to subclass)
Diagrammatically this can be represented as
4. Abstract Factory Pattern
Provides an interface for creating families of related / dependent objects without specifying their concrete classes.
Can be thought as Factory of Factory...
One of example of abstract Factory is
javax.xml.validation.SchemaFactory it has static factory method newInstance( ) which Lookup an implementation of the
SchemaFactory
that supports the specified schema language and return it.5. Command Pattern
Encapsulates a request as an object thereby letting you parameterize other objects with different requests / queue .
You can provide functionality of undo too.
Typical client for the command uses as follows
{
Command cmd = new ConcreteCommand();
cmd.execute();
}
Following diagram will make it more clearer with action receiver .
6. Adapter Pattern
Converts the interface of a class into another interface , the client expects.
Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
7. Template Method Pattern
Defines the skeleton of an algorithm in a method, deferring some steps to sub classes.
Template method lets subclasses redefine certain steps of an algorithm without changing overall algorithm structure.
If you look @ Spring ,It has many templates defined
JMSTemplate, JDBCTemplate
JDBCTemplate simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow (get connection , cerate statement , set parameter , set connection, execute statement , extract results ), leaving application code to provide SQL and extract results.
8. Composite Pattern
Allows you to compose objects into tree structure to represent part-whole hierarchy.
Composite lets client treat individual objects and composition of objects uniformly.
Diagram :
9. State Pattern
Allows an object to alter its behavior when its internal state changes.
The object will appear to change its class .
Like a coffee vending machine changing state from working to bin full ,out of milk... states...
Diagrammatically it is represented as below
10. Proxy Pattern
Provide a surrogate / placeholder for another object to control access to it.
Use the proxy pattern to create a representative object that controls access to another object which may be remote / expensive to create / in need of securing
1. Remote Proxy
Controls access to remote object
2. Virtual Proxy
Controls access to resource which is expensive to create
3. Protection Proxy
Controls access to a resource based on access rights.
No comments:
Post a Comment