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,

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

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
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.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
CREATIONAL DESIGN PATTERN Object Pool. CREATIONAL DESIGN PATTERN creational design patterns are design patterns that deal with object creation mechanisms,
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
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.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Advanced Programming Rabie A. Ramadan 7.
Abstract Factory Design Pattern making abstract things.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Software Components Creational Patterns.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Design 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.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
Programming in Java CSCI-2220 Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Design patterns.
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
Design Patterns Solving problems with already known solutions Unit - 13.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
DESIGN PATTERNS -CREATIONAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
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.
C++ Design Patterns 1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Joey Paquet, Emil Vassev. 2007, 2011, 2013.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
 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.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Unit II-Chapter No. : 5- design Patterns
Low Budget Productions, LLC
Factory Patterns 1.
Creational Pattern: Prototype
Software Design and Architecture
Review: Two Programming Paradigms
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
More Design Patterns 1.
More Design Patterns 1.
Object Oriented Design Patterns - Creational Patterns
Factory Pattern.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
Presented by Igor Ivković
Presentation transcript:

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, trying to create objects in a manner that is suitable to the situation Abstract Factory Intent: Provide an interface for creating families of related objects without specifying their concrete classes Builder Intent: Separate the construction of a complex object from its representation so that the same construction process can create different representations Factory Method Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate Prototype Intent: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Singleton Intent: Ensure that a class has only one instance, and provide a global point of access to it

Creational Pattern: Abstract Factory If an application needs to be portable, it needs to encapsulate platform dependencies. C h a p t e r 3 – P a g e 15 These platforms might include the operating system, the database management system, the windowing system, the web browser, etc. Frequently, this encapsulation isn’t engineered in advance, resulting in a massive proliferation of #ifdef case statements with options for all currently supported platforms.

The Abstract Factory Pattern Rather than having clients create platform objects directly, they create abstract objects and utilize an abstract factory as an interface for producing the concrete objects. C h a p t e r 3 – P a g e 16

Non-Software Example The AnimalWorld client uses the abstract ContinentFactory to produce abstract Herbivore and Carnivore products. C h a p t e r 3 – P a g e 17 When the concrete platform is an AfricaFactory, concrete Wildebeest and Lion objects are created, while when the concrete platform is an AmericaFactory, concrete Bison and Wolf objects are created.

Primitive Software Example The client uses the abstract ShapeFactory to produce abstract curved and straight shapes. C h a p t e r 3 – P a g e 18 If the platform has been established as “simple”, concrete Circle and Square objects are created, while if the platform has been established as “robust”, concrete Ellipse and Rectangle objects are created.

Shape Abstract Factory C++ Code C h a p t e r 3 – P a g e 19 #include using namespace std; class Shape { public: Shape() { id = total++; } virtual void draw() = 0; protected: int id; static int total; }; int Shape::total = 0; class Circle : public Shape { public: void draw() { cout << "circle " << id << ": draw" << endl; } }; class Square : public Shape { public: void draw() { cout << "square " << id << ": draw" << endl; } }; class Ellipse : public Shape { public: void draw() { cout << "ellipse " << id << ": draw" << endl; } }; class Rectangle : public Shape { public: void draw() { cout << "rectangle " << id << ": draw" << endl; } }; class Factory { public: virtual Shape* createCurvedInstance() = 0; virtual Shape* createStraightInstance() = 0; }; class SimpleShapeFactory : public Factory { public: Shape* createCurvedInstance() { return new Circle; } Shape* createStraightInstance() { return new Square; } }; class RobustShapeFactory : public Factory { public: Shape* createCurvedInstance() { return new Ellipse; } Shape* createStraightInstance() { return new Rectangle; } }; void main() { #ifdef SIMPLE Factory* factory = new SimpleShapeFactory; #else Factory* factory = new RobustShapeFactory; #endif Shape* shapes[3]; shapes[0] = factory->createCurvedInstance(); shapes[1] = factory->createStraightInstance(); shapes[2] = factory->createCurvedInstance(); for (int i=0; i < 3; i++) shapes[i]->draw(); }

Multi-Platform Pre-Abstract Factory C h a p t e r 3 – P a g e 20 #include using namespace std; #define MOTIF class Widget { public: virtual void draw() = 0; }; class MotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; class MotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; class WindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; class WindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; void display_window_one() { #ifdef MOTIF Widget* w[] = { new MotifButton, new MotifMenu }; #else // WINDOWS Widget* w[] = { new WindowsButton, new WindowsMenu }; #endif w[0]->draw(); w[1]->draw(); } void display_window_two() { #ifdef MOTIF Widget* w[] = { new MotifMenu, new MotifButton }; #else // WINDOWS Widget* w[] = { new WindowsMenu, new WindowsButton }; #endif w[0]->draw(); w[1]->draw(); } void main() { #ifdef MOTIF Widget* w = new MotifButton; #else // WINDOWS Widget* w = new WindowsButton; #endif w->draw(); display_window_one(); display_window_two(); }

Multi-Platform w/Abstract Factory C h a p t e r 3 – P a g e 21 #include using namespace std; #define WINDOWS class Widget { public: virtual void draw() = 0; }; class MotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; class MotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; class WindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; class WindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; class Factory { public: virtual Widget* create_button() = 0; virtual Widget* create_menu() = 0; }; class MotifFactory : public Factory { public: Widget* create_button() { return new MotifButton; } Widget* create_menu() { return new MotifMenu; } }; class WindowsFactory : public Factory { public: Widget* create_button() { return new WindowsButton; } Widget* create_menu() { return new WindowsMenu; } }; Factory* factory; void display_window_one() { Widget* w[] = { factory->create_button(), factory->create_menu() }; w[0]->draw(); w[1]->draw(); } void display_window_two() { Widget* w[] = { factory->create_menu(), factory->create_button() }; w[0]->draw(); w[1]->draw(); } void main() { #ifdef MOTIF factory = new MotifFactory; #else // WINDOWS factory = new WindowsFactory; #endif Widget* w = factory->create_button(); w->draw(); display_window_one(); display_window_two(); }

Abstract Factory Design Advantages C h a p t e r 3 – P a g e 22 Since the factory only provides an abstract pointer, the client doesn’t need to include any header files or class declarations relating to the concrete type. Objects of a concrete type are created by the factory, but the client accesses such objects only through their abstract interface. Adding new concrete types is done by modifying the client code to use a different factory (usually a one-line change in the code). This factory creates objects of a different concrete type, but still returns a pointer of the same abstract type as before, insulating the client from change, eliminating the need to change every location in the code where a new object is created.