Saturday, February 7, 2009

SCEA1.5 - Design Patterns

Benefits of using design patterns:
Improves communication between designers by use of pattern names vs. the details of the patterns.
Captures experience of solving a type of problem.
Provide a way of reusing design.
Provide a mechanism for making designs more reusable.
Provides a mechanism for systematizing the reuse of things that have been seen before.
Can be used to teach good design.

Abstract Factory:
The Abstract Factory pattern is used for creating many objects that are dependent on each other.
Also known as Kit
Used in J2EE - DAO and VO assembler, J2SE-java.awt.Toolkit
J2EE technology uses this pattern for the EJB Home interface, which creates new EJB objects.
Related patterns-Factory method,Prototype, concrete factpty often Singleton.
It isolates concrete classes.
It makes exchanging product families easy.
It promotes consistency among products.Supporting new kinds of products is difficult.

Builder:
The builder pattern separates the construction and representation of an object. The client is shielded from the objects construction only needing to specify it's content and type.
Related patterns-Abstract factory is similar to builder in that it too may construct complex objects,The primary difference is that the builder pattern focuses on constructing a complex object step by step, AbstractFactory's emphasis is on families of product objects(either simple or complex), Builder returns the product as a final step,but as far as the Abstract pattern is concerned, the product gets returned immediately. A composite is what is builder often builds.

Factory Method:
Factory Method pattern provides an interface for creating an object that allows either sub classes or helper classes to create that object.
Aslo knowna as: virtual constructor
Used in J2EE-EJBHome, EJBLocalHome, QueueConnectionFactory, TopicConnectionFactory, J2SE- Collator, ContentHandlerFactory, InitialContextFactory, SocketFactory
PrototypeJ2EE technology uses this pattern for the EJB Home interface, which creates new EJB objects.
Aslo knowna as: virtual constructor
Related patterns: absract factory,within Template Methods,
Eliminates the need to bind application-specific classes into your code.
Gives subclasses a hook for providing an extended version of an object being constructed.

Prototype:
The Prototype pattern is used to create new objects by copying its prototype.specify the kinds of objects to create using a prototypical instance,and create new objects by copying this prototype.
Used in J2SE-java.lang.Object
Related patterns-Prototype and abstract factory are competing is some ways.
Design that make heavy use of the composite and decorator patterns often can
benefit from prototype as well.

Singleton:
Ensure a class only had one instance and provide a global point of access to it.The Singleton doesn't just create a single instance it can also be used to create a variable number of instances of a class.J2SE-java.lang.Runtime
Related patterns:Abstract factory,builder and prototye can be implemented using singleton

Adapter:
The Adapter pattern implements an interface known to its clients and provides an instance of a class not known to its clients.
Also known as: Wrapper
Used in JCA architecture, J2SE-java.awt.event.ComponentAdapter
Related patterns-Bridge,Decorator,Proxy

Bridge:
The Bridge pattern creates a separation between abstractions and classes that implement those abstractions
Also known as Handle/Body
Related patterns-An abstract factory can create and configure a particular bridge.

Composite:
Compose objects into tree structure to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly
Related patterns: Chain of responsibility, Decorator, Flyweight, Iterator, Visitor

Decorator Pattern:
The Decorator pattern isn't used to build objects. It adds extra functionality to existing objects
Also known as: Wrapper
Used in J2EE-EJBObject, J2SE-BufferedReader
Related patterns: Adapter,Composite,Strategy
In J2EE technology, The EJB object is a decorator for the bean because the bean’s functionality is expanded to include remote dehavior.

Facade:
Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher level interface that makes the sub system easies to use
Used in J2SE-java.net.URL
Related patterns: AbstractFactory, Mediator,Single tons

Flyweight:
Use sharing to support large numbers of fine grained objects efficientlyWhen the instances of your class can be used interchangeably and you want to reduce the number of instances created in order to improve performance
Used in J2SE-java.lang.String
Related patterns: Composite,State and Strategy

Proxy:
Provide a surrogate or placeholder for another object to control access to it In this scenario what you are essentially trying to do is filter all packets that don't meet a certain set of requirements. This behavior is just like a Proxy server dropping packets from certain IP address etc
Also known as Surrogate
The EJB’s remote interface(EJBObject) acts as a proxy for the bean. Proxy is also used in RMI.
Related patterns: Adapter,Decorator

Chain of responsibility:
Avoid coupling the sender of a request to its receiver by giving mmore than one object a chance to handle the request.Chain the receiving objects and passthe request along the chain until an object handles it.
Uused in J2EE-ReqestDispatcher in the servlets/JSP API
Related patterns: composite

Command:
Encapsulate a request as an object,there by letting you parameterize clients with different requests,queue or log requests and support undoable operations
Also known as Action of Transaction
Used in J2EE-Message Beans,servlets and JSPs
Related patterns: Composite,Memonto

Interpreter:
Given a language,define a representation for its grammer along with an interpreter that uses the representation to interpret sentences in the language.
Related patterns:composite,iterator,visitor
Iterator:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Also known as- Cursor
Used in J2SE- Iterator,Enumaration
Related patterns: Composite,Factory method,Memento

Mediator:
Define an object that encapsulates how a set of objects interact. mediator promotes loose coupling by keeping objects from referring to each other explicitly
and it lets you vary their interaction independentlyThe Mediator pattern allows you to co-ordinate state changes between other objects by using one object.
Related patterns: Facade Observer

Memento:
Without violating encapsulation, capture and externalise an object's internal state so that the object can be restored to this state later.
Also known as Token
J2EE-Entiry Bean using Bean managed persistence
Related patterns: Command, Iterator

Observer:
Define a one to many dependency between objects so that when one object changes its state,all its dependents are notified and updated automatically.
When you need classes to be notified of events but you don't know which classes or if you will need to add more at a later date.
Also known as: Dependents,Publish-subscribe
Used in J2SE-Observable,Observer
Related patterns:Mediator.Singleton

State:
Allow an object to alter its behavior when its itnernal state changes.The object will appear to change its class.
Also known as: Objects for states
Related patterns:Flyweight, singletons

Strategy:
Define a family of algorithms, encapsulate each one,and make them interchangable. Strategy lets the algorithm vary independently from clients that uses it.
Also known as-Policy
Related patterns: Flyweight

Template method:
Define the skeleton of an algorithm in an operation,deferring spme steps to sub classes.Template method lets sub classes redefine certain steps of an algorithm without changing the algorithms structure
Related patterns: Factory method,Strategy

Visitor:
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates
Related patterns: Composite, Interpreter