SlideShare ist ein Scribd-Unternehmen logo
1 von 35
WELCOME!
Presented By :
Abrar Hasin(1507009)
Fabliha Haque(1507013)
Salim Shadman Ankur(1507014)
1
Course No: CSE 3120
Shaikh Akib Shahriyar
Lecturer
Department of Computer Science & Engineering
Khulna University of Engineering & Technology
Course Name : Software Engineering & Information
Systems Laboratory
Md. Abdus Salim Mollah
Assistant Professor
Department of Computer Science & Engineering
Khulna University of Engineering & Technology
Course Teacher
2
Speaker 1
Salim Shadman Ankur
3
About Presentation
This presentation provide information to understand builder
design pattern, it’s structure, it’s implementation.
We have tried our best to explain the concept in very simple
language. The programming language used for
implementation is Java. But any one from different
programming background can easily understand the
implementation..
4
Design Pattern
A pattern is the outline of a reusable solution to a
general problem encountered in a particular
context
In software engineering, a software design
pattern is a general, reusable solution to a
commonly occurring problem within a given
context in software design.
5
Design Pattern
1
Context
2
Problem
3
Forces
4
Solution
6
Builder Design Pattern
The Builder Design Pattern is a creational design pattern,
designed to provide a flexible design solution to various
object creation problems in Object-Oriented Software.
The intent of the Builder Design Pattern is to separate the
construction of a complex object from its representation.
The process of constructing an object should be generic so
that it can be used to create different representations of the
same object.
7
Builder Design Pattern
8
A
B
C
D
Product
Builder
Concrete Builder
Director
B u i l d e r
Design Pattern
9
Speaker 2
Fabliha Haque
10
Product
11
Product
The product class defines the type of the complex object
that is to be generated by the builder pattern.
12
Builder
13
Builder
This abstract base class defines all of the steps that must
be taken in order to correctly create a product.
Each step is generally abstract as the actual functionality
of the builder is carried out in the concrete subclasses.
The GetProduct method is used to return the final product.
The builder class is often replaced with a simple interface.
14
C o n c r e t e B u i l d e r
15
Concrete Builder
There may be any number of concrete builder classes
inheriting from Builder.
These classes contain the functionality to create a
particular complex product.
16
D i r e c t o r
17
Director
The director class controls the algorithm that generates the
final product object.
A director object is instantiated and its Construct method is
called.
The method includes a parameter to capture the specific
concrete builder object that is to be used to generate the
product.
The director then calls methods of the concrete builder in
the correct order to generate the product object.
On completion of the process, the GetProduct method of
the builder object can be used to return the product.
18
Speaker 3
Abrar Hasin
19
Let’s Give an example
20
Builder Design Pattern
21
public interface Item
{
public String name();
public Packing packing();
public float price();
}
22
Packing.Java
public interface Packing
{
public String pack();
}
.
23
Wrapper.Java
public interface Packing
{
public String pack();
}
.
Create concrete classes implementing the
Packing interface
24
Bottle.Java
public class Bottle implements Packing { @Override
public String pack() { return "Bottle"; } }
25
Burger.Java
public abstract class Burger implements Item {
@Override public Packing packing() { return new
Wrapper(); } @Override public abstract float price(); }
.
Create abstract classes implementing the item
interface providing default functionalities
26
ColdDrink.Java
public abstract class ColdDrink implements Item {
@Override public Packing packing() { return new
Bottle(); } @Override public abstract float price(); }
27
Burger.Java
public class VegBurger extends Burger
{ @Override public float price() { return 25.0f; }
@Override public String name() { return "Veg
Burger";
} }
.
Create concrete classes extending Burger and
ColdDrink classes Veg
28
ChickenBurger.Java
public class ChickenBurger extends Burger {
@Override public float price() { return 50.5f; }
@Override public String name() { return "Chicken
Burger"; } }
29
Coke.Java
public class Coke extends ColdDrink { @Override
public float price() { return 30.0f; } @Override public
String name() { return "Coke"; } }
30
Pepsi.Java
public class Pepsi extends ColdDrink { @Override
public float price() { return 35.0f; } @Override public
String name() { return "Pepsi"; } }
31
Meal.Java
import java.util.ArrayList; import java.util.List; public
class Meal { private List<Item> items = new
ArrayList<Item>(); public void addItem(Item item){
items.add(item); } public float getCost(){ float cost =
0.0f; for (Item item : items) { cost += item.price(); }
return cost; } public void showItems(){ for (Item item :
items) { System.out.print("Item : " + item.name());
System.out.print(", Packing : " +
item.packing().pack()); System.out.println(", Price : "
+ item.price()); } } }
Create a Meal class having Item objects defined
above
32
MealBuilder.Java
public class MealBuilder { public Meal
prepareVegMeal (){ Meal meal = new Meal();
meal.addItem(new VegBurger()); meal.addItem(new
Coke()); return meal; } public Meal
prepareNonVegMeal (){ Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi()); return meal; } }
.
Create a MealBuilder class, the actual builder
class responsible to create Meal objects
33
BuilderPatternDemo.Java
public class BuilderPatternDemo { public static void
main(String[] args) { MealBuilder mealBuilder = new
MealBuilder(); Meal vegMeal =
mealBuilder.prepareVegMeal();
System.out.println("Veg Meal");
vegMeal.showItems(); System.out.println("Total Cost:
" + vegMeal.getCost()); Meal nonVegMeal =
mealBuilder.prepareNonVegMeal();
System.out.println("nnNon-Veg Meal");
nonVegMeal.showItems(); System.out.println("Total
Cost: " + nonVegMeal.getCost()); } }
BuiderPatternDemo uses MealBuider to
demonstrate builder pattern
34
A n y Q u e s t i o n s ? ?

35

Weitere Àhnliche Inhalte

Was ist angesagt?

Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
Rana Muhammad Asif
 

Was ist angesagt? (20)

Visual studio.net
Visual studio.netVisual studio.net
Visual studio.net
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Observer Pattern
Observer PatternObserver Pattern
Observer Pattern
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
Presentation facade design pattern
Presentation facade design patternPresentation facade design pattern
Presentation facade design pattern
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design pattern
Design patternDesign pattern
Design pattern
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 

Ähnlich wie Builder design pattern

Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
lcbj
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
Dudy Ali
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
Umar Farooq
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
Antoine Sabot-Durand
 

Ähnlich wie Builder design pattern (20)

Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Mieux programmer grĂące aux design patterns
Mieux programmer grĂące aux design patternsMieux programmer grĂące aux design patterns
Mieux programmer grĂące aux design patterns
 
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)
 
Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
 
CDI: How do I ?
CDI: How do I ?CDI: How do I ?
CDI: How do I ?
 
C# Tutorial MSM_Murach chapter-14-slides
C# Tutorial MSM_Murach chapter-14-slidesC# Tutorial MSM_Murach chapter-14-slides
C# Tutorial MSM_Murach chapter-14-slides
 
Design patterns
Design patternsDesign patterns
Design patterns
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
 

KĂŒrzlich hochgeladen

Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
amitlee9823
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
amitlee9823
 
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
gajnagarg
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
amitlee9823
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men 🔝Kolkata🔝 Escorts...
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men  🔝Kolkata🔝   Escorts...âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men  🔝Kolkata🔝   Escorts...
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men 🔝Kolkata🔝 Escorts...
amitlee9823
 

KĂŒrzlich hochgeladen (20)

Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
 
Hingoli ❀CALL GIRL 8617370543 ❀CALL GIRLS IN Hingoli ESCORT SERVICE❀CALL GIRL
Hingoli ❀CALL GIRL 8617370543 ❀CALL GIRLS IN Hingoli ESCORT SERVICE❀CALL GIRLHingoli ❀CALL GIRL 8617370543 ❀CALL GIRLS IN Hingoli ESCORT SERVICE❀CALL GIRL
Hingoli ❀CALL GIRL 8617370543 ❀CALL GIRLS IN Hingoli ESCORT SERVICE❀CALL GIRL
 
Just Call Vip call girls Nagpur Escorts ☎8617370543 Starting From 5K to 25K ...
Just Call Vip call girls Nagpur Escorts ☎8617370543 Starting From 5K to 25K ...Just Call Vip call girls Nagpur Escorts ☎8617370543 Starting From 5K to 25K ...
Just Call Vip call girls Nagpur Escorts ☎8617370543 Starting From 5K to 25K ...
 
call girls in Vasundhra (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
đŸ’«âœ…jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
đŸ’«âœ…jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...đŸ’«âœ…jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
đŸ’«âœ…jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...
âž„đŸ” 7737669865 đŸ”â–» jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
 
call girls in Kaushambi (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service 🔝...
 
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024
 
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyHire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
 
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎9352988975 Two shot with one girl (diu )
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentation
 
Sweety Planet Packaging Design Process Book.pptx
Sweety Planet Packaging Design Process Book.pptxSweety Planet Packaging Design Process Book.pptx
Sweety Planet Packaging Design Process Book.pptx
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men 🔝Kolkata🔝 Escorts...
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men  🔝Kolkata🔝   Escorts...âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men  🔝Kolkata🔝   Escorts...
âž„đŸ” 7737669865 đŸ”â–» Kolkata Call-girls in Women Seeking Men 🔝Kolkata🔝 Escorts...
 

Builder design pattern

  • 1. WELCOME! Presented By : Abrar Hasin(1507009) Fabliha Haque(1507013) Salim Shadman Ankur(1507014) 1
  • 2. Course No: CSE 3120 Shaikh Akib Shahriyar Lecturer Department of Computer Science & Engineering Khulna University of Engineering & Technology Course Name : Software Engineering & Information Systems Laboratory Md. Abdus Salim Mollah Assistant Professor Department of Computer Science & Engineering Khulna University of Engineering & Technology Course Teacher 2
  • 4. About Presentation This presentation provide information to understand builder design pattern, it’s structure, it’s implementation. We have tried our best to explain the concept in very simple language. The programming language used for implementation is Java. But any one from different programming background can easily understand the implementation.. 4
  • 5. Design Pattern A pattern is the outline of a reusable solution to a general problem encountered in a particular context In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. 5
  • 7. Builder Design Pattern The Builder Design Pattern is a creational design pattern, designed to provide a flexible design solution to various object creation problems in Object-Oriented Software. The intent of the Builder Design Pattern is to separate the construction of a complex object from its representation. The process of constructing an object should be generic so that it can be used to create different representations of the same object. 7
  • 12. Product The product class defines the type of the complex object that is to be generated by the builder pattern. 12
  • 14. Builder This abstract base class defines all of the steps that must be taken in order to correctly create a product. Each step is generally abstract as the actual functionality of the builder is carried out in the concrete subclasses. The GetProduct method is used to return the final product. The builder class is often replaced with a simple interface. 14
  • 15. C o n c r e t e B u i l d e r 15
  • 16. Concrete Builder There may be any number of concrete builder classes inheriting from Builder. These classes contain the functionality to create a particular complex product. 16
  • 17. D i r e c t o r 17
  • 18. Director The director class controls the algorithm that generates the final product object. A director object is instantiated and its Construct method is called. The method includes a parameter to capture the specific concrete builder object that is to be used to generate the product. The director then calls methods of the concrete builder in the correct order to generate the product object. On completion of the process, the GetProduct method of the builder object can be used to return the product. 18
  • 20. Let’s Give an example 20
  • 22. public interface Item { public String name(); public Packing packing(); public float price(); } 22
  • 24. Wrapper.Java public interface Packing { public String pack(); } . Create concrete classes implementing the Packing interface 24
  • 25. Bottle.Java public class Bottle implements Packing { @Override public String pack() { return "Bottle"; } } 25
  • 26. Burger.Java public abstract class Burger implements Item { @Override public Packing packing() { return new Wrapper(); } @Override public abstract float price(); } . Create abstract classes implementing the item interface providing default functionalities 26
  • 27. ColdDrink.Java public abstract class ColdDrink implements Item { @Override public Packing packing() { return new Bottle(); } @Override public abstract float price(); } 27
  • 28. Burger.Java public class VegBurger extends Burger { @Override public float price() { return 25.0f; } @Override public String name() { return "Veg Burger"; } } . Create concrete classes extending Burger and ColdDrink classes Veg 28
  • 29. ChickenBurger.Java public class ChickenBurger extends Burger { @Override public float price() { return 50.5f; } @Override public String name() { return "Chicken Burger"; } } 29
  • 30. Coke.Java public class Coke extends ColdDrink { @Override public float price() { return 30.0f; } @Override public String name() { return "Coke"; } } 30
  • 31. Pepsi.Java public class Pepsi extends ColdDrink { @Override public float price() { return 35.0f; } @Override public String name() { return "Pepsi"; } } 31
  • 32. Meal.Java import java.util.ArrayList; import java.util.List; public class Meal { private List<Item> items = new ArrayList<Item>(); public void addItem(Item item){ items.add(item); } public float getCost(){ float cost = 0.0f; for (Item item : items) { cost += item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.print("Item : " + item.name()); System.out.print(", Packing : " + item.packing().pack()); System.out.println(", Price : " + item.price()); } } } Create a Meal class having Item objects defined above 32
  • 33. MealBuilder.Java public class MealBuilder { public Meal prepareVegMeal (){ Meal meal = new Meal(); meal.addItem(new VegBurger()); meal.addItem(new Coke()); return meal; } public Meal prepareNonVegMeal (){ Meal meal = new Meal(); meal.addItem(new ChickenBurger()); meal.addItem(new Pepsi()); return meal; } } . Create a MealBuilder class, the actual builder class responsible to create Meal objects 33
  • 34. BuilderPatternDemo.Java public class BuilderPatternDemo { public static void main(String[] args) { MealBuilder mealBuilder = new MealBuilder(); Meal vegMeal = mealBuilder.prepareVegMeal(); System.out.println("Veg Meal"); vegMeal.showItems(); System.out.println("Total Cost: " + vegMeal.getCost()); Meal nonVegMeal = mealBuilder.prepareNonVegMeal(); System.out.println("nnNon-Veg Meal"); nonVegMeal.showItems(); System.out.println("Total Cost: " + nonVegMeal.getCost()); } } BuiderPatternDemo uses MealBuider to demonstrate builder pattern 34
  • 35. A n y Q u e s t i o n s ? ?  35

Hinweis der Redaktion

  1. No slide master