DOC PREVIEW
Yale CPSC 427 - Object-Oriented Programming
School name Yale University
Pages 47

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable designExample: Duck GameInitial DesignDesign Change: add fly()Slide 5Anticipating ChangesHandling Varying BehaviorsDesignProgramming to implementation vs interface/supertypeImplementationSlide 11The Strategy PatternSlide 13Summary: Design PrinciplesExample: 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 28Slide 29Potential ChangesDesign ideaDesign approach 1Decorator designDecoration Delegation ProcessDecorator Class Model Slide 36Decoration 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 23bNovember 29, 2011Example: Duck Game•A startup produces a duck-pond simulation game•The game shows a large variety of duck species swimming and making quacking soundsInitial DesignMillardDuckdisplay() { looks like a mallard}RedheadDuckdisplay() { looks like a redhat}Other types of ducksDuckquack()swim()display() = 0// Other duck-like methodDesign Change: add fly()Duckquack()swim()display() = 0fly()// Other duck-like methodMillardDuckdisplay() { looks like a mallard}RedheadDuckdisplay() { looks like a redhat}Other types of ducksProblem•Generalization may lead tounintended behaviors: a rubber duck is flying and quacksDuckquack()swim()display() = 0// Other duck-like methodRubberDuckdisplay() {looks like a rubber duck}quack() { // sqeak }fly() { // cannot fly }MillardDuckdisplay() { looks like a mallard}Anticipating Changes•Identify the aspects of your application that may vary–What may change?•Anticipate that –new types of ducks may appear and –behaviors (quack, swimming, and flying) may change, even change at run time (swirl flying, circular flying, …)Handling Varying Behaviors•Solution: take what varies and “encapsulate” it–Since fly() and quack() vary across ducks, separate these behaviors from the Duck class and create a new set of classes to represent each behaviorsuper class of all ducksVarying and run-time changeable behaviorsDesign•Each duck object has a fly behaviorFlyWithWingsfly() { //}FlyNoWayfly() { // cannot fly}<<interface>>FlyBehaviorfly()Programming to implementation vs. interface/supertype•Programming to an implementation–Dog d = new Dog();–d.bark();•Programming to an interface/supertype–Animal a = new Dog();–a.makeSound();ImplementationFlyWithWingsfly() { //}FlyNoWayfly() { // cannot fly}<<interface>>FlyBehaviorfly()MallardDuckdisplay() { looks like a mallard}RedheadDuckdisplay() { looks like a redhat}Duckquack()swim()display() = 0// Other duck-like methodExercise•Add rocket-powered flying?The Strategy Pattern•Defines a set of algorithms, encapsulates each one, and makes them interchangeable by defining a common interfaceExerciseSummary: Design Principles•Identify the aspects of your application that vary and separate them from what stay the same•Program to an interface not implementation•Favor composition over inheritanceExample: 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


View Full Document

Yale CPSC 427 - Object-Oriented Programming

Download Object-Oriented Programming
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 Object-Oriented Programming 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 Object-Oriented Programming 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?