Yale CPSC 427 - Design Patterns for Flexible and Reusable design
School name Yale University
Pages 34

Unformatted text preview:

CS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable designExample: KitchenViewer InterfaceKitchenViewer ExampleSelecting Antique StyleKitchenViewer Using Standard InheritanceThe Abstract Factory IdeaAbstract Factory Design Pattern Applied to KitchenViewerAbstract Factory Design PatternConcrete and Abstract LayersAbstract Factory Application Sequence DiagramPotential use of this Design Pattern?ReferencesExample: Starbuzz CoffeeProblemSlide 15Slide 16Potential ChangesDesign ideaDesign approach 1Decorator designDecoration Delegation ProcessDecorator Class Model Slide 23Decoration FeaturesExerciseSome Common Design PatternsExample: Weather-O-RamaWeather-O-RamaWeather-O-Rama InterfaceFirst ImplementationObserver PatternObserver Design PatternHow does Observer apply these design principles?DiscussionCS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable designMichael J. Fischer(from slides by Y. Richard Yang)Lecture 21November 18, 2010Example: KitchenViewer InterfaceWallcabinetCounterFloorcabinetModern Classic Antique Arts & Craftsmenudisplay areastylesAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.KitchenViewer Example Modern Classic Antique Arts & CraftsWall cabinetsFloor cabinetsCountertopSelecting Antique Style Modern Classic Antique Arts & CraftsKitchenViewer Using Standard InheritanceKitchenClientrenderKitchen()FloorCabinet ModernWallCabinetModernFloorCabinet AntiqueFloorCabinetAntiqueWallCabinetWallCabinet Aspect of the system thatmay change/vary?: Reference directionAntiqueKStylegetWallCabinet()getFloorCabinet()The Abstract Factory IdeaKitchenStylegetWallCabinet()getFloorCabinet()ModernKStylegetWallCabinet()getFloorCabinet()WallCabinet FloorCabinet AntiqueWallCabinet AntiqueFloorCabinetFloorCabinet getFloorCabinet() { return new AntiqueFloorCabinet(); }……FloorCabinet getFloorCabinet() { return new ModernFloorCabinet(); }Abstract Factory Design Pattern Applied to KitchenViewerKitchenStylegetWallCabinet()getFloorCabinet()KitchengetWallCabinet()getFloorcabinet()ClientrenderKitchen( KitchenStyle )ModernKStylegetWallCabinet()getFloorCabinet()ModernKStylegetWallCabinet()getFloorCabinet()AntiqueKStylegetWallCabinet()getFloorCabinet()WallCabinet FloorCabinet ModernWallCabinetModernFloorCabinetAntiqueWallCabinetAntiqueFloorCabinetAbstract Factory Design PatternStylegetComponentA()getComponentB()ClientdoOperation( Style myStyle )Style1getComponentA()getComponentB()Style2getComponentA()getComponentB()ComponentA ComponentBStyle1ComponentAStyle1ComponentBStyle2ComponentAStyle2ComponentBCollectionConcrete and Abstract LayersKitchenStyleKitchenClientModernKStyleAntiqueKStyleWallCabinet FloorCabinet ModernWallCabinetModernFloorCabinetAntiqueWallCabinetAntiqueFloorCabinetAbstract levelConcrete levelgetWallCabinet()Abstract Factory Application Sequence DiagrammyStyle:KitchenStyleClientmyStyle:ModernKStylemyStyle:AntiqueKStylerenderKitchen( myStyle )wallCabinet1:ModernWallCabinetwallCabinet1:AntiqueWallCabinetModernWallCabinet()getWallCabinet()AntiqueWallCabinet()myStyle.getWallCabinet()-- IF myStyle BELONGS TO ModernKStyle ---- IF myStyle BELONGS TO AntiqueKStyle --Potential use of this Design Pattern?StylegetComponentA()getComponentB()ClientdoOperation( Style myStyle )Style1getComponentA()getComponentB()Style2getComponentA()getComponentB()ComponentA ComponentBStyle1ComponentAStyle1ComponentBStyle2ComponentAStyle2ComponentBCollectionReferences•Design Patterns•Headfirst Design Patterns•Software DesignExample: Starbuzz Coffee•The coffee shop offers a variety of beveragesHouseBlendcost()BeveragedescriptiongetDescription();cost();// Other methodsDarkRoastcost()Decafcost()Espressocost()Problem•A customer may also ask for condiments–steamed milk–soy–mocha (otherwise known as chocolate)–whipped milk•Starbuzz charges a bit for each of theseHouseBlendcost()BeveragedescriptionmilksoymochawhipgetDescription();cost();hasMilk(); setMilk();hasSoy(); setSoy();hasMocha(); setMocha();hasWhip(); setWhip();// Other methodsDarkRoastcost()Decafcost()Espressocost()Aspect of the system thatmay change/vary?Attempt 1Potential Changes•Potential changes:–Price change to condiments–New condiments–Double moca–…Design idea•Basic idea: extension at run time, not compile time•Definition: The Decorator pattern attaches additional features to an object dynamically. It provides a flexible alternative to subclassing for extending functionalityDesign approach 1•Each beverage contains a dynamic list of condiments•Example–Take a DarkRoast object–Decorate it with a Mocha object–Decorate it with a Whip objectUML class model?Decorator design•Example–Take a DarkRoast object–Decorate it with a Mocha object–Decorate it with a Whip object–Call the cost() method and rely on delegation to add on the condiment cost•Decorator adds its own behavior before or after calling the decorated objectDecoration Delegation ProcessDecorator Class Model ClientobjDecoratedDecoratordoAction()1ConcreteComponentdoAction()ComponentdoAction()void doAction(){ ….. // do actions special to this decoration objDecorated.doAction(); // pass along} Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.ConcreteDecoratorAdoAction()ConcreteDecoratorBdoAction()Sequence Diagram for Decorator:ClientdoAction()decoration1:DecorationdoAction()Decoration1.objDecorated:Decoration:ComponentdoAction()Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.Decoration Features•Decorators have the same supertype as the objects they decorate•You can use one or more decorators to wrap an object–Thus, you can pass decorated object in place of original (wrapped) object•The decorator adds its own behavior either before or after delegating to the object it decorates to•Objects can be decorated at any time, including run-time, with as many decorators as possibleExercise•Suppose we allow different sizes for the beverages–Tall (small)–Grande (medium)–Venti (large)Some Common Design PatternsExample: Weather-O-RamaWeather-O-RamaWeather-O-Rama InterfaceWeatherDatagetTemperature();getHumidity();getPressure();measurementsChanged();setMeasurements(); // other methodsThis method gets called wheneverthe weather measurements have been


View Full Document

Yale CPSC 427 - Design Patterns for Flexible and Reusable design

Download Design Patterns for Flexible and Reusable design
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns for Flexible and Reusable design and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns for Flexible and Reusable design 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?