Design Pattern Course Builder Pattern By : Sajjad Zare Teacher : Dr. Noorhosseini By: Sajjad Zare 1Design Pattern.

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.
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.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
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.
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 ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
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.
Design Patterns.
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.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.
Software Components Creational Patterns.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Factory Patterns. RHS – SOC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
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.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
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.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
Builder Compositional Design – with a twist…. Problem Consider your favorite –Text editor, word processor, spreadsheet, drawing tool They allow editing.
Advanced Object-oriented Design Patterns Creational Design Patterns.
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.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
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.
By Robert Smith Feb 23,  The normal method of dealing with an abstraction having several implementations is through inheritance.  However, this.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Factory Patterns. DCS – SWC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
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.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
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.
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Working with Java.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
Chapter 3: Using Methods, Classes, and Objects
Chapter 8, Design Patterns Builder
Introduction to Java Programming
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Introduction to Data Structure
Design pattern Lecture 6.
Creational Patterns.
Software Design Lecture : 39.
Software Engineering and Architecture
Software Design Lecture : 28.
Presentation transcript:

Design Pattern Course Builder Pattern By : Sajjad Zare Teacher : Dr. Noorhosseini By: Sajjad Zare 1Design Pattern

Intent  Separate the construction of a complex object from its representation so that the same construction process can create different representations. By: Sajjad Zare Design Pattern2

Example  A reader for the RTF (Rich Text Format) document be able to convert RTF to many text formats.  The reader might convert RTF documents into plain ASCII text or into a text widget that can be edited interactively.  The problem is that the number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader. By: Sajjad Zare Design Pattern3

