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

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Design Patterns.
Generic programming in Java
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
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,
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Chapter 3 Introduction to Collections – Stacks Modified
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
CSSE 374: Introduction to Gang of Four Design Patterns
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
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.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Proxy.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
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.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Singleton Pattern Presented By:- Navaneet Kumar ise
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
CSE 143 Lecture 27: Advanced List Implementation
Singleton design pattern
Presentation transcript:

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207 /~gibson/Teaching/CSC7322/ Design Patterns Revisited …/~gibson/Teaching/CSC7322/L11-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 NOTE: you should have a reasonable understanding of all 23 patterns, and a good understanding of implementation concerns in at least 2 different OO languages.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.3 The Singleton Design Pattern See - Intent Ensure a class has only one instance, and provide a global point of access to it. Encapsulated “just-in-time initialization” or “initialization on first use”. Problem Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.4 The Singleton Design Pattern See - Abstract Factory, Builder, and Prototype can use Singleton in their implementation. Facade objects are often Singletons because only one Facade object is required. The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton. The Singleton design pattern is one of the most inappropriately used patterns. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. Relation to other patterns

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.5 The Singleton Design Pattern UML class diagram

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.6 The Singleton Design Pattern UML sequence diagram

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.7 The Singleton Design Pattern Problem: Examine the 4 Singleton Java implementations in the folder Singleton (~gibson/Teaching/CSC7322/Code/Singleton.zip) Question: what are the differences between the implementations: Singleton1, Singleton2, Singleton3, Singleton4 ; and which best corresponds to our requirements/design? NOTE: Singleton1a and Singleton1b are for the next question on subclassing

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.8 The Singleton Design Pattern – example implementation (1) public class Singleton1 { protected static Singleton1 uniqueInstance = null; private int data; public synchronized static Singleton1 instance() { if(uniqueInstance == null) uniqueInstance = new Singleton1(); return uniqueInstance; } protected Singleton1() {data=0;} public int getData(){return data;} public void setData(int d){data =d;} }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.9 The Singleton Design Pattern – example implementation (2) public class Singleton2 { public static final Singleton2 uniqueinstance = new Singleton2(); private int data; private Singleton2() {data=0;} public int getData(){return data;} public void setData(int d){data =d;} }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.10 The Singleton Design Pattern – example implementation (3) public class Singleton3 { private static final Singleton3 instance = new Singleton3(); private int data; private Singleton3() { data=0; } public static Singleton3 instance() {return instance;} public int getData(){return data;} public void setData(int d){data =d;} }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.11 The Singleton Design Pattern – example implementation (4) public class Singleton4 { private int data; private Singleton4() { data=0; } private static class SingletonHolder { private static final Singleton4 INSTANCE = new Singleton4(); } public static Singleton4 getInstance() { return SingletonHolder.INSTANCE; } public int getData(){return data;} public void setData(int d){data =d;} }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.12 The Singleton Design Pattern – what about subclassing? The Singleton Design Pattern is meant to give you control over access to the Singleton class. But subclassing allows other code to access your class without you having direct control The uniqueness of the class cannot be imposed as a compile-time constraint on the subclass unless you use a private constructor (or declare the class to be final). If you want to allow subclassing, for example, you might make the constructor protected, but then a subclass could provide a public constructor, allowing anyone to make instances. QUESTION: which of these 2 subclassable singleton designs do you prefer:? Supposing we have a Singleton class A and a class B that is a sub-class of A: 1). You can have a single instance of A *OR* a single instance of B, but not both. 2). You can have exactly one instance of A *AND* exactly one instance of B. TO DO: Can you implement and test one of these designs? QUESTION: How can/should this design/code be extended to multiple subclasses?

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.13 Iterator An iterator is an object that facilitates traversal of a container/collection of objects Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterator are fixed, iterators are tightly coupled to the container implementation in order to enable the operational semantics of the iterator. Note that an iterator performs traversal and also gives access to data elements in a container, but does not perform iteration. QUESTION: Have you already seen this in Java?

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.14 Iterator Pattern See - Intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. The C++ and Java standard library abstraction that makes it possible to decouple collection classes and algorithms. Promote to “full object status” the traversal of a collection. Polymorphic traversal Problem Need to “abstract” the traversal of wildly different data structures so that algorithms can be defined that are capable of interfacing with each transparently.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.15 Iterator 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. Polymorphic Iterators rely on Factory Methods to instantiate the appropriate Iterator subclass. Memento is often used in conjunction with Iterator. An Iterator can use a Memento to capture the state of an iteration. The Iterator stores the Memento internally.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.16 Iterator Pattern - Contrasting with indexing Although indexing may also be used with some object-oriented containers, the use of iterators may have some advantages: Counting loops are not suitable to all data structures, in particular to data structures with no or slow random access Iterators make the code more readable, reusable, and less sensitive to a change in the data structure. An iterator can enforce additional restrictions on access, such as ensuring that elements can not be skipped or that a previously visited element can not be accessed a second time. An iterator may allow the container object to be modified without invalidating the iterator. For instance, once an iterator has advanced beyond the first element it may be possible to insert additional elements into the beginning of the container with predictable results. With indexing this is problematic since the index numbers must change.

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.17 Iterators and collections in Java There are multiple ways to iterate a collection in Java Example: ArrayList persons = new ArrayList(); Person p= new Person("john", "mith"); persons.add(p);... Iterator i = persons.iterator(); while(i.hasNext()) { Person p= (Person)i.next(); p.print(); } for(Person p : persons) { p.print(); } // since java 1.5

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.18 Iterator in UML class diagram

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.19 Iterator in UML class diagram (linked list example in Java) Notice that the Aggregate is an Iterable (in Java)

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.20 Java Iterable Example (in Iterator folder/package) (~gibson/Teaching/CSC7322/Code/Iterator.zip) TO DO

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.21 public class MyTableOfStrings implements Iterable { protected String[] data; public MyTableOfStrings(String [] data) { this.data = data; } public int length(){return data.length;} public Iterator iterator() { return new MyTableOfStrings_Iterator(this); } Java Iterable Example (in Iterator folder/package) (~gibson/Teaching/CSC7322/Code/Iterator.zip)

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.22 public class MyTableOfStrings_Iterator implements Iterator { private int index; private MyTableOfStrings table; public MyTableOfStrings_Iterator(MyTableOfStrings tab) { index = tab.length()-1; table = tab; } public String next() { index--; return table.data[index +1]; } public boolean hasNext() { return index >= 0; } public void remove() { throw new UnsupportedOperationException(); }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.23 public class MyTableOfStrings_Test { public static void main(String[] s) { String [] data = {"one", "two", "three"}; MyTableOfStrings t = new MyTableOfStrings(data); System.out.println("Iterate over original data array"); for (String value : data) { System.out.println(" "+value); } System.out.println("\nIterate over same data in MyTableOfStrings"); for (String value : t) { System.out.println(" "+value); }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.24 TO DO : Compile and execute the test class

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.25 In the previous example we saw how the Iterator code decides the order in which to visit the elements. By default Java iterates through arrays from the 1st to the last elements. In the example we iterate through MyTableOfStrings in reverse order. TO DO: Change the iterator code so that the elements are visited in random order. Do not do this by shuffling the elements as this may be expensive for a large number of elements. Random Iteration: Reservoir Sampling

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.26 A more complex data structure: an urn/ballot box of bulletins/votes TO DO: Your task is to iterate through the Strings in the Urn Look at the Urn_Test Code and write the Urn and Urn_Iterator classes appropriately. Check that the test, executed on your code, produces the expected results

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.27 public class Urn_Test { public static void main(String[] s) { String [] preferences1 = {"gibson", "smyth", "hughes"}; MyTableOfStrings vote1 = new MyTableOfStrings( preferences1); String [] preferences2 = {"jones", "bell"}; MyTableOfStrings vote2 = new MyTableOfStrings( preferences2); String [] preferences3 = {"raffy", "lallet"}; MyTableOfStrings vote3 = new MyTableOfStrings( preferences3); MyTableOfStrings [] votes = { vote1, vote2, vote3}; Urn urn = new Urn (votes); System.out.println("\nIterate over strings on bulletins in Urn"); for (String value : urn) {System.out.println(" "+value);} }

2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.28