首页 > 其他分享 >Tutorial 5_建造者模式

Tutorial 5_建造者模式

时间:2022-10-17 11:23:16浏览次数:45  
标签:return String Builder void 建造 模式 public Tutorial string

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

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

 

 

2.1 Builder.java
package jianzaozhe;

public abstract class Builder {

    Computer computer = new Computer();

    abstract public void buildZhuJi();

    abstract public void buildYinPan();

    abstract public void buildCpu();

    abstract public void buildNeiCun();

    public Computer getComputer(){

        return computer;

    }

}

2.2 Computer.java
package jianzaozhe;

public class Computer {

    private String cpu;
    private String neiCun;
    private String yinPan;
    private String zhuJi;

    public String getZhuJi() {

        return zhuJi;

    }

    public void setZhuJi(String zhuJi) {

        this.zhuJi = zhuJi;

    }

    public String getYinPan() {

        return yinPan;

    }

    public void setYinPan(String yinPan) {

        this.yinPan = yinPan;

    }

    public String getNeiCun() {

        return neiCun;

    }

    public void setNeiCun(String neiCun) {

        this.neiCun = neiCun;

    }

    public String getCpu() {

        return cpu;

    } 

    public void setCpu(String cpu) {

        this.cpu = cpu;

    }

}

2.3 Factory.java
package jianzaozhe;

public class Factory {

    private Builder computerKind; 

    public void setCompture(Builder kind) {

        this.computerKind = kind;

    }

    public Computer construct(){

        computerKind.buildCpu();

        computerKind.buildYinPan();

        computerKind.buildZhuJi();

        computerKind.buildNeiCun();

        return computerKind.getComputer();

    }

}

2.4 LapBuilder.java
package jianzaozhe;

public class LapBuilder extends Builder{

    @Override

    public void buildZhuJi() {

        computer.setZhuJi("LZJ");

    }

 

    @Override

    public void buildYinPan() {

        computer.setYinPan("LYP");

    }

 

    @Override

    public void buildCpu() {

        computer.setCpu("LC");

    }

 

    @Override

    public void buildNeiCun() {

        computer.setNeiCun("LNC");

    }

}

2.5 Store.java
package jianzaozhe;

public class Store {

    public static void main(String[] args) {

        Builder lap;

        Factory factory=new Factory();

        for (int i = 0; i < 2; i++) {

            if(i==1)

                lap = new LapBuilder();

            else

                lap=new TaiShiBuilder();

            factory.setCompture(lap);

            factory.construct();

            System.out.println(lap.getComputer().getCpu());

            System.out.println(lap.getComputer().getNeiCun());

            System.out.println(lap.getComputer().getYinPan());

            System.out.println(lap.getComputer().getZhuJi());

        }

 

    }

}

2.6 TaiShiBuilder.java
package jianzaozhe;

public class TaiShiBuilder extends Builder{

     

    @Override

    public void buildZhuJi() {

        computer.setZhuJi("TZJ");

    }

 

    @Override

    public void buildYinPan() {

        computer.setYinPan("TYP");

    }

 

    @Override

    public void buildCpu() {

        computer.setCpu("TC");

    }

 

    @Override

    public void buildNeiCun() {

        computer.setNeiCun("TNC");

    }

}

 

 

//c++
#include<iostream>
#include<string>

using namespace std;

class Compture {

private:

string cpu;

string neiCun;

string yinPan;

string zhuJi;

 

 

public:

string getZhuJi() {

return zhuJi;

}

 

void setZhuJi(string z) {

zhuJi = z;

}

 

string getYinPan() {

return yinPan;

}

 

void setYinPan(string y) {

yinPan = y;

}

 

string getNeiCun() {

return neiCun;

}

 

void setNeiCun(string n) {

neiCun = n;

}

 

string getCpu() {

return cpu;

}

 

void setCpu(string c) {

cpu = c;

}

};

 

class Builder {

 

public:

Compture* compture = new Compture();

virtual  void buildZhuJi() = 0;

virtual  void buildYinPan() = 0;

virtual  void buildCpu() = 0;

virtual  void buildNeiCun() = 0;

public:

Compture* getCompture(){

return compture;

}

};

