07 - Creational (3)CSC4071 Builder Separate the construction of a complex object from its representation so that the same construction process can create.

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.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
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.
Based on slides of: Mira Balaban Department of Computer Science Ben-Gurion university F. Tip. IBM T J Watson Research Center. Creational Patterns Software.
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 David Talby. This Lecture n The Creational Patterns u Abstract Factory u Builder u Prototype u Factory Method n Choosing Between Them.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
ECE450S – Software Engineering II Creational Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Case Studies on Design Patterns Design Refinements Examples.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Abstract Factory Design Pattern making abstract things.
Design Pattern Course Builder Pattern By : Sajjad Zare Teacher : Dr. Noorhosseini By: Sajjad Zare 1Design Pattern.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.
Software Components Creational Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction.
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
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.
Design Patterns Introduction
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
Design Pattern: Builder Timothy Holper. The Builder design pattern: What is it? The Builder pattern is a way to: ‘Separate the construction of a complex.
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
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.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
07 - Creational PatternsCSC4071 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
Creational Patterns (2)
Design Patterns Satya Puvvada Satya Puvvada.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
Informatics 122 Software Design II
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

07 - Creational (3)CSC4071 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 may different formats on load.

07 - Creational (3)CSC4072 Applicability Use When: –the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled –the construction process must allow different representations for the object that's constructed

07 - Creational (3)CSC4073 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

07 - Creational (3)CSC4074 Structure 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

07 - Creational (3)CSC4075 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.

07 - Creational (3)CSC4076 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(); } … }

07 - Creational (3)CSC4077 Sample Code public class StandardMazeBuilder extends MazeBuilder { private Maze currentMaze; public Maze getMaze() { if( currentMaze==null ) currentMaze = new Maze(); return currentMaze; } … }

07 - Creational (3)CSC4078 Sample Code public class StandardMazeBuilder extends MazeBuilder { … 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()); } … }

07 - Creational (3)CSC4079 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); } … }

07 - Creational (3)CSC40710 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; } }

07 - Creational (3)CSC40711 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()); } … }

07 - Creational (3)CSC40712 Sample Code 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; }

07 - Creational (3)CSC40713 Sample Code public Maze createMaze(MazeBuilder b) { b.buildRoom(1); b.buildRoom(2); b.buildDoor(1, Direction.North, 2); return b.getMaze(); }

07 - Creational (3)CSC40714 Consequences lets you vary a product's internal representation isolates code for construction and representation gives you control over the construction process

07 - Creational (3)CSC40715 Implementation Assembly interface –sometimes can just append next element to structure –more often must lookup previously constructed elements need an interface for doing this that hides Products –cookie of some sort beware order of construction Product hierarchy? –often no great similarity –no great need –don’t use up a precious inheritance dimension abstract v.s. empty methods? –empty methods more generally useful User-installable product classes

07 - Creational (3)CSC40716 Creational Patterns If createMaze() calls virtuals to construct components –Factory Method (class scoped) If createMaze() is passed a parameter object to create rooms, walls, … –Abstract Factory If createMaze() is passed a parameter object to create and connect-up mazes –Builder If createMaze is parameterized with various prototypical rooms, doors, walls, … which it copies and then adds to the maze –Prototype Need to ensure there is only one maze per game, and everybody can access it, and can extend or replace the maze without touching other code. –Singleton