Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Structural Patterns: Decorator Facade

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

1

Structural Patterns
Decorator
Facade

Structural patterns
2

Describe ways to assemble objects to realize


new functionality

Added flexibility inherent in object composition due


to ability to change composition at run-time
not possible with static class composition

Structural Patterns
3

Adapter:
Translator adapts a server interface for a client
Bridge:
Abstraction for binding one of many implementations
Composite:
Structure for building recursive aggregations
Decorator:
Decorator extends an object transparently
Facade:
Simplifies the interface for a subsystem
Flyweight:
Many fine-grained objects shared efficiently.
Proxy:
One object approximates another

Decorator Pattern

Decorator pattern
5

Decorator pattern allows a user to add new


functionality to an existing object without
altering its structure.
This type of design pattern comes under
structural pattern as this pattern acts as a
wrapper to existing class.
This pattern creates a decorator class
which wraps the original class and
provides additional functionality keeping
class methods signature intact.

Intent
6

The intent of this pattern is to add


additional responsibilities dynamically to
an object.

Structure
7

Participants
8

Component - Interface for objects that can


have responsibilities added to them dynamically.
ConcreteComponent - Defines an object to
which additional responsibilities can be added.
Decorator - Maintains a reference to a
Component object and defines an interface that
conforms to Component's interface.
Concrete Decorators - Concrete Decorators
extend the functionality of the component by
adding state or adding behavior.

Consequences
9

Good:

Decoration is more convenient for adding


functionalities to objects instead of entire
classes at runtime.
With decoration it is also possible to remove
the added functionalities dynamically.

Bad:

Decoration adds functionality to objects at


runtime which would make debugging
system functionality harder.

Example
10

we will decorate a shape with some color without alter shape class.
Sample cod
e

Example
create a Shape interface and concrete

11

classes implementing the Shape


interface.
We will then create an abstract
decorator class ShapeDecorator
implementing the Shape interface and
having Shape object as its instance
variable.
RedShapeDecorator is concrete class
implementing ShapeDecorator.
DecoratorPatternDemo, our demo class
will use RedShapeDecorator to decorate
Shape objects

12

Faade Pattern

Faade
13

Intent
Provide a unified interface to a set of interfaces in a subsystem.
Faade defines a higher-level interface that makes the subsystem easier to
use.

Applicability

Provides a simple interface to a complex subsystem.


Decouples the details of a subsystem from clients and other
subsystems.
Provides a layered approach to subsystems.

Intent
14

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.
Wrap a complicated subsystem with a
simpler interface.

Faade
15

Participants

Faade

Knows which classes are responsible for each request.

Delegates client requests to appropriate objects.

Subsystem classes

Implement subsystem functionality.

Handle work assigned by the Faade object.

Have no knowledge of the faade.

Collaborations

Clients communicate with the subsystem sending requests to the Faade.

Reduces the number of classes the client deals with.


Simplifies the subsystem.

Clients do not have to access subsystem objects directly.

Facade pattern
16

Facade pattern hides the complexities of the


system and provides an interface to the client
using which the client can access the system.
This type of design pattern comes under
structural pattern as this pattern adds an
interface to existing system to hide its
complexities.
This pattern involves a single class which
provides simplified methods required by client
and delegates calls to methods of existing
system classes.

Implementation
17

We are going to create a Shape interface


and concrete classes implementing the
Shape interface. A facade class
ShapeMaker is defined as a next step.
ShapeMaker class uses the concrete
classes to delegate user calls to these
classes. FacadePatternDemo, our demo
class, will use ShapeMaker class to show
the results.

Example
18

Sample Co
de

19

Facade (Non software


example)
Provide a unified
interface to a set
of interfaces in a
subsystem.

You might also like