设计模式-建造者模式实例-01

设计模式-建造者模式实例-01

要点:

创建一个复杂对象。

优点:

客户端不必知道产品内部组成细节,将产品本身和产品创建过程解耦,使相同创建过程可创建不同产品对象;

具体建造者相对独立,方便替换和增加,符合开闭原则;

可以更精细地控制产品创建过程。

缺点:

产品间差异性大的时候不适用;

内部变化复杂的话需要定义很多具体建造者类来实现变化。

适用环境:

将一个复杂对象的构建与它的表示分离,使同样的构造过程可以创建不同的表示。

实例1:KFC套餐AB的创建。

类图

 

 

代码实现:

代码结构:

 

Meal.java:

//产品类Meal(套餐类)
public class Meal {
    private String food;
    private String drink;

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public String getDrink() {
        return drink;
    }

    public void setDrink(String drink) {
        this.drink = drink;
    }
}

 

MealBuilder.java:

//套餐建造者类
public abstract class MealBuilder {
    protected Meal meal = new Meal();
    public abstract void buildFood();
    public abstract void buildDrink();

    public Meal getMeal(){
        return meal;
    }
}

 

SubMealBuilder:

SubMealBuilderA.java:

import BuilderPattern.MealBuilder;

public class SubMealBuilderA extends MealBuilder {
    public void buildFood(){
        meal.setFood("set a drumsticks!");
    }
    public void buildDrink(){
        meal.setDrink("set a cola!");
    }
}

 

SubMealBuilderB.java:

import BuilderPattern.MealBuilder;

public class SubMealBuilderB extends MealBuilder {
    public void buildFood(){
        meal.setFood("set a Chicken Rolls");
    }
    public void buildDrink(){
        meal.setDrink("set a juice");
    }
}

 

KFCWaiter.java:

public class KFCWaiter {
    private MealBuilder mb;
    public void setMealBuilder(MealBuilder mb){
        this.mb = mb;
    }

    public Meal construct(){
        mb.buildFood();
        mb.buildDrink();
        return mb.getMeal();
    }
}

 

XMLUtils.java:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import java.io.*;

    public class XMLUtils {
        public static Object getBean(){
            try {
                //创建文档对象
                DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = dFactory.newDocumentBuilder();
                Document doc;
                doc = builder.parse(new File("src/BuilderPattern/utils/config.xml"));
                //获取包含支付方式名称的文本节点
                NodeList nl = doc.getElementsByTagName("className");
                Node classNode=nl.item(0).getFirstChild();
                String cName=classNode.getNodeValue();
                //通过类名生成实例对象并将其返回
                Class c=Class.forName(cName);
                Object obj=c.newInstance();
                return obj;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    }

 

config.xml:

<?xml version="1.0"?>
<config>
    <className>BuilderPattern.SubMealBuilder.SubMealBuilderA</className>
</config>

 

Client.java:

import BuilderPattern.utils.XMLUtils;
import abstractTVFactory.utils.XMLUtil;

public class Client {
    public static void main(String[] args) {
        //动态确定套餐种类
        MealBuilder mb = (MealBuilder) XMLUtils.getBean();
        //服务员是指挥者
        KFCWaiter waiter = new KFCWaiter();
        //服务员准备套餐
        waiter.setMealBuilder(mb);
        //客户获得套餐
        Meal meal = waiter.construct();

        System.out.println("套餐组成:");
        System.out.println(meal.getFood());
        System.out.println(meal.getDrink());

    }
}

测试结果:

 

 

 

 
 
posted @ 2022-03-09 22:37  临易  阅读(61)  评论(0编辑  收藏  举报