1 / 35

CSC 7322 : Object Oriented Development J Paul Gibson, A207 paul.gibson@int-edu.eu http://www-public. it-sudparis.eu /

CSC 7322 : Object Oriented Development J Paul Gibson, A207 paul.gibson@int-edu.eu http://www-public. it-sudparis.eu /~gibson/Teaching/CSC7322/. Design Patterns Revisited …/~ gibson / Teaching /CSC7322/L12-DesignPatterns-2.pdf. Learning by PBL – the patterns selected.

ave
Download Presentation

CSC 7322 : Object Oriented Development J Paul Gibson, A207 paul.gibson@int-edu.eu http://www-public. it-sudparis.eu /

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 7322 : Object OrientedDevelopment J Paul Gibson, A207 paul.gibson@int-edu.eu http://www-public.it-sudparis.eu/~gibson/Teaching/CSC7322/ Design Patterns Revisited …/~gibson/Teaching/CSC7322/L12-DesignPatterns-2.pdf TSP: Software Engineering

  2. Learning by PBL – the patterns selected Singleton - creational Iterator – behavioural Visitor – behavioural Proxy - structural Factory - creational Decorator – structural Facade - structural Adapter - structural Chain Of Responsibility - behavioural MVC – a composite pattern (Strategy, Observer, Composite) TSP: Software Engineering

  3. The Visitor Pattern See - http://sourcemaking.com/design_patterns/visitor • Intent • Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. • The classic technique for recovering lost type information. • Do the right thing based on the type of two objects. • Double dispatch • Problem • Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid “polluting” the node classes with these operations. And, you don’t want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation. TSP: Software Engineering

  4. The Visitor Pattern See - http://sourcemaking.com/design_patterns/visitor • Relation to other patterns • The abstract syntax tree of Interpreter is a Composite (therefore Iterator and Visitor are also applicable). • Iterator can traverse a Composite. Visitor can apply an operation over a Composite. • The Visitor pattern is like a more powerful Command pattern because the visitor may initiate whatever is appropriate for the kind of object it encounters. • The Visitor pattern is the classic technique for recovering lost type information without resorting to dynamic casts. • NOTE: Visitor is not good for the situation where “visited” classes are not stable. Every time a new Composite hierarchy derived class is added, every Visitor derived class must be amended. TSP: Software Engineering

  5. UML Class DiagramVisitor QUESTION: why not define an abstract visitable class thatcanacceptvisitors? TSP: Software Engineering

  6. UML Class DiagramVisitorExample - Car TSP: Software Engineering

  7. An examplevisitable class Car implementsCarElement{ CarElement[] elements; publicCarElement[] getElements() { returnelements.clone(); // Return a copy of the array of references. } public Car() { this.elements = newCarElement[] { new Wheel("front left"), new Wheel("front right"), new Wheel("back left") , new Wheel("back right"), newDoors(), newEngine(8) }; } public String toString(){return"\n *** A Car *** \n“; } publicboolean invariant (){ return (elements!=null && elements.length>0);} publicvoidaccept(CarElementVisitorvisitor) { visitor.visit(this); for(CarElementelement : this.getElements()) { element.accept(visitor); } } } TSP: Software Engineering

  8. An examplevisitor classCarElementPrintVisitorimplementsCarElementVisitor { publicvoidvisit(Wheel wheel) { System.out.println(wheel); } publicvoidvisit(Engineengine) { System.out.println(engine); } publicvoidvisit(Doorsdoors) { System.out.println(doors); } publicvoidvisit(Car car) { System.out.println(car); } } TSP: Software Engineering

  9. Testing the example publicclassVisitor_Test { staticpublicvoid main(String[] args){ Car car = new Car(); car.accept(newCarElementPrintVisitor()); car.accept(newCarElementCheckInvariantVisitor()); } } *** A Car *** front left is not turning front right is not turning back left is not turning back right is not turning LeftDoorLocked is true and RightDoorLocked is true Engine speed is 0 / 8 QUESTION: whatisCarElementCheckInvariantVisitordoing? TSP: Software Engineering

  10. UML Class DiagramVisitorExample - Car TO DO: Add a visitor (breakInvariantVisitor) which changes the state of each component of the car sothattheir invariants are broken. Update the test class to check thatthisvisitorisworking as required TSP: Software Engineering

  11. Proxy Pattern Try to understand the proxy pattern justfrom the UML diagrams TSP: Software Engineering

  12. Proxy Pattern Try to understand the proxy pattern justfrom the UML diagrams TSP: Software Engineering

  13. Proxy Problem Create a service thatwilltake an integer and return if itis prime Write a proxy for the service thatwillask for a passwordbefore the service isexecuted Write a proxy thatwill count the number of times the service isexecuted Implement 2 double proxys: Asks a passwordthencounts Countsthenasks a password TSP: Software Engineering

  14. Factory Pattern See - http://sourcemaking.com/design_patterns/factory_method • Intent • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. • Defining a “virtual” constructor. • Problem • A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation. NOTE: The implementation of Factory Method discussed in the largely overlaps with that of Abstract Factory. TSP: Software Engineering

  15. Factory Pattern See - http://sourcemaking.com/design_patterns/factory_method Relation to other patterns Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype. Factory Methods are usually called within Template Methods. Factory Method: creation through inheritance. Prototype: creation through delegation. Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. Prototype doesn’t require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn’t require Initialize. The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type. TSP: Software Engineering

  16. Factories: Additional Motivation Factories are key to Software Product Lines • See: • Software Factories Assembling Applications with Patterns, Models, Frameworks and Tools, Greenfield and Short, OOPSLA, 2003. TSP: Software Engineering

  17. Patron: Factory (Fabrique): UML (generic) • Can be generalised to: • multiple products (by subclassing) • multiple clients (by association) TSP: Software Engineering

  18. Factory UML: concrete example – WindowsButtonFactory GUIFactory Button WindowsFactory WindowsButton You canfind the files in the Patterns folder in the p_factory package TSP: Software Engineering

  19. Factorycode TO DO: restructure/refactorthis code into ‘suitable’ packages TSP: Software Engineering

  20. Factory - Windows GUI in Java TSP: Software Engineering

  21. Factory - Windows GUI in Java publicclass WindowsButtonFactory { publicstaticvoid main(String[] args){ GUIFactory aFactory = GUIFactory.getFactory(); System.out.println("Using factory "+ aFactory+" to construct aButton"); Button aButton = aFactory.createButton(); aButton.setCaption("Push a"); aButton.paint(); GUIFactory bFactory = GUIFactory.getFactory(); System.out.println("Using factory "+ bFactory+" to construct bButton"); Button bButton = bFactory.createButton(); bButton.setCaption("Push b"); bButton.paint(); } } TO DO: Compile and execute to test for expected output TSP: Software Engineering

  22. Factory - Windows GUI in Java abstractclass Button { private String caption; publicabstractvoid paint(); public String getCaption() {returncaption;} publicvoid setCaption(String caption){ this.caption = caption; } } publicclass WindowsButton extends Button { publicvoid paint(){ System.out.println("WindowsButton: "+ getCaption()); } } TSP: Software Engineering

  23. Factory - Windows GUI in Java abstractclass GUIFactory{ publicstatic GUIFactory getFactory(){ return WindowsFactory.getInstance(); } publicabstract Button createButton(); } class WindowsFactory extends GUIFactory{ privatestatic WindowsFactory factory = new WindowsFactory(); publicstatic WindowsFactory getInstance () {returnfactory;}; public Button createButton(){ return(new WindowsButton()); } } TSP: Software Engineering

  24. Factory « UML »: OSXorWindowsFactory GUIFactoryChoice Button OSXFactory or Win Factory OSXButton or Win Button TO DO: Write code for OSXButton and OSXFactory TSP: Software Engineering

  25. Factory OSX and Win GUI Buttons in Java abstractclass GUIFactoryChoice{ publicenum OS_Type {Win, OSX} protectedstatic OS_Type readFromConfigFile(String param){ if (Math.random() > 0.5) return OS_Type.Win; elsereturn OS_Type.OSX; } publicstatic GUIFactory getFactory(){ OS_Type sys = readFromConfigFile("OS_TYPE"); switch (sys) { caseWin: return WindowsFactory.getInstance(); caseOSX: return OSXFactory.getInstance(); } thrownew IllegalArgumentException("The OS type " + sys + " is not recognized."); } publicabstract Button createButton(); } Use this more complex factory in your test code TSP: Software Engineering

  26. Factory OSX and Win GUI Buttons in Java publicclass OSXorWindowsFactory { publicstaticvoid main(String[] args){ GUIFactory aFactory = GUIFactoryChoice.getFactory(); System.out.println("Using factory "+ aFactory+" to construct aButton"); Button aButton = aFactory.createButton(); aButton.setCaption("Push a"); aButton.paint(); GUIFactory bFactory = GUIFactoryChoice.getFactory(); System.out.println("Using factory "+ bFactory+" to construct bButton"); Button bButton = bFactory.createButton(); bButton.setCaption("Push b"); bButton.paint(); GUIFactory cFactory = GUIFactoryChoice.getFactory(); System.out.println("Using factory "+ cFactory+" to construct cButton"); Button cButton = cFactory.createButton(); cButton.setCaption("Push c"); cButton.paint(); } } TO DO: Compile and execute this code TSP: Software Engineering

  27. Factory OSX and Win GUI Buttons in Java TSP: Software Engineering

  28. Abstract Factory CombiningProduct Lines Factory1 Factory2 TSP: Software Engineering

  29. Abstract Factory: UML class diagram (2 products 2 factory types) Can be generalised to multiple factories with multiple products TSP: Software Engineering

  30. Abstract Factory: UML(sequencediagram) Can begeneralised to multiple factorieswith multiple products TSP: Software Engineering

  31. Abstract Factory: UML - Buttons and Menus for Win and OSX Button OSX Win Menu TSP: Software Engineering

  32. Abstract Factory – GUIFactoryChoice2 TP - TO DO: Compile and execute this code in order to test it against expected behaviour TSP: Software Engineering

  33. Abstract Factory – OSXorWindowsFactory2 publicclass OSXorWindowsFactory2 { publicstaticvoid main(String[] args){ GUIFactory2 aFactory = GUIFactoryChoice2.getFactory(); System.out.println("Using factory "+ aFactory+" to construct aButton"); Button aButton = aFactory.createButton(); aButton.setCaption("Push a"); aButton.paint(); System.out.println("Using factory "+ aFactory+" to construct aMenu"); Menu aMenu = aFactory.createMenu(); aMenu.setCaption("Menu a"); aMenu.display(); GUIFactory2 bFactory = GUIFactoryChoice2.getFactory(); System.out.println("Using factory "+ bFactory+" to construct bButton"); Button bButton = bFactory.createButton(); bButton.setCaption("Push b"); bButton.paint(); System.out.println("Using factory "+ bFactory+" to construct bMenu"); Menu bMenu = bFactory.createMenu(); bMenu.setCaption("Menu b"); bMenu.display(); } } TSP: Software Engineering

  34. Abstract Factory – OSXorWindowsFactory2 Note that we had to extend the behaviour of classes in order to include buttons and menus (but we kept to the same design pattern): publicabstractclass GUIFactory2 extends GUIFactory{ publicabstract Menu createMenu(); } class WindowsFactory2 extends GUIFactory2 … class OSXFactory2 extends GUIFactory2 … class GUIFactoryChoice2 extends GUIFactoryChoice … TO DO: Look at code and try to understand how it works TSP: Software Engineering

  35. Problem – add an OS (linux) and a Component (slider) Button OSX Win Menu TO DO : Constucta linux productwithbutton and slider components: test the behaviour of yourproduct (code) TSP: Software Engineering

More Related