2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Automatically Extracting and Verifying Design Patterns in Java Code James Norris Ruchika Agrawal Computer Science Department Stanford University {jcn,
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207 /~gibson/Teaching/CSC7322/
Architecture and design patterns Jonathan Einav. Lecture Objectives Open a window to the architecture and design patterns world, explain why are they.
Design Patterns Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Programming in Java CSCI-2220 Object Oriented Programming.
Génie logiciel Software Engineering Summary C. Petitpierre.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
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.
Singleton Pattern Presented By:- Navaneet Kumar ise
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
Design Patterns Spring 2017.
How to be a Good Developer
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Creational Pattern: Prototype
Software Design and Architecture
Chapter 8, Design Patterns Bridge
object oriented Principles of software design
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Software Engineering Lecture 7 - Design Patterns
Chapter 8, Design Patterns Introduction
Chapter 8, Design Patterns Singleton
Presentation transcript:

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207 Design Patterns Revisited …/~gibson/Teaching/CSC7322/L12-DesignPatterns-2.pdf

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.2 1.Singleton - creational 2.Iterator – behavioural 3.Visitor – behavioural 4.Proxy - structural 5.Factory - creational 6.Decorator – structural 7.Facade - structural 8.Adapter - structural 9.Chain Of Responsibility - behavioural 10.MVC – a composite pattern (Strategy, Observer, Composite) Learning by PBL – the patterns selected

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.3 The Visitor Pattern See - 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.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.4 The Visitor Pattern See - 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.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.5 UML Class Diagram Visitor QUESTION: why not define an abstract visitable class that can accept visitors?

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.6 UML Class Diagram Visitor Example - Car

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.7 class Car implements CarElement{ CarElement[] elements; public CarElement[] getElements() { return elements.clone(); // Return a copy of the array of references. } public Car() { this.elements = new CarElement[] { new Wheel("front left"), new Wheel("front right"), new Wheel("back left"), new Wheel("back right"), new Doors(), new Engine(8) }; } public String toString(){ return "\n *** A Car *** \n“; } public boolean invariant (){ return (elements!=null && elements.length>0);} public void accept(CarElementVisitor visitor) { visitor.visit(this); for(CarElement element : this.getElements()) { element.accept(visitor); } An example visitable

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.8 class CarElementPrintVisitor implements CarElementVisitor { public void visit(Wheel wheel) { System.out.println(wheel); } public void visit(Engine engine) { System.out.println(engine); } public void visit(Doors doors) { System.out.println(doors); } public void visit(Car car) { System.out.println(car); } An example visitor

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.9 public class Visitor_Test { static public void main(String[] args){ Car car = new Car(); car.accept(new CarElementPrintVisitor()); car.accept(new CarElementCheckInvariantVisitor()); } Testing the example *** 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: what is CarElementCheckInvariantVisitor doing ?

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.10 UML Class Diagram Visitor Example - Car TO DO: Add a visitor ( breakInvariantVisitor ) which changes the state of each component of the car so that their invariants are broken. Update the test class to check that this visitor is working as required

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.11 Proxy Pattern Try to understand the proxy pattern just from the UML diagrams

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.12 Proxy Pattern Try to understand the proxy pattern just from the UML diagrams

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.13 Proxy Problem Create a service that will take an integer and return if it is prime Write a proxy for the service that will ask for a password before the service is executed Write a proxy that will count the number of times the service is executed Implement 2 double proxys: 1)Asks a password then counts 2)Counts then asks a password

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.14 Factory Pattern See - 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.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.15 Factory Pattern See - 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.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.16 Factories: Additional Motivation See: Software Factories Assembling Applications with Patterns, Models, Frameworks and Tools, Greenfield and Short, OOPSLA, Factories are key to Software Product Lines

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.17 Patron: Factory (Fabrique): UML (generic) Can be generalised to: multiple products (by subclassing) multiple clients (by association)

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.18 Factory UML: concrete example – GUIFactory WindowsFactory Button WindowsButton WindowsButtonFactory You can find the files in the Patterns folder in the p_factory package

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.19 Factory code TO DO: restructure/refactor this code into ‘suitable’ packages

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.20 Factory - Windows GUI in Java

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.21 Factory - Windows GUI in Java public class WindowsButtonFactory { public static void 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

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.22 Factory - Windows GUI in Java abstract class Button { private String caption; public abstract void paint(); public String getCaption() {return caption;} public void setCaption(String caption){ this.caption = caption; } public class WindowsButton extends Button { public void paint(){ System.out.println("WindowsButton: "+ getCaption()); }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.23 Factory - Windows GUI in Java abstract class GUIFactory{ public static GUIFactory getFactory(){ return WindowsFactory.getInstance(); } public abstract Button createButton(); } class WindowsFactory extends GUIFactory{ private static WindowsFactory factory = new WindowsFactory(); public static WindowsFactory getInstance () {return factory;}; public Button createButton(){ return(new WindowsButton()); }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.24 GUIFactoryChoice OSXFactory or Win Factory Button OSXButton or Win Button OSXorWindowsFactoryFactory « UML »: TO DO: Write code for OSXButton and OSXFactory

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.25 Factory OSX and Win GUI Buttons in Java abstract class GUIFactoryChoice{ public enum OS_Type {Win, OSX} protected static OS_Type readFromConfigFile(String param){ if (Math.random() > 0.5) return OS_Type.Win; else return OS_Type.OSX; } public static GUIFactory getFactory(){ OS_Type sys = readFromConfigFile("OS_TYPE"); switch (sys) { case Win: return WindowsFactory.getInstance(); case OSX: return OSXFactory.getInstance(); } throw new IllegalArgumentException("The OS type " + sys + " is not recognized."); } public abstract Button createButton(); } Use this more complex factory in your test code

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.26 Factory OSX and Win GUI Buttons in Java public class OSXorWindowsFactory { public static void 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

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.27 Factory OSX and Win GUI Buttons in Java

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.28 Abstract Factory Combining Product Lines Factory1 Factory2

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.29 Abstract Factory: UML class diagram (2 products 2 factory types) Can be generalised to multiple factories with multiple products

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.30 Abstract Factory: UML (sequence diagram) Can be generalised to multiple factories with multiple products

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.31 Abstract Factory: UML - Buttons and Menus for Win and OSX Win OSX Button Menu

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.32 Abstract Factory – GUIFactoryChoice2 TP - TO DO: Compile and execute this code in order to test it against expected behaviour

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.33 Abstract Factory – OSXorWindowsFactory2 public class OSXorWindowsFactory2 { public static void 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(); }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.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): public abstract class GUIFactory2 extends GUIFactory{ public abstract 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

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.35 Problem – add an OS (linux) and a Component (slider) Win OSX Button Menu TO DO : Constuct a linux product with button and slider components: test the behaviour of your product (code)