Академический Документы
Профессиональный Документы
Культура Документы
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.
Design Patterns
1. Creational
1. Factory Method IMPORTANT
2. Abstract Factory IMPORTANT
3. Singleton IMPORTANT
4. Builder IMPORTANT
5. Prototype
2. Structural
1. Adapter IMPORTANT
2. Bridge
3. Composite
4. Decorator
5. Facade IMPORTANT
6. Flyweight
7. Proxy IMPORTANT
3. Behavioral
1. Chain of Responsibility
2. Command
3. Interpreter
4. Iterator IMPORTANT
5. Mediator IMPORTANT
6. Momento
7. Observer IMPORTANT
8. State
9. Strategy
10. Template Method
11. Visitor
4. J2EE
1. Creational Patterns
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
Applicability:
Consequences:
Use in J2EE:
The Abstract Factory pattern is one level of abstraction higher than the factory pattern - it is a
factory object that returns one of several factories.
It is used when different families of Objects have to be used, for example the different look and
feels of a GUI.
Benefits:
Use in J2EE:
The Singleton patterns controls the number of instances of a class that can be created to a
predefined constant, typically but not always one. It provides a single global point of access to
these instances.
Benefits:
It avoids polluting the name space with global variables that store sole instances.
Permits a variable number of instances.
More flexible than static methods.
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
Benefits:
When to use:
The algorithm for creating a complex object should be independent of both the parts that
make up the object and how those parts are assembled
The construction process must allow different representations of the constructed object
Use in J2EE:
Bhupendra Kandola Page 4
Design Pattern
Separates the push scheduler and the data formatter for HTTP GET requests.
1.5. Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by
copying this prototype. (e.g. Java Cloning)
Benefits:
Use in J2EE:
2. Structural Patterns
Convert the interface of a class into another interface clients expect. Adapter lets classes work
together that couldn't otherwise because of incompatible interfaces.
Benefits:
When to use:
Permits you to use an existing class that has an interface that does not match the one you
need.
You want to create a reusable class that cooperates with unrelated or unforeseen classes.
The Object Adapter pattern can be used when you need to use several existing subclasses
but it is impractical to adapt their interface by subclassing every one.
Use in J2EE:
2.2 Bridge
Decouple an abstraction from its implementation so that the two can vary independently. Two
hierarchies of classes that vary independently. Rarely used, however often combined with
Abstract factory.
When to use:
You want to avoid a permanent binding between an abstraction and its implementation.
Both the abstractions and the implementations should be extensible by subclassing.
Changes in the implementation should not impact clients.
Benefits:
Use in J2EE:
2.3. Composite
Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients
treat individual objects and compositions of objects uniformly.
When to use:
You want to represent the whole hierarchy or a part of the hierarchy of objects
You want client to ignore the difference between composite and leaf objects
The structure can have any level of complexity and is dynamic
Benefits:
Disadvantage:
Use in J2EE:
2.4. Decorator
Benefits:
When to use:
Use in J2EE:
Servlet spec v2.3 includes a mechanism to build, add and remove pre/post-processing
filters.
java.io streams library uses Decorator to achieve what Unix programmers do with pipes
& filters.
The EJB object is a decorator for the bean because the bean’s functionality is expanded to
include remote behavior.
Benefits:
Provides simple interface to a complex system without reducing options provided by the
system
Shields clients from subsystem components
Promotes weak coupling between the subsystem and its clients
Reduces coupling between subsystems is each subsystem only used Façades to interact
with other subsystems
Translates client requests to subsystem that can fulfill request
When to use:
Use in J2EE:
The Session Entity Façade pattern is a derivation of Façade that uses a Session bean as a
façade for multiple Entity beans.
2.6. Flyweight
Use sharing to support large numbers of fine-grained objects efficiently. Each instance does not
contain its own state, but stores it externally. A way of splitting objects into mutable and
immutable data.
Benefits:
When to use:
Benefits:
A remote proxy can hide the fact that the object resides in a different address space
a virtual proxy can perform optimisations, such as creating an object on demand
When to use:
You need a more versatile or sophisticated reference to an object than a simple pointer
Use in J2EE:
3. Behavioral
Behavioral: Characterize the ways in which classes or objects interact and distribute
responsibility.
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
handle the request. Chain the receiving objects and pass the request along the chain until an
object handles it. Set of handlers is dynamic.
Benefits:
Reduced coupling
Added flexibility in assigning responsibilities to objects
Allows a set of classes to behave as a whole, because events produced in one class can be
set on to other handlers within the composite
When to use:
More than one object can handle a request, and the hander isn't known
You want to issue a request to one of several objects without specifying the receiver
explicitly
The set of objects that can handle a request should be specified dynamically
Use in J2EE:
3.2. Command
Encapsulate a request as an object, thereby letting you parameters clients with different requests,
queue or log requests, and support undoable operations.
The Command pattern can be used to provide pluggable behavior which enforces client access to
services.
Benefits:
Separates the object performing an operation from the one that knows how to perform it
Easy to add new commands - don't have to change existing classes
When to use:
3.3. Interpreter
Given a language, define a representation for its grammar along with an interpreter that uses the
representation to interpret sentences in the language.
Benefits:
When to use:
Provide a way to access the elements of an aggregate object sequentially without exposing its
underlying representation.
Benefits:
When to use:
Define an object that encapsulates how a set of objects interacts. Mediator promotes loose
coupling by keeping objects from referring to each other explicitly, and it lets you vary their
interaction independently.
Benefits:
Decouples colleagues
Simplifies object protocols
Centralizes control
When to use:
2.6. Memento
Without violating encapsulation, capture and externalize an object’s internal state so that the
object can be restored to this state later.
Bhupendra Kandola Page 17
Design Pattern
Benefits:
When to use:
A snapshot of an object's state must be saved so that it can be returned to that state later
Using a direct interface to obtain the state would expose implementation details and
break the object's encapsulation
Define a one-to-many dependency between objects so that when one object changes state, all its
dependents are notified and updated automatically.
Benefits:
When to use:
2.8. State
Allow an object to alter its behavior when its internal state changes. The object will appear to
change its class.
Benefits:
When to use:
An object's behavior depends on its state and it must change its behavior at run-time
depending on that state
Operations have large, multipart conditional statements that depend on the objects state
2.9. Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy
lets the algorithm vary independently from clients that use it.
The Strategy pattern can be used to provide pluggable behavior which enforces client access to
services.
Benefits:
An alternative to subclassing
Defines each behavior in its own class - eliminating conditional statements
Easier to extend a model to incorporate new behaviors without recoding
When to use:
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template
Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s
structure.
Benefits:
When to use:
You want to implement invariant parts of an algorithm once and use subclasses to
implement the behavior that can vary
2.11. 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.
Benefits:
When to use:
An object structure contains many classes of objects with differing interfaces and you
want to perform operations on these objects that depend on concrete classes
Classes defining the object structure rarely change but you often want to define new
operations over the structure