11_GoF-Patterns-Structural
11_GoF-Patterns-Structural
Structural
Structural Patterns
Structural Patterns
Deal with composition of classes or object
•Adapter •Decorator
•Composite •Façade
•Proxy •Flyweight
•Bridge
Adapter
Adapter
Object Diagram for
Adapter using Socket
Adapter Example
You want to use an existing class, and its interface does not
match the one you need.
1
Model: Adapter object Pattern
1
Composite
Composite
The Composite composes objects into tree structures, and lets clients treat
individual objects and compositions uniformly.
Intent
Compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects
uniformly.
Problem
Application needs to manipulate a hierarchical collection of "primitive" and
"composite" objects. Processing of a primitive object is handled one way,
and processing of a composite object is handled differently. Having to
query the "type" of each object before attempting to process it is not
desirable
Composite
Example: figures in a structured graphics toolkit
Controller
0..* 0..*
View Figure
children
paint()
translate()
getBounds()
Intent
Provide a surrogate or placeholder for
another object to control access to it.
Also Known As
Surrogate
Problem
• You need to support resource-hungry objects,
and you do not want to instantiate such
objects unless and until they are actually
requested by the client.
Illustration :problem
Consider a document editor ( Html creator ) that can embed graphical objects in a
document. Some graphical objects, like large raster images, can be expensive to
create. But opening a document should be fast, so we should avoid creating all
the expensive objects at once when the document is opened. This isn’t necessary
anyway, because not all of these objects will be visible in the document at the
same time.
These constraints would suggest creating each expensive object on demand,
which in this case occurs when an image becomes visible.
But what do we put in the document in place of the image? And how can we hide
the fact that the image is created on demand so that we don’t complicate the
editor’s implementation?
Illustration: solution
• The solution is to use another object, an image proxy, that acts as a
stand-in for the real image.
• The proxy acts just like the image and takes care of instantiating it
when it’s required.
• The image proxy creates the real image only when the document
editor asks it to display itself by invoking its Draw operation.
• The proxy forwards subsequent requests directly to the image.
• It must therefore keep a reference to the image after creating it.
Illustration: example Document Editor
Solution
Design a surrogate, or proxy, object that:
instantiates the real object the first time
the client makes a request of the proxy,
remembers the identity of this real object,
and forwards the instigating request to this
real object.
The Bridge pattern decouples an abstraction from its implementation, so that the
two can vary independently. A household switch controlling lights, ceiling fans,
etc. is an example of the Bridge. The purpose of the switch is to turn a device on
or off. The actual switch can be implemented as a pull chain, a simple two
position switch, or a variety of dimmer switches
Intent
◦ Decouple an abstraction from its
implementation so that the two can vary
independently
Also Known As
Handle/Body
Problem
"Hardening of the software arteries" has occurred by using subclassing of
an abstract base class to provide alternative implementations. This locks in
compile-time binding between interface and implementation. The
abstraction and implementation cannot be independently extended or
composed.
Illustration: Problem
Consider the implementation of a portable Window
abstraction in a user interface toolkit. This
abstraction should enable us to write applications
that work on both x window system and IBM’s PM
.
Bridge patterns address these problems by putting the Window abstraction and its
implementation in separate class hierarchies.
There is One class hierarchy for window interfaces(Window, IconWindow,
TransientWindow) and separate hierarchy for platform-specific window
implementations, with WindowImp as its root.
All operation on Window subclasses are implemented in terms of abstract
operations from the WindowImp interface. This decouples the Window
abstractions form the various platfom-specific implementations. We refer to the
relationship between window and WindowImp as a Bridge.
Solution:
Decompose the component's interface and implementation into
orthogonal class hierarchies.
The interface class contains a pointer to the abstract
implementation class. This pointer is initialized with an instance of
a concrete implementation class, but all subsequent interaction
from the interface class to the implementation class is limited to
the abstraction maintained in the implementation base class.
The interface object is the "handle" known and used by the client;
while the implementation object, or "body", is safely encapsulated
to ensure that it may continue to evolve, or be entirely replaced
(or shared at run-time
Applicability
Use the Bridge pattern when:
• you want run-time binding of the implementation and
want to avoid permanent binding between an abstract
and its implementation.
• Both the abstraction and their implementations should
be extensible by subclassing.
• Change in the implementation of an abstraction should
have no impact on client; ie their code should not be
recomplied
• you want to share an implementation among multiple
objects,
• you have a proliferation of classes resulting from a
coupled interface and numerous implementations
• you need to map orthogonal class hierarchies.
Structure
Collaborations
Abstraction forwards client request to its
implementers object.
Consequences include:
• decoupling the object's interface & implementation
Problem
Sometimes we want to add responsibilities to individual objects
(aTextView ) not to an entire class. ( i.e. how to give control to client to
decorate the component with a border / a scroll bar )
Illustration aTextView
Attached additional
responsibility to an object Attached additional
dynamically. Decorator responsibility to an
provide a flexible object dynamically.
alternative to sub classing Decorator provide a
for extending functionality flexible alternative to
sub classing for
extending functionality
aScrollDecorator
Illustration
Draw
Solution
One way to add responsibilities is with
inheritance.Inheriting a border with another class
puts a border along very subclass instance
( inflexible solution ).
A more flexible approach is to enclose the
component in another objects that adds the
border. The enclosing object is called Decorator.
The Decorator confirm to interface of the
component it decorate so that its presence is
transparent to its components clients.
Applicability
Use Decorator
To add responsibilities to individual objects dynamically
and transparently, that is, without affecting other objects.
Collaborations
Decorator forwards requests to its Component
object. It may optionally perform additional
operations before and after forwarding the request.
Consequences
More flexibility than static inheritance:
◦ to add or delete responsibilities to objects at runtime simply by
attaching and detaching them.
◦ Providing different decorator classes for a specific component class
lets you mix and match responsibilities.
The Facade defines a unified, higher level interface to a subsystem, that makes
it easier to use.
Consumers encounter a Facade when ordering from a catalog. The consumer
calls one number and speaks with a customer service representative. The
customer service representative acts as a Facade, providing an interface to the
order fulfillment department, the billing department, and the shipping
department.
Facade
Provide unified interface to interfaces within a subsystem
Shield clients from subsystem components
Promote weak coupling between client and subsystem components
Client
Facade
Intent
Provide a unified interface to a set of interfaces
in a subsystem. Facade defines a higher-level
interface that makes the subsystem easier to
use.
Problem:
A segment of the client community needs a simplified interface to the
overall functionality of a complex subsystem
Illustration: Problem
Consider for example a programming envirnoment
that gives application access to its compiler
subsystem. This subsystem contains classes such
as Scanner, Parser,ProgramNode, ByteCodeStream,
and ProgramNodeBuilder that implements te
Compiler. Some Specialized applications might
need to access these classes directly.
But most clients of a compiler generally don’t
care about details like parsing and byte code
generation; they merely want to complier
subsysem only complicate their task.
Illustration : solution
Facade
Compiler class in the above can act as a façade, which will provide higher-
level interface that can shield clients form these classes, the compiler
subsystem also includes a Compiler class. This class defines a unified
interface to the compiler’s functionality.
It offers client a single, simple interface to the compiler subsystem. It glues
together the classes that implement compiler functionality without hiding them
completely.
[Compiler façade makes easier for most programmers without hiding the
lower-level functionality form the few that need it]
Illustration: solution
Applicability
Use the Façade pattern when
You want to provide a simple interface to a
complex subsystem.
◦ To tackle the complexity of subsystem as they evolve
◦ Most patterns, when applied, result in more and
smaller classes. This makes the subsystem more
reusable and easier to customize, but it also becomes
harder to use for clients that don’t need to customize
it.
◦ A façade can provide a simple default view of the
subsystem that is good enough for most client. Only
clients needing more customizability will need to look
beyond the façade
Applicability……...
Use the Façade pattern when
Facade
Subsystem classes
Collaborations
The Flyweight uses sharing to support large numbers of objects efficiently. The
public switched telephone network is an example of a Flyweight. There are
several resources such as dial tone generators, ringing generators, and digit
receivers that must be shared between all subscribers. A subscriber is unaware
of how many resources are in the pool when he or she lifts the hand set to
make a call. All that matters to subscribers is that dial tone is provided, digits
are received, and the call is completed.
Intent
Use sharing to support large numbers of
fine-grained objects efficiently
Problem
• Designing objects down to the lowest levels
of system "granularity" provides optimal
flexibility, but can be unacceptably expensive
in terms of performance and memory usage.
Illustration
Designing objects down
to the lowest levels of
system "granularity”
provides optimal flexibility
, but can be unacceptably
expensive in terms Row objects
a p p a r e n t
Row objects
Character object
Column object
Illustration
Flyweight is a shared object that can be used in multiple context
Simultaneously
column
a b c d e f
g h i j k l
Flyweight pool
Applicability
Apply in all of the following are true:
quantity of objects
Most object state can be made extrinsic
identity
Structure
Collaborations
State that a fly-weight needs to function must
stored.
Thank You!