Creational Patterns (2)

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.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns David Talby. This Lecture n The Creational Patterns u Builder u Prototype u Factory Method u (Abstract Factory) u Singleton n Choosing.
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 Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
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.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.
Software Components Creational Patterns.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
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.
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.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
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 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.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
07 - Creational (3)CSC4071 Builder Separate the construction of a complex object from its representation so that the same construction process can create.
 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.
The Abstract Factory Pattern (Creational) ©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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
March 10, 2007 Design Patterns David Talby.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Design Pattern.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Patterns: Brief Examples
Factory Method Pattern
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Behavioral Design Patterns
Creational Design Patterns
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Multiuser Protection and the Mediator Pattern
Informatics 122 Software Design II
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Design pattern Lecture 6.
Creational Patterns.
Informatics 122 Software Design II
GoF Patterns Ch. 26.
Presentation transcript:

Creational Patterns (2) CSE8313

Inheritance, Encapsulation, Polymorphism Lower the Cost of Maintenance Economic Goal Design Principles Coupling-Cohesion, Open-Close, Information-Hiding, Dependency Inversion, Separation of Concerns… Experiences of leveraging OO to follow the principles Design Patterns Show the simple maze code OO programming Inheritance, Encapsulation, Polymorphism

The Maintenance of Simple Maze Game Is not Simple! How the classes will be Add? Removed? Changed?

What are the concerns? Features/Concerns Maze Type: red, blue, enchanted, bombed, HarryPotter, SnowWhite… Each type requires a *family* of components Maze Components: wall, door, room maze Maze Structure: 2 rooms? 9 rooms? 100 rooms? Square maze? Component Building: How many walls a room can have? 4? 8? How to build a door? Maze Building Process: 1. Maze, 2, Rooms, 3, Doors …

Current code public Maze createMaze (MazeFactory factory) { Maze aMaze = factory.makeMaze(); Room r1 = factory.makeRoom(1); Room r2 = factory.makeRoom(2); Door theDoor = factory.makeDoor(r1, r2); aMaze.addRoom(r1); aMaze.addRoom(r2); r1.setSide(MapSite.NORTH, factory.makeWall()); r1.setSide(MapSite.EAST, theDoor); r1.setSide(MapSite.SOUTH, factory.makeWall()); r1.setSide(MapSite.WEST, factory.makeWall()); r2.setSide(MapSite.NORTH, factory.makeWall()); r2.setSide(MapSite.EAST, factory.makeWall()); r2.setSide(MapSite.SOUTH, factory.makeWall()); r2.setSide(MapSite.WEST, theDoor); return aMaze; }

Simplification We would like to factor out the knowledge about how to assemble Rooms. Solution? Hire a contractor A “Builder” And just give orders: Act as the “Director” of the work

Meet the Builder

Builder Pattern - structure

Builder Participants Builder ConcreteBuilder Director Product specifies an abstract interface for creating parts of a Product object ConcreteBuilder 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 demands the construction of an object using the Builder interface Product represents the complex object under construction. Concrete builder builds the product’s internal representation and defines the process by which it is assembled

Builder: motivation 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

The Maze with Builder Simplify the code of the CreateMaze method by passing it a MazeBuilder as a parameter. MazeBuilder interface can be used to build three things 1) the maze 2) rooms with a particular room number 3) doors between numbered rooms.

The Maze - Builder abstract

The Maze ---Builder The operations in the abstract MazeBuilder super- class are meant to be overridden by subclasses, i.e. concrete builders. Concrete builders will override also GetMaze() to return the Maze they build

Modified code In the MazeGameCreator class: Maze createMaze(Builder theBuilder) { builder.buildMaze() builder.buildRoom(1); builder.buildRoom(2); builder.buildDoor(1,2); return builder.getMaze() }

The Maze ---Builder Notice how the Builder hides the internal representation – that is classes that define rooms, doors and walls – of the maze how these parts are assembled to form the final maze. This makes easy to change the way Maze is represented since no client code is dependent on it. For instance we might have windows in the representation of rooms This is a design decision that is hidden from clients. Client only needs to know about Maze, Rooms and Doors in very little detail

