Builder Pattern
اسلاید 1: Design Pattern Course Builder PatternBy : Sajjad ZareTeacher : Dr. NoorhosseiniBy: Sajjad Zare1Design Pattern
اسلاید 2: IntentSeparate the construction of a complex object from its representation so that the same construction process can create different representations.By: Sajjad ZareDesign 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 ZareDesign Pattern3
اسلاید 4: while( t = getToken() ){switch destinationType{ASCII://...WORD://...PDF://......}}//showConveretedResultRepresentationBy: Sajjad ZareDesign Pattern4
اسلاید 5: SolutionConfigure 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 ZareDesign Pattern5
اسلاید 6: SolutionBy: Sajjad ZareDesign Pattern6
اسلاید 7: StructureBy: Sajjad ZareDesign Pattern7
اسلاید 8: Components in Builder PatternBuilder 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 ZareDesign Pattern8
اسلاید 9: ApplicabilityThe algorithm for creating a complex object should be independent of the parts that make up the object and how theyre assembled. The construction process must allow different representations for the object thats constructed.By: Sajjad ZareDesign Pattern9
اسلاید 10: AdvantagesEncapsulate 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 ZareDesign Pattern10
اسلاید 11: Related PatternAbstract FactoryBuilder 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 ZareDesign Pattern11
اسلاید 12: Example 1By: Sajjad ZareDesign Pattern12
اسلاید 13: StructureBy: Sajjad ZareDesign Pattern13
اسلاید 14: Implementation//Productclass 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; }}//Builderinterface MealBuilder{ public void addSandwich(); public void addSideOrder(); public void addDrink(); public void setPrice(); public Meal getMeal();}By: Sajjad ZareDesign 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 ZareDesign Pattern15
اسلاید 16: Implementation//Directorclass 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.99System.out.println(mMeal); //Sandwich:DoubleBurger SideOrder:Nachos Drink:Pepsi Price:5.49}}By: Sajjad ZareDesign Pattern16
اسلاید 17: Example 2By: Sajjad ZareDesign Pattern17Media 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 ZareDesign 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 ZareDesign Pattern19
اسلاید 20: Implementationclass 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 ZareDesign Pattern20
اسلاید 21: Implementationpublic 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 ZareDesign Pattern21
اسلاید 22: ThanksBy: Sajjad ZareDesign Pattern22
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.