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
Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts
Saturday, February 7, 2009
Wednesday, February 4, 2009
SCEA1.5 - Few Design patterns
Service Activator: ----
Problem:a business service such as a session or entity bean provides only synchronous processing and thus presents a challenge to implementing asynchronous processingSolution: Use a Service Activator to receive asynchronous client requests and messages. On receiving a message, the Service Activator locates and invokes the necessary business methods on the business service components to fulfill the request asynchronously.
The ServiceActivator is a JMS Listener and delegation service that requires implementing the JMS message listener-making it a JMS listener object that can listen to JMS messages. The ServiceActivator can be implemented as a standalone service. Clients act as the message generator, generating events based on their activity.
Any client that needs to asynchronously invoke a business service, such as an enterprise bean, may create and send a message to the Service Activator. The Service Activator receives the message and parses it to interpret the client request. Once the client's request is parsed or unmarshalled, the Service Activator identifies and locates the necessary business service component and invokes business methods to complete processing of the client's request asynchronously.
The Service Activator may optionally send an acknowledgement to the client after successfully completing the request processing. The Service Activator may also notify the client or other services on failure events if it fails to complete the asynchronous request processing.
The Service Activator may use the services of a Service Locator to locate a business component.
Virtual Proxy: ---Application is often a collection of components and in most of the situations it may be the case that a component should be loaded until it is first accessed by the Client. Reasons may be that the component in consideration may be using most of the system resources. For example, consider the Microsoft Word Application which is providing Printer Service and the Help System. It should not be the case that both the Printer Service and the Help System should be loaded during the Application startup. Imagine what would be the case if the Client after starting the Word Application is accessing other part of the System and not the Printer and the Help Service. Definitely it will lead to a slow-response time because the Components are un-necessarily loaded.
Virtual Proxy Pattern comes into picture here as it defers the Object Creation process of memory-intensive components thereby speeding up the Application. Now, let us see how to design the Virtual Proxy.
Composite View: --- ProblemInstead of providing a mechanism to combine modular, atomic portions of a view into a composite whole, pages are built by embedding formatting code directly within each view.modification to the layout of multiple views is difficult and error prone, due to the duplication of code.
SolutionUse composite views that are composed of multiple atomic subviews. Each component of the template may be included dynamically into the whole and the layout of the page may be managed independently of the content.
This solution provides for the creation of a composite view based on the inclusion and substitution of modular dynamic and static template fragments. It promotes the reuse of atomic portions of the view by encouraging modular design. It is appropriate to use a composite view to generate pages containing display components that may be combined in a variety of ways. This scenario occurs, for example, with portal sites that include numerous independent subviews, such as news feeds, weather information, and stock quotes on a single page. The layout of the page is managed and modified independent of the subview content.
View Helper : --- ProblemPresentation tier changes occur often and are difficult to develop and maintain when business data access logic and presentation formatting logic are interwoven. This makes the system less flexible, less reusable, and generally less resilient to change.SolutionA view contains formatting code, delegating its processing responsibilities to its helper classes, implemented as JavaBeans or custom tags. Helpers also store the view's intermediate data model and serve as business data adapters.
Service to Worker: ---The system controls flow of execution and access to business data, from which it creates presentation contentThe Service to Worker pattern, like the Dispatcher View pattern, describes a common combination of other patterns from the catalog. Both of these macro patterns describe the combination of a controller and dispatcher with views and helpers. While describing this common structure, they emphasize related but different usage patterns.
Combine a controller and dispatcher with views and helpers to handle client requests and prepare a dynamic presentation as the response. Controllers delegate content retrieval to helpers, which manage the population of the intermediate model for the view. A dispatcher is responsible for view management and navigation and can be encapsulated either within a controller or a separate component.
Problem:a business service such as a session or entity bean provides only synchronous processing and thus presents a challenge to implementing asynchronous processingSolution: Use a Service Activator to receive asynchronous client requests and messages. On receiving a message, the Service Activator locates and invokes the necessary business methods on the business service components to fulfill the request asynchronously.
The ServiceActivator is a JMS Listener and delegation service that requires implementing the JMS message listener-making it a JMS listener object that can listen to JMS messages. The ServiceActivator can be implemented as a standalone service. Clients act as the message generator, generating events based on their activity.
Any client that needs to asynchronously invoke a business service, such as an enterprise bean, may create and send a message to the Service Activator. The Service Activator receives the message and parses it to interpret the client request. Once the client's request is parsed or unmarshalled, the Service Activator identifies and locates the necessary business service component and invokes business methods to complete processing of the client's request asynchronously.
The Service Activator may optionally send an acknowledgement to the client after successfully completing the request processing. The Service Activator may also notify the client or other services on failure events if it fails to complete the asynchronous request processing.
The Service Activator may use the services of a Service Locator to locate a business component.
Virtual Proxy: ---Application is often a collection of components and in most of the situations it may be the case that a component should be loaded until it is first accessed by the Client. Reasons may be that the component in consideration may be using most of the system resources. For example, consider the Microsoft Word Application which is providing Printer Service and the Help System. It should not be the case that both the Printer Service and the Help System should be loaded during the Application startup. Imagine what would be the case if the Client after starting the Word Application is accessing other part of the System and not the Printer and the Help Service. Definitely it will lead to a slow-response time because the Components are un-necessarily loaded.
Virtual Proxy Pattern comes into picture here as it defers the Object Creation process of memory-intensive components thereby speeding up the Application. Now, let us see how to design the Virtual Proxy.
Composite View: --- ProblemInstead of providing a mechanism to combine modular, atomic portions of a view into a composite whole, pages are built by embedding formatting code directly within each view.modification to the layout of multiple views is difficult and error prone, due to the duplication of code.
SolutionUse composite views that are composed of multiple atomic subviews. Each component of the template may be included dynamically into the whole and the layout of the page may be managed independently of the content.
This solution provides for the creation of a composite view based on the inclusion and substitution of modular dynamic and static template fragments. It promotes the reuse of atomic portions of the view by encouraging modular design. It is appropriate to use a composite view to generate pages containing display components that may be combined in a variety of ways. This scenario occurs, for example, with portal sites that include numerous independent subviews, such as news feeds, weather information, and stock quotes on a single page. The layout of the page is managed and modified independent of the subview content.
View Helper : --- ProblemPresentation tier changes occur often and are difficult to develop and maintain when business data access logic and presentation formatting logic are interwoven. This makes the system less flexible, less reusable, and generally less resilient to change.SolutionA view contains formatting code, delegating its processing responsibilities to its helper classes, implemented as JavaBeans or custom tags. Helpers also store the view's intermediate data model and serve as business data adapters.
Service to Worker: ---The system controls flow of execution and access to business data, from which it creates presentation contentThe Service to Worker pattern, like the Dispatcher View pattern, describes a common combination of other patterns from the catalog. Both of these macro patterns describe the combination of a controller and dispatcher with views and helpers. While describing this common structure, they emphasize related but different usage patterns.
Combine a controller and dispatcher with views and helpers to handle client requests and prepare a dynamic presentation as the response. Controllers delegate content retrieval to helpers, which manage the population of the intermediate model for the view. A dispatcher is responsible for view management and navigation and can be encapsulated either within a controller or a separate component.
Labels:
Design Patterns,
J2EE,
Java,
SCEA,
Sun Certified Enterprise Architect
Subscribe to:
Posts (Atom)