建造者模式

软件设计                  石家庄铁道大学信息学院

 

实验5:建造者模式

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

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

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

 
   

 

 

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

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

实验要求:

1.  画出对应的类图;

 

 

 

2.  提交源代码;

public class Main {

public static void main(String[] args) {

    int n;

   

    Computer cp=null;

    Builder bd=null;

    Scanner sc=new Scanner(System.in);

   

    System.out.println("请选择你要的电脑类型  1为笔记本电脑 2位台式电脑");

    n=sc.nextInt();

    bd=new ConcreteBuilder();

    if(n==1||n==2) {

        cp=bd.build(n);

        System.out.println(cp.toString());

    }else {

        System.out.println("打开方式不对,不给你了!!!");

    }  

}

}

 

 

 

 

 

 

#include <iostream>

 

using namespace std;

 

class Computer{

public:

    virtual void setCpu(){};

virtual void setNc(){};

virtual void setYp(){};

virtual void setZj(){};

};

 

class LapComputer:public Computer{

private:

    string CPU="";

string nc="";

string yp="";

public:

    string tostring() {

    return "LapComputer [" + CPU + ", " + nc + ", " + yp + "]";

}

 

void setCpu() {

    // TODO Auto-generated method stub

     CPU="CPU";

}

 

void setNc() {

    // TODO Auto-generated method stub

     nc="内存条";

}

 

void setYp() {

    // TODO Auto-generated method stub

     yp="硬盘";

}

 

void setZj() {

    // TODO Auto-generated method stub

 

}

};

 

class PCComputer:public Computer{

private:

    string CPU="";

string nc="";

string yp="";

string zj="";

 

public:

    void setCpu() {

    // TODO Auto-generated method stub

     CPU="CPU";

}

 

void setNc() {

    // TODO Auto-generated method stub

     nc="内存条";

}

 

void setYp() {

    // TODO Auto-generated method stub

     yp="硬盘";

}

 

void setZj() {

    // TODO Auto-generated method stub

     zj="主机";

}

 

string tostring() {

    return "PCComputer [" + CPU + ", " + nc + ", " + yp + ", " + zj + "]";

}

 

string getCPU() {

    return CPU;

}

 

void setCPU(string cPU) {

    CPU = cPU;

}

 

string getNc() {

    return nc;

}

 

void setNc(string nc) {

     nc = nc;

}

 

string getYp() {

    return yp;

}

 

void setYp(string yp) {

     yp = yp;

}

 

};

 

class Builder{

public:

    virtual void produceCPU(){};

virtual void produceNC(){};

virtual void ProduceYP(){};

virtual void ProduceZJ(){};

virtual Computer* build(int n){};

};

 

class ConcreteBuilder:public Builder{

private:

    Computer *cp;

 

public:

    Computer* build(int t) {

        cout<<"正在安装... ..."<<endl;

    //1是笔记本  2是台式

    if(t==1) {

        LapComputer c;

        cp=&c;

        produceCPU();

        produceNC();

        ProduceYP();

        cout<<"笔记本安装成功"<<endl;

        cout<<c.tostring()<<endl;

    }

    else if(t==2) {

        PCComputer c;

        cp=&c;

        produceCPU();

        produceNC();

        ProduceYP();

        ProduceZJ();

        cout<<"台式电脑安装成功"<<endl;

        cout<<c.tostring()<<endl;

    }

    else {

                ;

    }

}

 

void produceCPU() {

    // TODO Auto-generated method stub

     cp->setCpu();

    cout<<"安装CPU"<<endl;

}

 

void produceNC() {

    // TODO Auto-generated method stub

     cp->setNc();

    cout<<"安装内存条"<<endl;

}

 

void ProduceYP() {

    // TODO Auto-generated method stub

     cp->setYp();

    cout<<"安装硬盘"<<endl;

}

 

void ProduceZJ() {

    // TODO Auto-generated method stub

     cp->setZj();

    cout<<"安装主机"<<endl;

}

};

 

 

int main()

{

    int n;

    ConcreteBuilder b;

    Builder* bd=&b;

    cout<<"请选择你要的电脑类型  1为笔记本电脑 2位台式电脑"<<endl;

    cin>>n;

    if(n==1||n==2) {

        bd->build(n);

    }else {

        cout<<"打开方式不对,不给你了!!!"<<endl;

    }

    return 0;

}

 

 

 

 

3.注意编程规范。

posted @ 2021-10-15 21:41  我试试这个昵称好使不  阅读(57)  评论(0)    收藏  举报