Builder Pattern说:“使用逐步方法从简单对象构造复杂对象”
它通常用于无法在单个步骤中创建对象(如对复杂对象进行反序列化)的情况。
建造者设计模式的优势
构建器模式的主要优点如下:
- 它在对象的构造和表示之间提供了清晰的分隔。
- 它可以更好地控制施工过程。
- 它支持更改对象的内部表示。
用于构建器模式的UML示例
生成器设计模式示例
要创建构建器设计模式的简单示例, 你需要执行以下6个步骤。
- 创建打包界面
- 创建2个抽象类CD和Company
- 创建公司的2个实施类:Sony和Samsung
- 创建CDType类
- 创建CDBuilder类
- 创建BuilderDemo类
1)创建打包界面
public interface Packing {
public String pack();
public int price();
}
2)创建2个抽象类CD和Company
创建一个抽象类CD, 它将实现Packing接口。
public abstract class CD implements Packing{
public abstract String pack();
}
public abstract class Company extends CD{
public abstract int price();
}
3)创建公司的两个实现类:索尼和三星
public class Sony extends Company{
@Override
public int price(){
return 20;
}
@Override
public String pack(){
return "Sony CD";
}
}//End of the Sony class.
public class Samsung extends Company {
@Override
public int price(){
return 15;
}
@Override
public String pack(){
return "Samsung CD";
}
}//End of the Samsung class.
4)创建CDType类
import java.util.ArrayList;
import java.util.List;
public class CDType {
private List<Packing> items=new ArrayList<Packing>();
public void addItem(Packing packs) {
items.add(packs);
}
public void getCost(){
for (Packing packs : items) {
packs.price();
}
}
public void showItems(){
for (Packing packing : items){
System.out.print("CD name : "+packing.pack());
System.out.println(", Price : "+packing.price());
}
}
}//End of the CDType class.
5)创建CDBuilder类
public class CDBuilder {
public CDType buildSonyCD(){
CDType cds=new CDType();
cds.addItem(new Sony());
return cds;
}
public CDType buildSamsungCD(){
CDType cds=new CDType();
cds.addItem(new Samsung());
return cds;
}
}// End of the CDBuilder class.
6)创建BuilderDemo类
public class BuilderDemo{
public static void main(String args[]){
CDBuilder cdBuilder=new CDBuilder();
CDType cdType1=cdBuilder.buildSonyCD();
cdType1.showItems();
CDType cdType2=cdBuilder.buildSamsungCD();
cdType2.showItems();
}
}
上面例子的输出
CD name : Sony CD, Price : 20
CD name : Samsung CD, Price : 15
构建器模式的另一个真实示例
用于构建器模式的UML:
我们正在考虑一个必胜客的商业案例, 从中可以得到不同种类的匹萨和冷饮。
披萨可以是多种类型的Veg比萨或非Veg披萨(例如奶酪比萨, 洋葱比萨, masala-pizza等), 并具有4种尺寸, 即小, 中, 大, 特大。
冷饮可以有几种类型(例如百事可乐, 可乐, 露水, 雪碧, 芬达, 马萨酒, 林卡, Thums-up等), 将分为3种尺寸, 小, 中, 大。
建造者模式的真实例子
让我们逐步了解Builder设计模式的真实示例。
步骤1:创建一个界面项目, 代表比萨饼和冷饮。
public interface Item
{
public String name();
public String size();
public float price();
}// End of the interface Item.
步骤2:创建一个抽象类Pizza, 该类将实现到接口Item。
public abstract class Pizza implements Item{
@Override
public abstract float price();
}
步骤3:创建一个抽象类ColdDrink, 该类将实现到接口Item。
public abstract class ColdDrink implements Item{
@Override
public abstract float price();
}
步骤4:创建一个抽象类VegPizza, 它将扩展到抽象类Pizza。
public abstract class VegPizza extends Pizza{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}// End of the abstract class VegPizza.
步骤5:创建一个抽象类NonVegPizza, 它将扩展到抽象类Pizza。
public abstract class NonVegPizza extends Pizza{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}// End of the abstract class NonVegPizza.
步骤6:现在, 创建具体的子类SmallCheezePizza, MediumCheezePizza, LargeCheezePizza, ExtraLargeCheezePizza, 这些子类将扩展到抽象类VegPizza。
public class SmallCheezePizza extends VegPizza{
@Override
public float price() {
return 170.0f;
}
@Override
public String name() {
return "Cheeze Pizza";
}
@Override
public String size() {
return "Small size";
}
}// End of the SmallCheezePizza class.
public class MediumCheezePizza extends VegPizza{
@Override
public float price() {
return 220.f;
}
@Override
public String name() {
return "Cheeze Pizza";
}
@Override
public String size() {
return "Medium Size";
}
}// End of the MediumCheezePizza class.
</textaera></div>
<div id="filename">File: LargeCheezePizza.java</div>
<div class="codeblock"><textarea name="code" class="java">
public class LargeCheezePizza extends VegPizza{
@Override
public float price() {
return 260.0f;
}
@Override
public String name() {
return "Cheeze Pizza";
}
@Override
public String size() {
return "Large Size";
}
}// End of the LargeCheezePizza class.
public class ExtraLargeCheezePizza extends VegPizza{
@Override
public float price() {
return 300.f;
}
@Override
public String name() {
return "Cheeze Pizza";
}
@Override
public String size() {
return "Extra-Large Size";
}
}// End of the ExtraLargeCheezePizza class.
步骤7:现在, 类似地创建具体的子类SmallOnionPizza, MediumOnionPizza, LargeOnionPizza, ExtraLargeOnionPizza, 这些子类将扩展到抽象类VegPizza。
public class SmallOnionPizza extends VegPizza {
@Override
public float price() {
return 120.0f;
}
@Override
public String name() {
return "Onion Pizza";
}
@Override
public String size() {
return "Small Size";
}
}// End of the SmallOnionPizza class.
public class MediumOnionPizza extends VegPizza {
@Override
public float price() {
return 150.0f;
}
@Override
public String name() {
return "Onion Pizza";
}
@Override
public String size() {
return "Medium Size";
}
}// End of the MediumOnionPizza class.
public class LargeOnionPizza extends VegPizza{
@Override
public float price() {
return 180.0f;
}
@Override
public String name() {
return "Onion Pizza";
}
@Override
public String size() {
return "Large size";
}
}// End of the LargeOnionPizza class.
public class ExtraLargeOnionPizza extends VegPizza {
@Override
public float price() {
return 200.0f;
}
@Override
public String name() {
return "Onion Pizza";
}
@Override
public String size() {
return "Extra-Large Size";
}
}// End of the ExtraLargeOnionPizza class
步骤8:现在, 类似地创建具体的子类SmallMasalaPizza, MediumMasalaPizza, LargeMasalaPizza, ExtraLargeMasalaPizza, 这些子类将扩展到抽象类VegPizza。
public class SmallMasalaPizza extends VegPizza{
@Override
public float price() {
return 100.0f;
}
@Override
public String name() {
return "Masala Pizza";
}
@Override
public String size() {
return "Samll Size";
}
}// End of the SmallMasalaPizza class
public class MediumMasalaPizza extends VegPizza {
@Override
public float price() {
return 120.0f;
}
@Override
public String name() {
return "Masala Pizza";
}
@Override
public String size() {
return "Medium Size";
}
public class LargeMasalaPizza extends VegPizza{
@Override
public float price() {
return 150.0f;
}
@Override
public String name() {
return "Masala Pizza";
}
@Override
public String size() {
return "Large Size";
}
} //End of the LargeMasalaPizza class
public class ExtraLargeMasalaPizza extends VegPizza {
@Override
public float price() {
return 180.0f;
}
@Override
public String name() {
return "Masala Pizza";
}
@Override
public String size() {
return "Extra-Large Size";
}
}// End of the ExtraLargeMasalaPizza class
步骤9:现在, 创建具体的子类SmallNonVegPizza, MediumNonVegPizza, LargeNonVegPizza, ExtraLargeNonVegPizza, 这些子类将扩展到抽象类NonVegPizza。
public class SmallNonVegPizza extends NonVegPizza {
@Override
public float price() {
return 180.0f;
}
@Override
public String name() {
return "Non-Veg Pizza";
}
@Override
public String size() {
return "Samll Size";
}
}// End of the SmallNonVegPizza class
public class MediumNonVegPizza extends NonVegPizza{
@Override
public float price() {
return 200.0f;
}
@Override
public String name() {
return "Non-Veg Pizza";
}
@Override
public String size() {
return "Medium Size";
}
public class LargeNonVegPizza extends NonVegPizza{
@Override
public float price() {
return 220.0f;
}
@Override
public String name() {
return "Non-Veg Pizza";
}
@Override
public String size() {
return "Large Size";
}
}// End of the LargeNonVegPizza class
public class ExtraLargeNonVegPizza extends NonVegPizza {
@Override
public float price() {
return 250.0f;
}
@Override
public String name() {
return "Non-Veg Pizza";
}
@Override
public String size() {
return "Extra-Large Size";
}
}
// End of the ExtraLargeNonVegPizza class
步骤10:现在, 创建两个抽象类Pepsi和Coke, 它们将扩展抽象类ColdDrink。
public abstract class Pepsi extends ColdDrink {
@Override
public abstract String name();
@Override
public abstract String size();
@Override
public abstract float price();
}// End of the Pepsi class
public abstract class Coke extends ColdDrink {
@Override
public abstract String name();
@Override
public abstract String size();
@Override
public abstract float price();
}// End of the Coke class
</textaea></div>
<p>Step 11:<b>Now, create concrete sub-classes SmallPepsi, MediumPepsi, LargePepsi that will extend to the abstract class Pepsi.</b></p>
<div id="filename">File: SmallPepsi.java</div>
<div class="codeblock"><textarea name="code" class="java">
public class SmallPepsi extends Pepsi{
@Override
public String name() {
return "300 ml Pepsi";
}
@Override
public float price() {
return 25.0f;
}
@Override
public String size() {
return "Small Size";
}
}// End of the SmallPepsi class
public class MediumPepsi extends Pepsi {
@Override
public String name() {
return "500 ml Pepsi";
}
@Override
public String size() {
return "Medium Size";
}
@Override
public float price() {
return 35.0f;
}
}// End of the MediumPepsi class
public class LargePepsi extends Pepsi{
@Override
public String name() {
return "750 ml Pepsi";
}
@Override
public String size() {
return "Large Size";
}
@Override
public float price() {
return 50.0f;
}
}// End of the LargePepsi class
步骤12:现在, 创建具体的子类SmallCoke, MediumCoke, LargeCoke, 这些子类将扩展到抽象类Coke。
public class SmallCoke extends Coke{
@Override
public String name() {
return "300 ml Coke";
}
@Override
public String size() {
return "Small Size";
}
@Override
public float price() {
return 25.0f;
}
}// End of the SmallCoke class
public class MediumCoke extends Coke{
@Override
public String name() {
return "500 ml Coke";
}
@Override
public String size() {
return "Medium Size";
}
@Override
public float price() {
return 35.0f;
}
}// End of the MediumCoke class
public class LargeCoke extends Coke {
@Override
public String name() {
return "750 ml Coke";
}
@Override
public String size() {
return "Large Size";
}
@Override
public float price() {
return 50.0f;
}
}// End of the LargeCoke class
</textrea></div>
<p>Step 13:<b>Create an OrderedItems class that are having Item objects defined above.</b></p>
<div id="filename">File: OrderedItems.java</div>
<div class="codeblock"><textarea name="code" class="java">
import java.util.ArrayList;
import java.util.List;
public class OrderedItems {
List<Item> items=new ArrayList<Item>();
public void addItems(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.println("Item is:" +item.name());
System.out.println("Size is:" +item.size());
System.out.println("Price is: " +item.price());
}
}
}// End of the OrderedItems class
步骤14:创建一个OrderBuilder类, 该类将负责创建OrderedItems类的对象。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderBuilder {
public OrderedItems preparePizza() throws IOException{
OrderedItems itemsOrder=new OrderedItems();
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the choice of Pizza ");
System.out.println("============================");
System.out.println(" 1. Veg-Pizza ");
System.out.println(" 2. Non-Veg Pizza ");
System.out.println(" 3. Exit ");
System.out.println("============================");
int pizzaandcolddrinkchoice=Integer.parseInt(br.readLine());
switch(pizzaandcolddrinkchoice)
{
case 1:{
System.out.println("You ordered Veg Pizza");
System.out.println("\n\n");
System.out.println(" Enter the types of Veg-Pizza ");
System.out.println("------------------------------");
System.out.println(" 1.Cheeze Pizza ");
System.out.println(" 2.Onion Pizza ");
System.out.println(" 3.Masala Pizza ");
System.out.println(" 4.Exit ");
System.out.println("------------------------------");
int vegpizzachoice=Integer.parseInt(br.readLine());
switch(vegpizzachoice)
{
case 1:
{
System.out.println("You ordered Cheeze Pizza");
System.out.println("Enter the cheeze pizza size");
System.out.println("------------------------------------");
System.out.println(" 1. Small Cheeze Pizza ");
System.out.println(" 2. Medium Cheeze Pizza ");
System.out.println(" 3. Large Cheeze Pizza ");
System.out.println(" 4. Extra-Large Cheeze Pizza ");
System.out.println("------------------------------------");
int cheezepizzasize=Integer.parseInt(br.readLine());
switch(cheezepizzasize)
{
case 1:
itemsOrder.addItems(new SmallCheezePizza());
break;
case 2:
itemsOrder.addItems(new MediumCheezePizza());
break;
case 3:
itemsOrder.addItems(new LargeCheezePizza());
break;
case 4:
itemsOrder.addItems(new ExtraLargeCheezePizza());
break;
case 2:
{
System.out.println("You ordered Onion Pizza");
System.out.println("Enter the Onion pizza size");
System.out.println("----------------------------------");
System.out.println(" 1. Small Onion Pizza ");
System.out.println(" 2. Medium Onion Pizza ");
System.out.println(" 3. Large Onion Pizza ");
System.out.println(" 4. Extra-Large Onion Pizza ");
System.out.println("----------------------------------");
int onionpizzasize=Integer.parseInt(br.readLine());
switch(onionpizzasize)
{
case 1:
itemsOrder.addItems(new SmallOnionPizza());
break;
case 2:
itemsOrder.addItems(new MediumOnionPizza());
break;
case 3:
itemsOrder.addItems(new LargeOnionPizza());
break;
case 4:
itemsOrder.addItems(new ExtraLargeOnionPizza());
break;
}
}
break;
case 3:
{
System.out.println("You ordered Masala Pizza");
System.out.println("Enter the Masala pizza size");
System.out.println("------------------------------------");
System.out.println(" 1. Small Masala Pizza ");
System.out.println(" 2. Medium Masala Pizza ");
System.out.println(" 3. Large Masala Pizza ");
System.out.println(" 4. Extra-Large Masala Pizza ");
System.out.println("------------------------------------");
int masalapizzasize=Integer.parseInt(br.readLine());
switch(masalapizzasize)
{
case 1:
itemsOrder.addItems(new SmallMasalaPizza());
break;
case 2:
itemsOrder.addItems(new MediumMasalaPizza());
break;
case 3:
itemsOrder.addItems(new LargeMasalaPizza());
break;
case 4:
itemsOrder.addItems(new ExtraLargeMasalaPizza());
break;
}
}
break;
}
}
break;// Veg- pizza choice completed.
case 2:
{
System.out.println("You ordered Non-Veg Pizza");
System.out.println("\n\n");
System.out.println("Enter the Non-Veg pizza size");
System.out.println("------------------------------------");
System.out.println(" 1. Small Non-Veg Pizza ");
System.out.println(" 2. Medium Non-Veg Pizza ");
System.out.println(" 3. Large Non-Veg Pizza ");
System.out.println(" 4. Extra-Large Non-Veg Pizza ");
System.out.println("------------------------------------");
int nonvegpizzasize=Integer.parseInt(br.readLine());
switch(nonvegpizzasize)
{
case 1:
itemsOrder.addItems(new SmallNonVegPizza());
break;
case 2:
itemsOrder.addItems(new MediumNonVegPizza());
break;
case 3:
itemsOrder.addItems(new LargeNonVegPizza());
break;
case 4:
itemsOrder.addItems(new ExtraLargeNonVegPizza());
break;
}
}
break;
default:
{
break;
}
}//end of main Switch
//continued?..
System.out.println(" Enter the choice of ColdDrink ");
System.out.println("============================");
System.out.println(" 1. Pepsi ");
System.out.println(" 2. Coke ");
System.out.println(" 3. Exit ");
System.out.println("============================");
int coldDrink=Integer.parseInt(br.readLine());
switch (coldDrink)
{
case 1:
{
System.out.println("You ordered Pepsi ");
System.out.println("Enter the Pepsi Size ");
System.out.println("------------------------");
System.out.println(" 1. Small Pepsi ");
System.out.println(" 2. Medium Pepsi ");
System.out.println(" 3. Large Pepsi ");
System.out.println("------------------------");
int pepsisize=Integer.parseInt(br.readLine());
switch(pepsisize)
{
case 1:
itemsOrder.addItems(new SmallPepsi());
break;
case 2:
itemsOrder.addItems(new MediumPepsi());
break;
case 3:
itemsOrder.addItems(new LargePepsi());
break;
}
}
break;
case 2:
{
System.out.println("You ordered Coke");
System.out.println("Enter the Coke Size");
System.out.println("------------------------");
System.out.println(" 1. Small Coke ");
System.out.println(" 2. Medium Coke ");
System.out.println(" 3. Large Coke ");
System.out.println(" 4. Extra-Large Coke ");
System.out.println("------------------------");
int cokesize=Integer.parseInt(br.readLine());
switch(cokesize)
{
case 1:
itemsOrder.addItems(new SmallCoke());
break;
case 2:
itemsOrder.addItems(new MediumCoke());
break;
case 3:
itemsOrder.addItems(new LargeCoke());
break;
}
}
break;
default:
{
break;
}
}//End of the Cold-Drink switch
return itemsOrder;
} //End of the preparePizza() method
第15步:创建一个将使用OrderBuilder类的BuilderDemo类。
import java.io.IOException;
public class BuilderDemo {
public static void main(String[] args) throws IOException {
// TODO code application logic here
OrderBuilder builder=new OrderBuilder();
OrderedItems orderedItems=builder.preparePizza();
orderedItems.showItems();
System.out.println("\n");
System.out.println("Total Cost : "+ orderedItems.getCost());
}
}// End of the BuilderDemo class
评论前必须登录!
注册