ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Creational PatternsCmpE1961 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns CS is not simply about programming
Design Patterns David Talby. This Lecture n The Creational Patterns u Builder u Prototype u Factory Method u (Abstract Factory) u Singleton n Choosing.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
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.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
ECE450S – Software Engineering II Creational Design Patterns.
Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Abstract Factory Design Pattern making abstract things.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Components Creational Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
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.
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Introduction
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
07 - Creational (3)CSC4071 Builder Separate the construction of a complex object from its representation so that the same construction process can create.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Design Pattern : Builder SPARCS 04 고윤정. Main Concepts One of Creational Patterns. Process to construct Complex object in Abstract type. –Use the same.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
07 - Creational PatternsCSC4071 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
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,
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
Design Patterns: Brief Examples
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
Software Design and Architecture
Creational Design Patterns
Creational Patterns (2)
Software Engineering Lecture 7 - Design Patterns
Object Oriented Design Patterns - Creational Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Design pattern Lecture 6.
Creational Patterns.
Informatics 122 Software Design II
Presentation transcript:

ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III

ECE450 - Software Engineering II2 Detour: The “simplest” creational pattern: Singleton Ensure a class only has one instance, and provide a global point of access to it In my experience, if there is one pattern people know of, it’s Singleton. –Catchy name –Simple concept Again, in my experience, if there is one pattern people get wrong, it’s Singleton. –Used as a simple substitute for global variables –...while thinking that there is nothing wrong with that: “They’re not global variables –they’re a Singleton!” And even when people get the concept right, they frequently get the implementation wrong. –Threatened by parallel programs –Subclassing vs. protecting the one and only instance

ECE450 - Software Engineering II3 When is it OK to use Singletons? Sometimes we need only one instance of an object –One file system –One print spooler If you think you need only one instance of the object, answer these questions: –Can you assign ownership of the single instance reasonably? –Is lazy initialization desirable? –Is global access not provided otherwise? If all criteria are satisfied, you might need a Singleton after all. –Threatened by parallel programs –Subclassing vs. protecting the one and only instance

ECE450 - Software Engineering II4 Singleton’s structure Singleton –Defines a class-scoped instance() operation that lets clients access its unique instance –May be responsible for creating its own unique instance