The Maze ---Builder Subdividing responsibility between the Maze and Builder classes and separating the two Enabled reusability of part of the construction process Can have a variety of MazeBuilders each constructing mazes with different classes for rooms, walls, doors. What was the basis for the decision which part of the construction remains in the MazeCreator, and what is delegated to Builder? Find what must vary and extract it, hide it. The varying parts: type of walls, doors, rooms varies, The stable parts: e.g. the fact that rooms are connected by doors. (What if this varies too? ) Show the simple maze builder code. What do we do if we need different types of maze too?

Creational Patterns – Builder Intent: Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Creational Patterns – Builder RTF Text Converter

Creational Patterns – Builder Use the Builder pattern 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

Creational Patterns – Builder Structure and Participants (TextConverter) (RTFReader) (ASCIIText, TeXText, TextWidget) (ASCIIConverter, TeXConverter, TextWidgetConverter)

Creational Patterns – Builder Collaborations

Creational Patterns – Builder Consequences It lets you vary a product's internal representation: The Builder object provides the director with an abstract interface for constructing the product. It isolates code for construction and representation: The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. It gives you finer control over the construction process: The Builder pattern constructs the product step by step under the director's control.

Creational Patterns – Builder Implementation Assembly and construction interface Builders construct their products in step-by-step fashion. Therefore the 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? Empty methods as default in Builder

