实验5:建造者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解建造者模式的动机,掌握该模式的结构;
2、能够利用建造者模式解决实际问题。
[实验任务一]:计算机组装
使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。
实验要求:
1.画出对应的类图;
2.提交源代码;
3.注意编程规范。

Java
1.Builder
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 Compture getCompture(){
return compture;
}
}
① Compture
public class Compture {
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;
}
}
② Factory
public class Factory {
private Builder comptureKind;
public void setCompture(Builder kind) {
this.comptureKind = kind;
}
public Compture construct(){
comptureKind.buildCpu();
comptureKind.buildYinPan();
comptureKind.buildZhuJi();
comptureKind.buildNeiCun();
return comptureKind.getCompture();
}
}
③ LapBuilder
public class LapBuilder extends Builder{
@Override
public void buildZhuJi() {
compture.setZhuJi("LZJ");
}
@Override
public void buildYinPan() {
compture.setYinPan("LYP");
}
@Override
public void buildCpu() {
compture.setCpu("LC");
}
@Override
public void buildNeiCun() {
compture.setNeiCun("LNC");
}
}
④ Store
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.getCompture().getCpu());
System.out.println(lap.getCompture().getNeiCun());
System.out.println(lap.getCompture().getYinPan());
System.out.println(lap.getCompture().getZhuJi());
}
}
}
⑤ TaiShiBuilder
public class TaiShiBuilder extends Builder{
@Override
public void buildZhuJi() {
compture.setZhuJi("TZJ");
}
@Override
public void buildYinPan() {
compture.setYinPan("TYP");
}
@Override
public void buildCpu() {
compture.setCpu("TC");
}
@Override
public void buildNeiCun() {
compture.setNeiCun("TNC");
}
}
(1) C++:
// ShiYan_5.cpp : 定义控制台应用程序的入口点。
//
include "stdafx.h"
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;
}
实验6:原型模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解原型模式的动机,掌握该模式的结构;
2、能够利用原型模式解决实际问题。
[实验任务一]:向量的原型
用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。
实验要求:
1.画出对应的类图;
2.提交源代码(用C++完成);
3.注意编程规范。

浅克隆
include <bits/stdc++.h> using namespace std;
class Vector {private:
int* p;
int len;
public:
// 构造函数
Vector(int len) : len(len) {
p = (int*)calloc(len, sizeof(int));
}
// 拷贝构造函数(浅克隆)
Vector(const Vector& vector) {this->len = vector.len;this->p = vector.p;
}// 析构函数
~Vector() {free(p);
}int operator[](int i) const { return p[i]; }
int& operator[](int i) { return p[i]; }// 获取长度
int length() const { return len; }// 显示向量
void display() const {for (int i = 0; i < len; i++) {if (i == len - 1) {cout << p[i] << endl;} else {cout << p[i] << ", ";}}
}
};
int main() {
Vector v1(10);
for (int i = 0; i < 10; i++) {
v1[i] = i;
}
Vector v2 = v1; // 浅克隆
v2[6] = 33;
v2[2] = 19;cout << "浅克隆:" << endl;
cout << "更改copyVector后vector的数据:" << endl;
v1.display();
cout << "更改copyVector后copyVector的数据:" << endl;
v2.display();return 0;
}
深克隆
include
include // for memcpy and callocusing namespace std;
class Vector {private:
int* p;
int len;
public:
// 构造函数
Vector(int len) : len(len) {
p = (int*)calloc(len, sizeof(int));
}
// 拷贝构造函数(深克隆)
Vector(const Vector& vector) {this->len = vector.len;this->p = (int*)calloc(this->len, sizeof(int));memcpy(this->p, vector.p, len * sizeof(int));
}// 析构函数
~Vector() {free(p);
}// 下标操作符重载
int operator[](int i) const { return p[i]; }
int& operator[](int i) { return p[i]; }// 获取长度
int length() const { return len; }// 显示向量
void display() const {for (int i = 0; i < len; i++) {if (i == len - 1) {cout << p[i] << endl;} else {cout << p[i] << ", ";}}
}
};
int main() {
Vector v1(10);
for (int i = 0; i < 10; i++) {
v1[i] = i;
}
Vector v2 = v1; // 深克隆
v2[6] = 33;
v2[2] = 19;cout << "深克隆:" << endl;
cout << "更改copyVector后vector的数据:" << endl;
v1.display();
cout << "更改copyVector后copyVector的数据:" << endl;
v2.display();return 0;
}