Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Design Pattern Course Builder Pattern By : Sajjad Zare Teacher : Dr. Noorhosseini By: Sajjad Zare 1Design Pattern."— Presentation transcript:

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

2 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

3 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

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

5 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

6 Solution By: Sajjad Zare Design Pattern6

7 Structure By: Sajjad Zare Design Pattern7

8 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

9 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

10 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

11 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

12 Example 1 By: Sajjad Zare Design Pattern12

13 Structure By: Sajjad Zare Design Pattern13

14 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

15 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

16 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

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

18 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

19 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

20 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

21 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

22 Thanks By: Sajjad Zare Design Pattern22


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

Similar presentations


Ads by Google