Creational Patterns – Builder MazeBuilder Example class MazeBuilder { public: virtual void BuildMaze() { } virtual void BuildRoom(int room) { } virtual void BuildDoor(int roomFrom, int roomTo) { } virtual Maze* GetMaze() { return 0; } protected: MazeBuilder(); }; Maze* MazeGame::CreateMaze (MazeBuilder& builder) { builder.BuildMaze(); builder.BuildRoom(1); builder.BuildRoom(2); builder.BuildDoor(1, 2); return builder.GetMaze(); } Maze* MazeGame::CreateComplexMaze (MazeBuilder& builder) { builder.BuildRoom(1); // ... builder.BuildRoom(1001); return builder.GetMaze(); }

Creational Patterns – Builder MazeBuilder Example class StandardMazeBuilder : public MazeBuilder { public: StandardMazeBuilder(); virtual void BuildMaze(); virtual void BuildRoom(int); virtual void BuildDoor(int, int); virtual Maze* GetMaze(); private: Direction CommonWall(Room*, Room*); Maze* _currentMaze; }; StandardMazeBuilder::StandardMazeBuilder () { _currentMaze = 0; } void StandardMazeBuilder::BuildMaze () { _currentMaze = new Maze; } Maze* StandardMazeBuilder::GetMaze () { return _currentMaze;

Creational Patterns – Builder MazeBuilder Example void StandardMazeBuilder::BuildRoom (int n) { if (!_currentMaze->RoomNo(n)) { Room* room = new Room(n); _currentMaze->AddRoom(room); room->SetSide(North, new Wall); room->SetSide(South, new Wall); room->SetSide(East, new Wall); room->SetSide(West, new Wall); } void StandardMazeBuilder::BuildDoor (int n1, int n2) { Room* r1 = _currentMaze->RoomNo(n1); Room* r2 = _currentMaze->RoomNo(n2); Door* d = new Door(r1, r2); r1->SetSide(CommonWall(r1,r2), d); r2->SetSide(CommonWall(r2,r1), d); } Maze* maze; MazeGame game; StandardMazeBuilder builder; game.CreateMaze(builder); maze = builder.GetMaze();

Creational Patterns – Builder CountingMazeBuilder Example class CountingMazeBuilder : public MazeBuilder { public: CountingMazeBuilder(); virtual void BuildMaze(); virtual void BuildRoom(int); virtual void BuildDoor(int, int); virtual void AddWall(int, Direction); void GetCounts(int&, int&) const; private: int _doors; int _rooms; }; CountingMazeBuilder::CountingMazeBuilder () { _rooms = _doors = 0; } void CountingMazeBuilder::BuildRoom (int) { _rooms++; void CountingMazeBuilder::BuildDoor (int, int) { _doors++; void CountingMazeBuilder::GetCounts ( int& rooms, int& doors ) const { rooms = _rooms; doors = _doors; int rooms, doors; MazeGame game; CountingMazeBuilder builder; game.CreateMaze(builder); builder.GetCounts(rooms, doors); cout << "The maze has " << rooms << " rooms and " << doors << " doors" << endl;

Abstract Factory & Builder What is the difference between Abstract Factory pattern and Builder pattern?

What about sub-contracting?

Do we still need A Factory?

The Maze ---Builder The concrete builder SimpleMazeBuilder is an implementation that builds simple mazes. Let’s take a look at its code: Maze myMaze; Maze getMaze() { return myMaze; } void buildMaze() { myMaze = new Maze(); } void buildRoom (int i) { r = new Room(i): myMaze.addRoom(r); // all room-construction code …

SimpleMazeBuilder This simple builder takes care of object instantiation itself With vanilla rooms etc. We could still use a Factory For extensibility For separation of concerns Let’s create a FactoryMazeBuilder

FactoryMazeBuilder

Builder Maze Game

Creational patterns Creational patterns involve object instantiation and all provide a way to decouple a client from objects it needs to instantiate Some members of this “group”: Factory Method Abstract Factory Builder

Intent The intent of Factory Method is to allow a class to defer instantiation to its subclasses The intent of Abstract Factory is to create families of related objects without explicitly tying the code on their concrete classes The intent of Builder is to encapsulate the construction of composite structures

More creational patterns

One of a Kind Objects There are cases in which you need one object of a certain type One and only one Shared globally across the system Examples: Abstraction for accessing a single resource (e.g. log file) A load balancer object A centralized Factory A dialog box that is used across the GUI

Requirements Enforce uniqueness: Responsibility: make sure nobody can create further instances use information hiding mechanisms Responsibility: you don’t want the clients of that object to be responsible for checking uniqueness and for usage policy encapsulate that responsibility within the class of that object

The Singleton pattern

Multi-threading What if multiple clients try to obtain the handle to the singleton at the same time? Is uniqueness guaranteed? You need a thread-safe implementation for multi-threaded applications

A bullet-proof Java “idiom” public class Singleton { // Private constructor suppresses generation of (public) default constructor private Singleton() {} /** * SingletonHolder is loaded on the 1st call of Singleton.getInstance() * or the first access to SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { private final static Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE;

Singleton instance is a private class attribute The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it. Singleton    instance is a private class attribute instance() is a class (static) operation. Creates and maintains its own unique instance.

The Singleton pattern Classification: Creational purpose; Object scope Context: You want to have only one object of a class, but no global object controls the instantiation of this object. You want to ensure all clients reference this object without passing a handle to all of them Problem: several clients need regulated access to the same thing, and you want to ensure uniqueness Solution: guarantees one and only on instance Consequences: Clients do not need to care about existence or uniqueness of singleton instance They do not need to pass around references

Implementation Example https://www.youtube.com/watch?v=NZaXM67fxbs

Pattern team-up Singleton + Factory Method How do they work together? The product created by the Factory Method can be a Singleton A different type depending on settings or preferences Example: java.awt.Toolkit

Singleton variant Why one and only one? What about N and no more than N? Can you leverage the Singleton pattern to manage N objects that can be re-used? Think of thread or connection pools

The Prototype pattern

Creation vs. cloning We are better at cloning Creation is hard work

Creation vs. cloning When you instantiate an object with the new operator you create it out of thin air: e.g. in Java , the JVM goes through a complex process of loading, memory allocation, initialization … Cloning an existing object may be a better choice Especially when the object is complex and/or large

The two approaches Java caveat: shallow vs. deep copy public class Sheep { private String dna; public Sheep() { //instantiate VERY long DNA string replicateAllGenes(); } private replicateAllGenes() { //compute all the various DNA fragments for Sheep Long and memory expensive Done for every instance public class Sheep implements Cloneable { public static final Sheep proto = new Sheep(); private final String dna = “…”; public Sheep() {} public Object clone() { //Cloneable.clone() provides a shallow copy of an object } public class SheepCloneMachine { public static void Main (String[] args) { Sheep dolly = Sheep.proto; for (i=0; i<100;i++) Sheep other = dolly.clone(); Java caveat: shallow vs. deep copy

Prototype pattern A prototype is an object that packs in advance all the necessary info for further instances Instead of instantiating new instances, clients clone the prototype Notice that cloning carries over the full state of the prototype

Real world usage Examples: Object contains data that is complex to compute Object contains lots of data Object contains data that are loaded from a slow medium You want to give the same data to multiple workers

Prototype - structure

Implementation Example https://www.youtube.com/watch?v=AFbZhRL0Uz8

We have done with all Creational Patterns