ECE450 - Software Engineering II5 Sample code (not the real deal) public class SingletonObject { private SingletonObject() { // Just making the constructor private... } public static SingletonObject getSingletonObject() { if (ref == null) // The object has not been created yet ref = new SingletonObject(); return ref; } private static SingletonObject ref; }

ECE450 - Software Engineering II6 But there’s a threading problem... public class SingletonObject { private SingletonObject() { // No code, just making the constructor private... } public static synchronized SingletonObject getSingletonObject() { if (ref == null) // The object has not been created yet ref = new SingletonObject(); return ref; } private static SingletonObject ref; }

ECE450 - Software Engineering II7 Attack of the clones public class Clone { public static void main(String args[]) throws Exception { // Get a singleton SingletonObject obj = SingletonObject.getSingletonObject(); // Oh crap... SingletonObject clone = (SingletonObject) obj.clone(); } Note: Only really a problem if your Singleton class is extending another class that supports cloning –...since clone() is a protected method

ECE450 - Software Engineering II8 Sample code (the real deal) public class SingletonObject { private SingletonObject() { // Just making the constructor private... } public static synchronized SingletonObject getSingletonObject() { if (ref == null) // The object has not been created yet ref = new SingletonObject(); return ref; } public Object clone() throws CloneNotSupportedException { // You shall not pass! throw CloneNotSupportedException; } private static SingletonObject ref; }

ECE450 - Software Engineering II9 Consequences Controlled access to sole instance –Because Singleton encapsulates the sole instance, it has strict control Reduced name space –One access method only Variable number of instances –You could change your mind to have n (e.g. 5) instances

ECE450 - Software Engineering II10 Implementation Implementation is very language-dependent –...and Singletons are more necessary in some language than others Not an excuse for using global variables! –“The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less... [Designers] frequently use Singletons in a misguided attempt to replace global variables... A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.” –Jim Hyslop

ECE450 - Software Engineering II11 Back to that pretty maze of ours... We’ve explored several ways to construct the elements of the maze –Factory methods –Abstract factories –Prototypes We’ll see one more and, with that, finish our tour through creational patterns...

ECE450 - Software Engineering II12 Introducing the Builder Separate the construction of a complex object from its representation so that the same construction process can create different representations –E.g. read in Rich Text Format, converting to many different formats on load.

ECE450 - Software Engineering II13 Applicability Use when: –The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled –The construction process must allow different representations for the object that is constructed

ECE450 - Software Engineering II14 Structure Builder –Specifies an abstract interface for creating parts of a Product object Concrete Builder –Constructs and assembles parts of the product by implementing the Builder interface –Defines and keeps track of the representation it creates –Provides an interface for retrieving the product Director –Constructs an object using the Builder interface Product –Represents the complex object under construction –Includes classes that define the constituent parts, including interfaces for assembling the parts into the final result

ECE450 - Software Engineering II15 Collaborations The Client creates the Director object and configures it with the Builder object Director notifies the Builder whenever a part of the product should be built Builder handles requests from the director and adds parts to the product The client retrieves the product from the Builder

ECE450 - Software Engineering II16 Sample code public abstract class MazeBuilder { public void buildRoom(int r){} public void buildDoor(int r1, int direction, int r2){} public Maze getMaze(){return null;} } public class MazeGame { … public Maze createMaze(MazeBuilder b) { b.buildRoom(1); b.buildRoom(2); b.buildDoor(1, Direction.North, 2); return b.getMaze(); } … }

ECE450 - Software Engineering II17 Sample code public class StandardMazeBuilder extends MazeBuilder { private Maze currentMaze; public Maze getMaze() { if( currentMaze==null ) currentMaze = new Maze(); return currentMaze; } public void buildRoom(int r) { if( getMaze().getRoom(r) == null ) { Room room = new Room(r); getMaze().addRoom(room); for(int d = Direction.First; d <= Direction.Last; d++) room.setSide(d, new Wall()); } … }

ECE450 - Software Engineering II18 Sample code public class StandardMazeBuilder extends MazeBuilder { … public void buildDoor(int r1, int d, int r2) { Room room1 = getMaze().getRoom(r1); Room room2 = getMaze().getRoom(r2); if( room1 == null ) { buildRoom(r1); room1 = getMaze().getRoom(r1); } if( room2 == null ) { buildRoom(r2); room2 = getMaze().getRoom(r2); } Door door = new Door(room1, room2); room1.setSide(d, door); room2.setSide(Direction.opposite(d), door); } … }

ECE450 - Software Engineering II19 Sample code public class CountingMazeBuilder extends MazeBuilder { private int rooms = 0; private int doors = 0; public void buildDoor(int r1, int direction, int r2) { doors++; } public void buildRoom(int r) { rooms++; } public int getDoors() { return doors; } public int getRooms() { return rooms; } }

ECE450 - Software Engineering II20 Sample code public class MazeGame { public static void main(String args[]) { MazeGame mg = new MazeGame(); Maze m = mg.createMaze(new StandardMazeBuilder()); System.out.println(m); CountingMazeBuilder cmb = new CountingMazeBuilder(); mg.createMaze(cmb); System.out.println("rooms = "+cmb.getRooms()); System.out.println("doors = "+cmb.getDoors()); } … }

ECE450 - Software Engineering II21 Sample code (Abstract factory reminder) public Maze createMaze(MazeFactory f) { Room r1 = f.makeRoom(1); Room r2 = f.makeRoom(2); Door d = f.makeDoor(r1,r2); r1.setSide(Direction.North, f.makeWall()); r1.setSide(Direction.East, d); r1.setSide(Direction.West, f.makeWall()); r1.setSide(Direction.South, f.makeWall()); r2.setSide(Direction.North, f.makeWall()); r2.setSide(Direction.East, f.makeWall()); r2.setSide(Direction.West, d); r2.setSide(Direction.South, f.makeWall()); Maze m = f.makeMaze(); m.addRoom(r1); m.addRoom(r2); return m; }

ECE450 - Software Engineering II22 Sample code (Builder comparison) public Maze createMaze(MazeBuilder b) { b.buildDoor(1, Direction.North, 2); // A bit extreme, but you get the point... return b.getMaze(); }

ECE450 - Software Engineering II23 Consequences Lets you vary a product’s internal representation –And hides details on how the product is assembled Isolates code for construction and representation –Clients don’t need to know anything about the classes that define the product’s internal structure; such classes don’t appear in Builder’s interface Gives you finer control over the production process –Constructs the product step by step under the director’s control, instead of in one shot

ECE450 - Software Engineering II24 Implementation Assembly and construction interface –Builders construct their products in step-by-step fashion. –Builder class interface must be general enough to allow the construction of products for all kinds of concrete builders Why no abstract class for products? –In the common case, the products produced by the concrete builders differ so greatly in their representation that there is little to gain from giving different products a common parent class

ECE450 - Software Engineering II25 Creational patterns recap If createMaze() calls virtuals to construct components –Factory method If createMaze() is passed a parameter object to create rooms, walls, and all other elements of the same family –Abstract factory If createMaze() is passed a parameter object to create and connect mazes step by step –Builder If createMaze() is parameterized with various prototypical rooms, doors, walls,..., which it copies and then adds to the maze –Prototype If we need to ensure that there is only one maze per game, or one factory that produces its elements –Singleton