DOC PREVIEW
CU-Boulder CSCI 5448 - STRATEGY, BRIDGE & FACTORY

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 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 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011STRATEGY, BRIDGE & FACTORYCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 9 — 02/08/20111Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Goals of the LectureCover the material in Chapters 9, 10 & 11 of our textbookThe Strategy PatternThe Bridge PatternThe Abstract Factory PatternBut first, a quiz…2Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Question 1What is polymorphism?3Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Question 2 and 2.5What is the problem with using inheritance for specialization of implementation?How should we use inheritance instead?4Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Question 3, 3.5 and 3.9What does “encapsulate what varies” mean?How is it used in the adaptor pattern?How is it used in the strategy pattern?5Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Question 4When would I choose to use an interface over an abstract class?6Tuesday, February 8, 2011© Kenneth M. Anderson, 2011The Strategy PatternWe already covered this pattern in Lecture 6IntentionLets you use different algorithms depending on contextOr lets you vary behavior across subclasses of some abstract class7Tuesday, February 8, 2011© Kenneth M. Anderson, 2011The Structure of the Strategy Pattern8performOperation()setAlgorithm(a: Algorithm)Clientoperation()AlgorithmConcreteAlgorithm1strategyConcreteAlgorithmN...strategy.operation()Tuesday, February 8, 2011© Kenneth M. Anderson, 2011SimUDuck’s use of Strategy9swim()display()setFlyBehavior()setQuackBehavior()performFly()performQuack()Duckdisplay()MallardDuckdisplay()RedheadDuckdisplay()RubberDuckdisplay()DecoyDuckfly()FlyBehaviorquack()QuackBehaviorFlyWithWings CantFly QuackSilence SqueakflyBehaviorquackBehaviorDuck’s Fly and Quack behaviors are decoupled from DuckTuesday, February 8, 2011© Kenneth M. Anderson, 2011Yo u r Tu r n10Work in a group and create a class diagram that shows a design that meets these requirements and uses StrategyA bank account has a balance and a set of transactionsA transaction records the date, the vendor, the amount and a categoryA client can add transactions, ask for a List of transactions and ask that the List be sorted in a certain wayThe list can be sorted by date, vendor, amount or categoryTuesday, February 8, 2011© Kenneth M. Anderson, 2011My Attempt11add(Transaction)List<Transaction> getTransactions()sortTransactions()setSortBehavior(SortBehavior)Bank AccountDate dateString vendorBIgDecimal amountString categoryTransactionsort(List<Transaction>)SortBehaviorsort(List<Transaction>)ByDatesort(List<Transaction>)ByVendorsort(List<Transaction>)ByCategorysort(List<Transaction>)ByAmountClientTuesday, February 8, 2011© Kenneth M. Anderson, 2011The Bridge Pattern12The Gang of Four says the intent of the pattern is to “decouple an abstraction from its implementation so that the two can vary independently”What it doesn’t meanAllow a C++ header file to be implemented in multiple waysWhat it does meanAllows a set of abstract objects to implement their operations in a number of ways in a scalable fashion Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Bottom-UpThe book presents an example that derives the bridge patternLet a set of shapes draw themselves using different drawing librariesThink of the libraries as items such as Monitor, Printer, OffScreenBuffer, etc.Imagine a world where each of these might have slightly different methods and method signatures13Tuesday, February 8, 2011© Kenneth M. Anderson, 2011Examples of Drawing LibraryThe drawing library for Monitor has these methodsdraw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)The drawing library for Printer has these methodsdrawline(x1, x2, y1, y2)drawcircle(x, y, r)14draw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)Monitordrawline(x1, x2, y1, y2)drawcircle(x, y, r)PrinterTuesday, February 8, 2011© Kenneth M. Anderson, 2011Examples of Shape15+draw()Shape+draw()Rectangle+draw()CircleWe want to be able to create collections of rectangles and circles and then tell the collection to draw itself and have it work regardless of the mediumTuesday, February 8, 2011© Kenneth M. Anderson, 201116+draw()Shape+draw()#drawLine()Rectangle+draw()#drawCircle()Circle#drawLine()MonitorRect#drawLine()PrinterRect#drawCircle()MonitorCircle#drawCircle()PrinterCircledraw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)Monitordrawline(x1, x2, y1, y2)drawcircle(x, y, r)PrinterClientTwo Shapes,Two Libraries:Approach OneTuesday, February 8, 2011© Kenneth M. Anderson, 201117+draw()Shape+draw()#drawLine()Rectangle+draw()#drawCircle()Circle#drawLine()MonitorRect#drawLine()PrinterRect#drawCircle()MonitorCircle#drawCircle()PrinterCircledraw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)Monitordrawline(x1, x2, y1, y2)drawcircle(x, y, r)PrinterClientProblem: Try adding another shape or libraryTuesday, February 8, 2011© Kenneth M. Anderson, 201118Emphasis of Problem (I)We are using inheritance to specialize for implementationAnd, surprise, surprise, we encounter the combinatorial subclass program once again2 shapes, 2 libraries: 4 subclasses3 shapes, 3 libraries: 9 subclasses100 shapes, 10 libraries: 1000 subclassesNot good!Tuesday, February 8, 2011© Kenneth M. Anderson, 201119Emphasis of Problem (II)Is there redundancy (duplication) in this design?Ye s , e a c h s u b c l a s s m e t h o d i s V E RY s i m i l a rTight CouplingYo u b e t … e a c h s u b c l a s s h i g h l y d e p e n d e n t o n t h e drawing librarieschange a library, change a lot of subclassesStrong Cohesion? Not completely, shapes need to know about their drawing libraries; no single location for drawingTuesday, February 8, 2011© Kenneth M. Anderson, 201120+draw()Shape#drawLine()#drawCircle()PrinterShapes#drawLine()#drawCircle()MonitorShapes+draw()PrinterRect+draw()PrinterCircle+draw()MonitorRect+draw()MonitorCircledraw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)Monitordrawline(x1, x2, y1, y2)drawcircle(x, y, r)PrinterClientTwo Shapes,Two Libraries:Approach TwoTuesday, February 8, 2011© Kenneth M. Anderson, 201121+draw()Shape#drawLine()#drawCircle()PrinterShapes#drawLine()#drawCircle()MonitorShapes+draw()PrinterRect+draw()PrinterCircle+draw()MonitorRect+draw()MonitorCircledraw_a_line(x1, y1, x2, y2)draw_a_circle(x, y, r)Monitordrawline(x1, x2, y1, y2)drawcircle(x, y, r)PrinterClientSame Problems: Inheritance for Specialization just doesn’t scale!Tuesday, February 8, 2011© Kenneth M. Anderson,


View Full Document

CU-Boulder CSCI 5448 - STRATEGY, BRIDGE & FACTORY

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download STRATEGY, BRIDGE & FACTORY
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 STRATEGY, BRIDGE & FACTORY 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 STRATEGY, BRIDGE & FACTORY 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?