while( t = getToken() ) { switch destinationType { ASCII: //... WORD: //... PDF: // } //showConveretedResultRepresentation By: Sajjad Zare Design Pattern4

Solution  Configure the RTFReader class with a TextConverter object that converts RTF to another textual representation.  Whenever the RTFReader recognizes an RTF token, it issues a request to the TextConverter to convert the token.  TextConverter objects are responsible both for performing the data conversion and for representing the token in a particular format. By: Sajjad Zare Design Pattern5

Solution By: Sajjad Zare Design Pattern6

Structure By: Sajjad Zare Design Pattern7

Components in Builder Pattern  Builder Abstract interface for creating objects (product).  Concrete Builder Provide implementation for Builder. Construct and assemble parts to build the objects.  Director The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.  Product The final object that will be created by the Director using Builder. By: Sajjad Zare Design Pattern8

Applicability  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. By: Sajjad Zare Design Pattern9

Advantages  Encapsulate the way a complex object is constructed.  Allows objects to be constructed in a multi-step processes.  Hides the internal representation of the product from the client.  Product implementation can be swapped in and out because the client only sees the abstract interface. By: Sajjad Zare Design Pattern10

Related Pattern  Abstract Factory Builder focuses on constructing a complex object step by step while Abstract Factory emphasizes a family of product objects The builder pattern encapsulates the logic of how to put together a complex object so that the client just requests a configuration and the builder directs the logic of building it. The Abstract factory pattern defers the choice of what concrete type of object to make. By: Sajjad Zare Design Pattern11

Example 1 By: Sajjad Zare Design Pattern12

Structure By: Sajjad Zare Design Pattern13

Implementation //Product class Meal { public String sandwich ; public String sideOrder ; public String drink ; public String offer ; public double price ; public String toString() { return "Sandwich:"+sandwich+ " SideOrder:"+sideOrder+ " Drink:"+drink+" Price:"+price; } //Builder interface MealBuilder { public void addSandwich(); public void addSideOrder(); public void addDrink(); public void setPrice(); public Meal getMeal(); } By: Sajjad Zare Design Pattern14

Implementation //concreteBuilder 1 class JapanianMealBuilder implements MealBuilder { private Meal meal = new Meal(); public void addSandwich() { meal.sandwich = "Burger"; } public void addSideOrder() { meal.sideOrder = "Fries"; } public void addDrink() { meal.drink = "Cola"; } public void setPrice() { meal.price = 4.99; } public Meal getMeal() { return meal; } } //concreteBuilder 2 class MexicanMealBuilder implements MealBuilder { private Meal meal = new Meal(); public void addSandwich() { meal.sandwich = "DoubleBurger"; } public void addSideOrder() { meal.sideOrder = "Nachos"; } public void addDrink() { meal.drink = "Pepsi"; } public void setPrice() { meal.price = 5.49; } public Meal getMeal() { return meal; } } By: Sajjad Zare Design Pattern15

Implementation //Director class MealDirector { public void makeMeal(MealBuilder mealBuilder) { mealBuilder.addSandwich(); mealBuilder.addSideOrder(); mealBuilder.addDrink(); mealBuilder.setPrice(); } public class MealUse { public static void main(String[] args) { MealDirector director = new MealDirector(); MealBuilder jmb = new JapanianMealBuilder(); director.makeMeal(jmb); Meal jMeal = jmb.getMeal(); MealBuilder mmb = new MexicanMealBuilder(); director.makeMeal(mmb); Meal mMeal = mmb.getMeal(); System.out.println(jMeal); //Sandwich:Burger SideOrder:Fries Drink:Cola Price:4.99 System.out.println(mMeal); //Sandwich:DoubleBurger SideOrder:Nachos Drink:Pepsi Price:5.49 } By: Sajjad Zare Design Pattern16

Example 2 By: Sajjad Zare Design Pattern17  Media may be constructed into different representations, in this case books, magazines.

Implementation // Different "representations" of media: class Media extends ArrayList {} class Book extends Media {} class Magazine extends Media {} //... contain different kinds of media items: class MediaItem { private String s; public MediaItem(String s) { this.s = s; } public String toString() { return s; } } class Chapter extends MediaItem { public Chapter(String s) { super(s); } } class Article extends MediaItem { public Article(String s) { super(s); } } By: Sajjad Zare Design Pattern18

Implementation //... but use the same basic construction steps: class MediaBuilder { public void buildBase() {} public void addMediaItem(MediaItem item) {} public Media getFinishedMedia() { return null; } } class BookBuilder extends MediaBuilder { private Book b; public void buildBase() { System.out.println("Building book framework"); b = new Book(); } public void addMediaItem(MediaItem chapter) { System.out.println("Adding chapter " + chapter); b.add(chapter); } public Media getFinishedMedia() { return b; } } By: Sajjad Zare Design Pattern19

Implementation class MagazineBuilder extends MediaBuilder { private Magazine m; public void buildBase() { System.out.println("Building magazine framework"); m = new Magazine(); } public void addMediaItem(MediaItem article) { System.out.println("Adding article " + article); m.add(article); } public Media getFinishedMedia() { return m; } } class MediaDirector { private MediaBuilder mb; public MediaDirector(MediaBuilder mb) { this.mb = mb; // buider } public Media produceMedia(List input) { mb.buildBase(); for(Iterator it = input.iterator(); it.hasNext();) mb.addMediaItem((MediaItem)it.next()); return mb.getFinishedMedia(); } }; By: Sajjad Zare Design Pattern20

Implementation public class BuildMedia { public static void main(String[] args){ private List input = Arrays.asList(new MediaItem[] { new MediaItem("item1"), new MediaItem("item2"), new MediaItem("item3"), new MediaItem("item4"), }); MediaDirector buildBook = new MediaDirector(new BookBuilder()); Media book = buildBook.produceMedia(input); String result = "book: " + book; System.out.println(result);//"book: [item1, item2, item3, item4]“ MediaDirector buildMagazine = new MediaDirector(new MagazineBuilder()); Media magazine = buildMagazine.produceMedia(input); String result = "magazine: " + magazine; System.out.println(result); //"magazine: [item1, item2, item3, item4]“ } By: Sajjad Zare Design Pattern21

Thanks By: Sajjad Zare Design Pattern22