class Factory {

private:

Builder* comptureKind;

 

public:

void setCompture(Builder* kind) {

comptureKind = kind;

}

Compture* construct(){

comptureKind->buildCpu();

comptureKind->buildYinPan();

comptureKind->buildZhuJi();

comptureKind->buildNeiCun();

return comptureKind->getCompture();

}

};

class LapBuilder :public Builder{

 

 

public:

void buildZhuJi() {

compture->setZhuJi("LZJ");

}

 

 

void buildYinPan() {

compture->setYinPan("LYP");

}

 

 

void buildCpu() {

compture->setCpu("LC");

}

 

 

void buildNeiCun() {

compture->setNeiCun("LNC");

}

};

 

class TaiShiBuilder :public Builder{

 

 

public:

void buildZhuJi() {

compture->setZhuJi("TZJ");

}

 

 

void buildYinPan() {

compture->setYinPan("TYP");

}

 

 

void buildCpu() {

compture->setCpu("TC");

}

 

 

void buildNeiCun() {

compture->setNeiCun("TNC");

}

};

 

int main()

{

Builder* lap;

Factory* factory = new Factory();

for (int i = 0; i < 2; i++) {

if (i == 1)

lap = new LapBuilder();

else

lap = new TaiShiBuilder();

factory->setCompture(lap);

factory->construct();

cout << lap->getCompture()->getCpu()<<endl;

cout << lap->getCompture()->getNeiCun()<<endl;

cout << lap->getCompture()->getYinPan()<<endl;

cout << lap->getCompture()->getZhuJi() << endl;

}

system("pause");

return 0;

}

 

标签:return,String,Builder,void,建造,模式,public,Tutorial,string
From: https://www.cnblogs.com/manmmm/p/16798536.html

相关文章

  • Tutorial 1_UML与面向对象程序设计基本原则
    [实验任务一]:UML复习阅读教材第一章复习UML,回答下述问题:面向对象程序设计中类与类的关系都有哪几种?分别用类图实例说明。1. 继承关系     继承指的是一个类(称为子......
  • Java_SE_Lesson_3:接口、单例模式、包与访问控制
    接口中所声明的方法都是抽象方法。接口中的方法都是public​的。接口中也可以定义成员变量。接口中的成员变量都是public,final,static的。一个类不能既是f......
  • 设计模式五(桥接模式)
      publicinterfaceDrawAPI{publicvoiddrawCircle(intradius,intx,inty);}publicclassRedCircleimplementsDrawAPI{@Overridepublic......
  • IDEA debug模式下修改变量后运行 ALT + F8 然后选中变量,右键可以修改变量值 Set Val
    IDEAdebug模式下修改变量后运行ALT+F8然后选中变量,右键可以修改变量值SetValue首先运行Debug模式来到30行,可以见到这个时刻入参configPath,对于if的判断结果是t......
  • 工厂模式
    1、c++工厂模式,点击进入(简单工厂模式、工厂方法模式、抽象工厂模式)2、实现可动态注册的简单工厂模式,点击进入3、c语言实现简单工厂模式,点击进入()4、工厂模式,点击进入(......
  • 初识设计模式 - 观察者模式
    简介观察者设计模式(ObserverDesignPattern)的别名有很多,如发布-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-监听(Source/Listener)模式或从属者(Depend......
  • 软件设计师:23种设计模式
    总图创建型设计模式1.抽象工厂方法模式代码packagenwnu.sun.patterns.createtype.abstractfactory;/***抽象工厂设计模式*/publicclassAbstractFfactor......
  • 【设计模式】原型模式(只能说很少用)
    针对原型模式(我有把这个模式命名为克隆模式)的定义(设计意图)原型实例指定创建对象的种类,并通过拷贝这些原型来创建新的对象。(所以核心是:拷贝原型对象)直接基于内存进行拷贝......
  • archlinux + i3wm 使用 tauri 开发者模式 (react + ts) 预览白屏 解决方法
    在我这台机器只需将tauri.conf.json的devPath的localhost改成127.0.0.1即可参考来源https://github.com/tauri-apps/tauri/issues/1140......
  • redis的发布订阅模式
    redis的发布订阅模式redis发布订阅(pub/sub)是一种消息通信模式,消息的发布者不会将消息发送给特定的订阅者,而是通过消息通道(频道)广播出去,让订阅该消息主题(频道)的订阅......