设计模式--建造者模式

实验5:建造者模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解建造者模式的动机,掌握该模式的结构;

2、能够利用建造者模式解决实际问题。

[实验任务]:计算机组装

使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。

类图

设计模式--建造者模式_第1张图片

运行效果

设计模式--建造者模式_第2张图片 

 源代码

public class Client
{
    public static void main(String[] args)
    {
        //动态确定电脑类型
        ComputerBuilder cb = (ComputerBuilder) XMLUtil.getBean();
        //指挥者
        Director director = new Director();
        //指挥者准备组装
        director.setComputerBuilder(cb);
        //获得组装
        Computer computer = director.construct();

        System.out.println("电脑组装");
        System.out.println(computer.getCpu());
        System.out.println(computer.getMemory());
        System.out.println(computer.getHardDisk());
        System.out.println(computer.getHost());
    }
}

public class Computer
{
    private String cpu;
    private String memory;
    private String hardDisk;
    private String host;

    public String getCpu()
    {
        return cpu;
    }

    public void setCpu(String cpu)
    {
        this.cpu = cpu;
    }

    public String getMemory()
    {
        return memory;
    }

    public void setMemory(String memory)
    {
        this.memory = memory;
    }

    public String getHardDisk()
    {
        return hardDisk;
    }

    public void setHardDisk(String hardDisk)
    {
        this.hardDisk = hardDisk;
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }
}


public abstract class ComputerBuilder
{
   protected Computer computer=new Computer();

   public abstract void buildCpu();
   public abstract void buildMemory();
   public abstract void buildHardDisk();
   public abstract void buildHost();

   public Computer getComputer()
   {
       return computer;
   }
}


public class DesktopBuilder extends ComputerBuilder
{

    @Override
    public void buildCpu()
    {
        computer.setCpu("台式机装了一个Cpu");
    }

    @Override
    public void buildMemory()
    {
        computer.setMemory("台式机装了一个内存");
    }

    @Override
    public void buildHardDisk()
    {
        computer.setHardDisk("台式机装了一个硬盘");
    }

    @Override
    public void buildHost()
    {
        computer.setHost("台式机装了一个主机");
    }
}

public class Director
{
    private ComputerBuilder cb;

    public void setComputerBuilder(ComputerBuilder cb)
    {
        this.cb = cb;
    }

    public Computer construct()
    {
        cb.buildCpu();
        cb.buildMemory();
        cb.buildHardDisk();
        cb.buildHost();

        return cb.getComputer();
    }
}

public class LaptopBuilder extends ComputerBuilder
{

    @Override
    public void buildCpu()
    {
        computer.setCpu("笔记本装了一个Cpu");
    }

    @Override
    public void buildMemory()
    {
        computer.setMemory("笔记本装了一个内存");
    }

    @Override
    public void buildHardDisk()
    {
        computer.setHardDisk("笔记本装了一个硬盘");
    }

    @Override
    public void buildHost()
    {
        computer.setHost("笔记本装了一个主机");
    }
}

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class XMLUtil
{
    public static Object getBean()
    {
        try
        {
            //创建DOM文档对象
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("src/main/resources/config5.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;
        }
    }
}

config5.xml




    Tutorial_5.DesktopBuilder

 

你可能感兴趣的:(设计模式,设计模式,建造者模式)