Decorator Pattern.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Design Patterns.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
S.Ducasse Stéphane Ducasse 1 Adapter.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Modern Programming Tools And Techniques-I
Design Patterns: MORE Examples
Web Design & Development Lecture 9
Design Patterns: Brief Examples
Unit II-Chapter No. : 5- design Patterns
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Chapter 11 Object-Oriented Design
ABSTRACT FACTORY.
More Design Patterns 1.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Design Pattern Detection
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
State Design Pattern 1.
Object Oriented Design Patterns - Structural Patterns
Decorator Pattern Richard Gesick.
Behavioral Design Pattern
METHOD OVERRIDING in JAVA
UNIT-III Structural Design Patterns
Factory Pattern.
Polymorphism Pattern.
Software Design Lecture : 12.
Interpreter Pattern.
BRIDGE PATTERN.
More About Inheritance & Interfaces
Structural Patterns: Adapter and Bridge
Decorator.
Chapter 14 Abstract Classes and Interfaces
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

Decorator Pattern

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 Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality. Also Known As Wrapper

Motivation Sometimes we want to add responsibilities to individual objects, not to an entire class. A graphical user interface toolkit, for example, should let you add properties like borders or behaviors like scrolling to any user interface component. One way to add responsibilities is with inheritance. Inheriting a border from another class puts a border around every subclass instance. This is inflexible, however, because the choice of border is made statically. A client can't control how and when to decorate the component with a border.

Applicability Use Decorator To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. for responsibilities that can be withdrawn. when extension by sub classing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for sub classing.

Participants Component (VisualComponent) o defines the interface for objects that can have responsibilities added to them dynamically. ConcreteComponent (TextView) o defines an object to which additional responsibilities can be attached. Decorator o maintains a reference to a Component object and defines an interface that conforms to Component's interface. ConcreteDecorator (BorderDecorator, ScrollDecorator) o adds responsibilities to the component.

Collaborations Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request. Consequences The Decorator pattern has the following benefits . More flexibility than static inheritance. Avoids feature-laden classes high up in the hierarchy. A decorator and its component aren't identical.

Implementation We're going to create a Shape interface and concrete classes implementing the Shapeinterface. We will then create an abstract decorator class ShapeDecorator implementing theShape interface and having Shape object as its instance variable. RedShapeDecorator is concrete class implementing ShapeDecorator. DecoratorPatternDemo, our demo class will use RedShapeDecorator to decorate Shapeobjects.

Step 1 Create an interface. Shape.java public interface Shape { void draw(); } Step 2 Create concrete classes implementing the same interface. Rectangle.java public class Rectangle implements Shape @Override public void draw() System.out.println("Shape: Rectangle");

Circle.java public class Circle implements Shape { @Override public void draw() System.out.println("Shape: Circle"); } Step 3 Create abstract decorator class implementing the Shape interface. ShapeDecorator.java public abstract class ShapeDecorator implements Shape

protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape) { this.decoratedShape = decoratedShape; } public void draw() decoratedShape.draw();

Step 4 Create concrete decorator class extending the ShapeDecorator class. RedShapeDecorator.java public class RedShapeDecorator extends ShapeDecorator { public RedShapeDecorator(Shape decoratedShape) super(decoratedShape); } @Override public void draw() decoratedShape.draw(); setRedBorder(decoratedShape); private void setRedBorder(Shape decoratedShape) System.out.println("Border Color: Red");

Step 5 Use the RedShapeDecorator to decorate Shape objects. DecoratorPatternDemo.java public class DecoratorPatternDemo { public static void main(String[] args) Shape circle = new Circle(); Shape redCircle = new RedShapeDecorator(new Circle()); Shape redRectangle = new RedShapeDecorator(new Rectangle()); System.out.println("Circle with normal border"); circle.draw(); System.out.println("\nCircle of red border"); redCircle.draw(); System.out.println("\nRectangle of red border"); redRectangle.draw(); }

Step 6 Verify the output. Circle with normal border Shape: Circle Circle of red border Border Color: Red Rectangle of red border