• 建造者模式——java实现


    问题描述:

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

    类图:

    Java代码:

    //Computer.java
    package shiyan5;
    
    public class Computer {
        private String CPU;
        private String RAM;
        private String Hard;
        private String Host;
        public String getCPU() {
            return CPU;
        }
        public void setCPU(String cPU) {
            CPU = cPU;
        }
        public String getRAM() {
            return RAM;
        }
        public void setRAM(String rAM) {
            RAM = rAM;
        }
        public String getHard() {
            return Hard;
        }
        public void setHard(String hard) {
            Hard = hard;
        }
        public String getHost() {
            return Host;
        }
        public void setHost(String host) {
            Host = host;
        }
    }
    //ComputerBuilder.java
    package shiyan5;
    
    public abstract class ComputerBuilder {
        protected Computer computer=new Computer();
        public abstract void buildCPU();
        public abstract void buildRAM();
        public abstract void buildHard();
        public abstract void buildHost();
        public Computer getComputer()
        {
            return computer;  
        }
    }
    //DesktopComputerBuilder.java
    package shiyan5;
    
    public class DesktopComputerBuilder extends ComputerBuilder{
        public void buildCPU() {
            computer.setCPU("安装台式机CPU");
        }
        public void buildRAM() {
            computer.setRAM("安装台式机内存");
        }
        public void buildHard() {
            computer.setHard("安装台式机硬盘");
        }
        public void buildHost() {
            computer.setHost("安装台式机主机");
        }
    }
    //LaptopComputerBuilder.java
    package shiyan5;
    
    public class LaptopComputerBuilder extends ComputerBuilder{
        public void buildCPU() {
            computer.setCPU("安装笔记本CPU");
        }
        public void buildRAM() {
            computer.setRAM("安装笔记本内存");
        }
        public void buildHard() {
            computer.setHard("安装笔记本硬盘");
        }
        public void buildHost() {
            computer.setHost("安装笔记本主机");
        }
    }  
    //Director.java
    package shiyan5;
     
    public class Director {
        private ComputerBuilder cb;
        public void setComputerBuilder(ComputerBuilder c)
        {
            cb=c;
        }
        public Computer construct()
        {
            cb.buildCPU();  
            cb.buildRAM();
            cb.buildHard();
            cb.buildHost();
            return cb.getComputer();
        }
    }
    //main.java
    package shiyan5;
    
    import java.util.Scanner;
    
    public class main {
        public static void main(String args[])
        {
            ComputerBuilder cb = null;
            System.out.println("请选择:1、笔记本 2、台式机");
            Scanner input=new Scanner(System.in);
            int i=input.nextInt();
            if(i==1) {
                cb=new LaptopComputerBuilder();
            }else if(i==2) {
                cb=new DesktopComputerBuilder();
            }else {
                System.out.println("输入无效!");
            }
            Director dir=new Director();
            dir.setComputerBuilder(cb); 
            Computer computer=dir.construct();
            System.out.println("计算机组成:");
            System.out.println(computer.getCPU());
            System.out.println(computer.getRAM());
            System.out.println(computer.getHard());
            System.out.println(computer.getHost());
        }
    }

    运行结果:

  • 相关阅读:
    HBase Cassandra比较
    重新认识HBase,Cassandra列存储——本质是还是行存储,只是可以动态改变列(每行对应的数据字段)数量而已,当心不是parquet
    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
    Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
    【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
    LVM逻辑卷管理命令
    Java引进和应用的包装类
    Android 4.0新组件:GridLayout详细说明
    【剑指offer】打印单列表从尾部到头部
    原因以及如何避免产生僵尸进程
  • 原文地址:https://www.cnblogs.com/znjy/p/14166128.html
Copyright © 2020-2023  润新知