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.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Chapter 3: The Decorator Pattern
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
Jan 29, Ron McFadyen1 UML Class Diagram Examples Based on well-known patterns Exhibit ways of providing dynamic structures and behaviour.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Abstract Classes and Interfaces
Inheritance and Polymorphism CS351 – Programming Paradigms.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
1 Pattern-Oriented Design by Rick Mercer based on the GoF book and Design Patterns Explained A New Perspective on Object-Oriented Design Alan Shalloway,
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Design Patterns.
CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Provides an interface for creating families of related or dependent objects without specifying.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Huang-Ming Huang, Shih-Ling Chen, Sajeeva Pallemulle.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Modern Programming Tools And Techniques-I
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Factory Patterns 1.
Introduction to Design Patterns
CS 350 – Software Design The Strategy Pattern – Chapter 9
Sequences and Iterators
Object Oriented Analysis and Design
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Object Oriented Design Patterns - Structural Patterns
Decorator Pattern Richard Gesick.
CS 350 – Software Design A Standard Object-Oriented Solution – Chapter 4 Before studying design patterns, many programmers solve a problem by starting.
Pattern-Oriented Design
DESIGN PATTERNS : State Pattern
CS 350 – Software Design Singleton – Chapter 21
7. Decorator SE2811 Software Component Design
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
OOP Aga private institute for computer science 5th grade
Decorator Pattern.
Abstract Classes and Interfaces
Presentation transcript:

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. The original solution showed how we could have a Sales order us the CalcTax object to compute the tax and a SalesTicket object to print the ticket.

CS 350 – Software Design The Decorator Pattern – Chapter 17 However what happens if the sales ticket gets more complicated? We could add a header to the ticket. For that matter, we can add a footer to the ticket as well. A simple solution is to add flags to SalesTicket to determine whether or not I should print headers or footers. This is fine for a few simple options. If I have a option of printing one of many headers/footers at a time, I could implement a strategy to determine which header/footer to print. However, what if I have multiple headers and footers to print? Instead of having a control method to handle the added functionality, chain the functionality together in the order it is required. Separate the dynamic building of the chain of functionality from the client that uses it.

CS 350 – Software Design The Decorator Pattern – Chapter 17 Sales Order using SalesTicket with different Options

CS 350 – Software Design The Decorator Pattern – Chapter 17 The Decorator Pattern: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

CS 350 – Software Design The Decorator Pattern – Chapter 17 The class diagram below implies the previously shown chain of objects. Each chain starts with a component (either a Concrete Component or a Decorator) Each Decorator is followed by another Decorator or by the original Concrete Component. The Concrete Component always ends the chain. Note that the Decorator and the Concrete Component must inherit from the same base class.

CS 350 – Software Design The Decorator Pattern – Chapter 17 Decorator Pattern Applied to E-Commerce Case Study The concrete decorators are the headers and footers.

CS 350 – Software Design The Decorator Pattern – Chapter 17 Decorator Code for E-Commerce Example //The client uses an abstract factory to create the objects needed public class Client { public static void main (String[] args) { public static void main (String[] args) { Factory myFactory; Factory myFactory; myFactory = new Factory(); myFactory = new Factory(); Component myComponent = myFactory.getComponent(); Component myComponent = myFactory.getComponent(); }} //The Component is the base class used to establish a common interface as well as allowing //both the Sales ticket and TicketDecorator to behave polymorphically. abstract public class Component { abstract public void prtTicket(); abstract public void prtTicket();} abstract public class TicketDecorator extends Component { private Component myTrailer; private Component myTrailer; public TicketDecorator (Component myComponent) { public TicketDecorator (Component myComponent) { myTrailer=myComponent; myTrailer=myComponent;} public void callTrailer () { public void callTrailer () { if (myTrailer != null) myTrailer.prtTicket(); if (myTrailer != null) myTrailer.prtTicket(); }}

CS 350 – Software Design The Decorator Pattern – Chapter 17 Decorator Code for E-Commerce Example public class Header1 extends TicketDecorator { public Header1 (Component myComponent) { public Header1 (Component myComponent) { super (myComponent); super (myComponent); } public void prtTicket () { public void prtTicket () { //place printing header 1 code here //place printing header 1 code here super.callTrailer(); super.callTrailer(); }} public class Header2 extends TicketDecorator { public Header2 (Component myComponent) { public Header2 (Component myComponent) { super (myComponent); super (myComponent); } public void prtTicket () { public void prtTicket () { //place printing header 2 code here //place printing header 2 code here super.callTrailer(); super.callTrailer(); }}

CS 350 – Software Design The Decorator Pattern – Chapter 17 Decorator Code for E-Commerce Example public class Footer1 extends TicketDecorator { public Footer1 (Component myComponent) { public Footer1 (Component myComponent) { super (myComponent); super (myComponent); } public void prtTicket() { public void prtTicket() { super.callTrrailer(); super.callTrrailer(); //place printing footer 1 code here //place printing footer 1 code here }} public class Footer2 extends TicketDecorator { public Footer2 (Component myComponent) { public Footer2 (Component myComponent) { super (myComponent); super (myComponent); } public void prtTicket() { public void prtTicket() { super.callTrrailer(); super.callTrrailer(); //place printing footer 2 code here //place printing footer 2 code here }}

CS 350 – Software Design The Decorator Pattern – Chapter 17 Decorator Code for E-Commerce Example public class Factory { public Compnent getComponent () { public Compnent getComponent () { Component myComponent; Component myComponent; myComponent = new SalesTicket(); myComponent = new SalesTicket(); myComponent = new Footer1(myComponent); myComponent = new Footer1(myComponent); myCompoent = new Header1(myComponent); myCompoent = new Header1(myComponent); return myComponent; return myComponent; }}

CS 350 – Software Design The Decorator Pattern – Chapter 17 If I want the sales ticket to look like: Header 1 Sales Ticket Footer 1 myFactory.getComponent returns: return (new Header1 (new Footer1 (new SalesTicket())));

CS 350 – Software Design The Decorator Pattern – Chapter 17 If I want the sales ticket to look like: Header 1 Header 2 Sales Ticket Footer 1 myFactory.getComponent returns: return (new Header1 (new Header2 (new Footer1 (new SalesTicket()))));

CS 350 – Software Design The Decorator Pattern – Chapter 17 If I want the sales ticket to look like: Header 1 Header 2 Sales Ticket Footer 1 myFactory.getComponent returns: return (new Header1 (new Header2 (new Footer1 (new SalesTicket()))));

CS 350 – Software Design The Decorator Pattern – Chapter 17 Intent: Attach additional responsibilities to an object dynamically. Problem: The object that you want to use does not have the basic functions you require. However, you need to add additional functionality to the object that occurs before or after the object’s base functionality. Solution: Allow extending functionality of the object without resorting to subclassing by building a linked list of objects derived from the same base class. Implementation: Create an abstract class that represents both the original class and the new functions to be added to the class. In the decorators, place the new function calls before or after the trailing calls to get the correct order.

CS 350 – Software Design The Decorator Pattern – Chapter 17 Generic Structure of the Decorator Pattern