Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.

Slides:



Advertisements
Similar presentations
1 Symbian Client Server Architecture. 2 Client, who (a software module) needs service from service provider (another software module) Server, who provide.
Advertisements

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
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.
Structural Pattern: Flyweight To maximize flexibility, it is often advantageous to model objects down to very fine levels of granularity. C h a p t e.
Behavioral Pattern: Interpreter C h a p t e r 5 – P a g e 149 Some programs benefit from having a language to describe operations that they can perform.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
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,
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
Abstract Factory Design Pattern making abstract things.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
Design Pattern Course Builder Pattern By : Sajjad Zare Teacher : Dr. Noorhosseini By: Sajjad Zare 1Design Pattern.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Design patterns.
Behavioral Pattern: Command C h a p t e r 5 – P a g e 139 There are times when the need arises to issue a request to an object without knowing anything.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
Structural Design Patterns
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction.
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.
C++ Classes and Data Structures Jeffrey S. Childs
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction.
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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Advanced Object-oriented Design Patterns Creational Design Patterns.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
07 - Creational (3)CSC4071 Builder Separate the construction of a complex object from its representation so that the same construction process can create.
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.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
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 Pattern : Builder SPARCS 04 고윤정. Main Concepts One of Creational Patterns. Process to construct Complex object in Abstract type. –Use the same.
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,
Motivation for Generic Programming in C++
CS 3370 – C++ Object-oriented Programming
Creational Pattern: Prototype
Review: Two Programming Paradigms
Chapter 5 Function Basics
Introduction to Classes
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Pointers & Functions.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
C++ Compilation Model C++ is a compiled language
Inheritance: Polymorphism and Virtual Functions
Pointers & Functions.
Presentation transcript:

Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its representation. C h a p t e r 3 – P a g e 23 In this way, the same construction process may be utilized to create different representations of the object. A “director” invokes the “builder” as an interface, which creates part of the complex object each time it’s called, maintaining all intermediate states. When the product is finished, the client retrieves the result from the “builder”.

The Builder Pattern The Builder is an abstract interface for constructing objects. C h a p t e r 3 – P a g e 24 The ConcreteBuilder provides the Builder’s implementation, constructing and assembling parts to build objects. The Director is responsible for managing the correct sequence of object creation, receiving a ConcreteBuilder as a parameter and executing the necessary operations on it. The Product is the final object created by the Director, using the Builder.

Non-Software Example The Cook director uses the abstract PizzaBuilder builder to construct the Pizza product piece by piece. C h a p t e r 3 – P a g e 25 By invoking a particular concrete builder (HawaiianPizzaBuilder or SpicyPizzaBuilder), the Pizza that is produced is polymorphic (i.e., either a thin-crust, mild-sauce, ham/pineapple-topped Pizza from a HawaiianPizzaBuilder or a pan-crust, hot-sauce, sausage/pepperoni-topped Pizza from a SpicyPizzaBuilder).

Distributed Computing Example The Reader director (i.e., a parser) uses the abstract Builder to construct the DistributedWorkPackage product piece by piece (i.e., to prepare it for the server to send to the appropriate client machine). C h a p t e r 3 – P a g e 26 By invoking a particular concrete builder (UnixBuilder or VmsBuilder), the work package that is produced is polymorphic (i.e., either a flat-file, FIFO- queue, thread-pathway work package from a UnixBuilder or an indexed- sequential-file, priority-queue, lightweight-process-pathway work package from a VmsBuilder).

Work Package Builder C++ Code C h a p t e r 3 – P a g e 27 #include using namespace std; enum PersistenceType { File, Queue, Pathway }; struct PersistenceAttribute { PersistenceType type; string value; }; class DistributedWorkPackage { public: DistributedWorkPackage(string type) { cout << desc << "Distributed Work Package for: " << type; } void setFile(string filetype, string value) { cout << temp << endl << " File(" << filetype << "): " << value; desc = desc + temp; } void setQueue(string queuetype, string value) { cout << temp << endl << " Queue(" << queuetype << "): " << value; desc = desc + temp; } void setPathway(string pathwaytype, string value) { cout << temp << endl << " Pathway(" << pathwaytype << "): " << value; desc = desc + temp; } const string getState() { return desc; } private: string desc, temp; };

C h a p t e r 3 – P a g e 28 class Builder { public: virtual void configureFile(string name) = 0; virtual void configureQueue(string queue) = 0; virtual void configurePathway(string type) = 0; DistributedWorkPackage *getResult() { return result; } protected: DistributedWorkPackage *result; }; class UnixBuilder: public Builder { public: UnixBuilder() { result = new DistributedWorkPackage("Unix"); } void configureFile(string name) { result->setFile("flatFile", name); } void configureQueue(string queue) { result->setQueue("FIFO", queue); } void configurePathway(string type) { result->setPathway("thread", type); } }; class VmsBuilder: public Builder { public: VmsBuilder() { result = new DistributedWorkPackage("Vms"); } void configureFile(string name) { result->setFile("ISAM", name); } void configureQueue(string queue) { result->setQueue("priority", queue); } void configurePathway(string type) { result->setPathway("LWP", type); } }; class Reader { public: void setBuilder(Builder *b) { builder = b; } void construct(PersistenceAttribute list[], int num); private: Builder *builder; };

C h a p t e r 3 – P a g e 29 void Reader::construct(PersistenceAttribute list[], int num) { for (int i = 0; i < num; i++) if (list[i].type == File) builder->configureFile(list[i].value); else if (list[i].type == Queue) builder->configureQueue(list[i].value); else if (list[i].type == Pathway) builder->configurePathway(list[i].value); } const int NUM_ENTRIES = 6; PersistenceAttribute input[NUM_ENTRIES] = { { File, "state.dat" }, { File, "config.sys" }, { Queue, "compute" }, { Queue, "log" }, { Pathway, "authentication" }, { Pathway, "error processing" } }; void main() { UnixBuilder unixBuilder; Reader reader; reader.setBuilder(&unixBuilder); reader.construct(input, NUM_ENTRIES); cout getState() << endl << endl; VmsBuilder vmsBuilder; reader.setBuilder(&vmsBuilder); reader.construct(input, NUM_ENTRIES); cout getState() << endl << endl; }

Builder Design Advantages C h a p t e r 3 – P a g e 30 The Builder pattern ensures that all steps to create a product are carried out in the appropriate order without burdening the client with the details. At the same time, however, it affords the client the flexibility to substitute methods and components as long as the substitutions don’t damage the building process. While the Abstract Factory pattern focuses on what is being built, the Builder pattern concentrates on how it is being built. When the object being produced is particularly complex, the Builder pattern provides finer control over the step-by-step construction process.