You are on page 1of 20

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Structural Pattern Decorator


Introduction & Overview

CT070-3-3 - Design Patterns Decorator Slide 1 (of 12)


Learning Outcomes

At the end of this session, you should be


able to;

Describe benefits of Decorator pattern


Recognise and apply Decorator pattern
given a scenario

CT070-3-3 - Design Patterns Decorator Slide 2 (of 12)


Design Pattern -Decorator

Name: Decorator Pattern

Problem:
You want to add behavior or state to individual objects
at run-time. Inheritance is not feasible because it is
static and applies to an entire class.
Motivation:
Sometimes we want to add responsibilities to individual
objects, not to an entire class.

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Design Pattern -Bridge

Context:
We use inheritance or composition to extend the
behavior of an object but this is done at compile
time and its applicable to all the instances of the
class. We cant add any new functionality of remove
any existing behavior at runtime this is when
Decorator pattern comes into picture.

CT070-3-3 - Design Patterns Decorator


Description Decorator Pattern

Applicability:
Decorator pattern should be used when you
want to:
- Add responsibilities to individual objects
dynamically and transparently, that is, without
affecting other objects.

- When extension by subclass is impractical

CT070-3-3 - Design Patterns Decorator Slide 5 (of 12)


Definition Decorator Pattern

Add additional responsibilities to an object


dynamically. Decorators provide a flexible alternative
to sub classing for extending functionality.

CT070-3-3 - Design Patterns Decorator Slide 6 (of 12)


Consequences of Decorator

More flexibility than static inheritance.


The Decorator pattern provides a more flexible way to
add responsibilities to objects than can be had with static
(multiple) inheritance. With decorators, responsibilities
can be added and removed at run-time simply by
attaching and detaching them.
In contrast, inheritance requires creating a new class for
each additional responsibility. This gives rise to many
classes and increases the complexity of a system.
Furthermore, providing different Decorator classes for a
specific Component class lets you mix and match
responsibilities.
Decorators also make it easy to add a property twice.
For example, to give a TextView a double border, simply
attach two BorderDecorators. Inheriting from a Border
class twice is error-prone at best.

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Decorator vs. Adapter

There is an essential similarity among these


classes that you may have recognized.
Adapters also seem to decorate an existing
class However, their function is to change the
interface of one or more classes to one that is
more convenient for a particular program.
Decorators add methods to particular instances
of classes, rather than to all of them.

CT070-3-3 - Design Patterns Decorator Slide 9 (of 12)


Decorator Pattern Case study

Design and implement Basic car model


and support provisions for manufacturing
sports car and luxury car.

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Decorator Pattern Case
study(Contd)
But if we want to get a car at runtime that
has both the features of sports car and
luxury car, then the implementation gets
complex and if further more we want to
specify which features should be added
first, it gets even more complex.
Now imagine if we have ten different kind
of cars, the implementation logic using
inheritance and composition will be
impossible to manage
CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)
Class design-Decorator

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Sample Code
Component Interface:
The interface or abstract class defining the
methods that will be implemented. In our
case Car will be the component interface.

public interface Car {


public void assemble();
}

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Sample Code(Contd)

Component Implementation:
The basic implementation of the component
interface. We can have BasicCar class as our
component implementation.
public class BasicCar implements Car {
@Override
public void assemble() {
System.out.print("Basic Car."); }
}
CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)
Sample Code(Contd)
Decorator Decorator class implements the
component interface and it has a HAS-A relationship with
the component interface. The component variable should
be accessible to the child decorator classes, so we will
make this variable protected.
public class CarDecorator implements Car
{ protected Car car;
public CarDecorator(Car c)
{ this.car=c; }
@Override
public void assemble()
{ this.car.assemble(); } }
CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)
Sample Code(Contd)

Concrete Decorators Extending the base


decorator functionality and modifying the component
behavior accordingly. We can have concrete decorator
classes as LuxuryCar and SportsCar.
public class SportsCar extends CarDecorator {
public SportsCar(Car c)
{super(c);}
@Override
public void assemble(){
super.assemble();
system.out.print(" Adding features of Sports Car."); }}

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Sample Code(Contd)
public class LuxuryCar extends CarDecorator
{ public LuxuryCar(Car c)
{ super(c); }
@Override
public void assemble()
{ super.assemble();
System.out.print(" Adding features of Luxury Car.");
}
}

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Test-Decorator Pattern
public class DecoratorPatternTest
{ public static void main(String[] args)
{ Car sportsCar = new SportsCar(new BasicCar());
sportsCar.assemble();
System.out.println("\n*****");
Car sportsLuxuryCar = new SportsCar(new
LuxuryCar(new BasicCar()));
sportsLuxuryCar.assemble();
}}

CT070-3-3 - Design Patterns Decorator Slide 1 (of 18)


Conclusion

Decorator design pattern is helpful in providing


runtime modification abilities and hence more
flexible. Its easy to maintain and extend when
the number of choices are more.
The disadvantage of decorator design pattern is
that it uses a lot of similar kind of objects
(decorators).
Decorator pattern is used a lot in Java
IO classes, such as File Reader,
BufferedReader etc.

CT070-3-3 - Design Patterns Decorator Slide 10 (of 12)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Decorator Slide 11 (of 12)


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Decorator Slide 12 (of 12)

You might also like