一、任务
使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。
二、类图
三、代码
1、Java代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tutorial5; 2 3 public class BuilderComputer { 4 public void builderDesktop() { 5 6 } 7 public void builderLaptop() { 8 9 } 10 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tutorial5; 2 3 public class ProductComputer { 4 public Desktop desktop; 5 public Laptop laptop; 6 7 public ProductComputer() { 8 9 } 10 11 public void showDesktop(Desktop desktop2) { 12 System.out.println("现在展示笔记本计算机的组成:"); 13 14 desktop2.builderDesktop(); 15 } 16 public void showLaptop(Laptop laptop2) { 17 System.out.println("现在展示台式机计算机的组成:"); 18 laptop2.builderLaptop(); 19 } 20 21 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tutorial5; 2 3 import java.util.Scanner; 4 5 public class Director { 6 public static void main(String []args) { 7 ProductComputer product=new ProductComputer(); 8 Desktop desktop=new Desktop(); 9 Laptop laptop=new Laptop(); 10 boolean flag=true; 11 while(flag) { 12 System.out.println("1、组装笔记本电脑!!!"); 13 System.out.println("2、组装台式机电脑!!!"); 14 System.out.println("3、退出!!!"); 15 System.out.println("请进行选择:"); 16 Scanner in =new Scanner(System.in); 17 int a=in.nextInt(); 18 if(a==1) { 19 product.showDesktop(desktop); 20 }else if(a==2) { 21 product.showLaptop(laptop); 22 }else if(a==3) { 23 System.out.println("欢迎下次组装!!!"); 24 break; 25 }else { 26 flag=false; 27 System.out.println("请重新输入!!!"); 28 } 29 } 30 } 31 32 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tutorial5; 2 3 public class Desktop extends ConcreteBuilderComputer { 4 private String CPU; 5 private String zhuji; 6 private String neicun; 7 private String yingpan; 8 public String getCPU() { 9 return CPU; 10 } 11 public void setCPU(String cPU) { 12 CPU = cPU; 13 } 14 public String getZhuji() { 15 return zhuji; 16 } 17 public void setZhuji(String zhuji) { 18 this.zhuji = zhuji; 19 } 20 public String getNeicun() { 21 return neicun; 22 } 23 public void setNeicun(String neicun) { 24 this.neicun = neicun; 25 } 26 public String getYingpan() { 27 return yingpan; 28 } 29 public void setYingpan(String yingpan) { 30 this.yingpan = yingpan; 31 } 32 public Desktop(String cPU, String zhuji, String neicun, String yingpan) { 33 super(); 34 CPU = cPU; 35 this.zhuji = zhuji; 36 this.neicun = neicun; 37 this.yingpan = yingpan; 38 } 39 public Desktop() { 40 41 } 42 @Override 43 public void builderDesktop() { 44 System.out.println("笔记本电脑组成有:"); 45 System.out.println("笔记本硬盘、主机、内存、CPU"); 46 System.out.println("****************************"); 47 } 48 49 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package tutorial5; 2 3 public class Laptop extends ConcreteBuilderComputer { 4 private String CPU; 5 private String zhuji; 6 private String neicun; 7 private String yingpan; 8 public String getCPU() { 9 return CPU; 10 } 11 public void setCPU(String cPU) { 12 CPU = cPU; 13 } 14 public String getZhuji() { 15 return zhuji; 16 } 17 public void setZhuji(String zhuji) { 18 this.zhuji = zhuji; 19 } 20 public String getNeicun() { 21 return neicun; 22 } 23 public void setNeicun(String neicun) { 24 this.neicun = neicun; 25 } 26 public String getYingpan() { 27 return yingpan; 28 } 29 public void setYingpan(String yingpan) { 30 this.yingpan = yingpan; 31 } 32 public Laptop(String cPU, String zhuji, String neicun, String yingpan) { 33 super(); 34 CPU = cPU; 35 this.zhuji = zhuji; 36 this.neicun = neicun; 37 this.yingpan = yingpan; 38 } 39 public Laptop() { 40 41 } 42 @Override 43 public void builderLaptop() { 44 System.out.println("台式机电脑组成有:"); 45 System.out.println("台式机硬盘、主机、内存、CPU"); 46 System.out.println("****************************"); 47 } 48 49 }
2、C++
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 //class Builder; 6 7 // Product 8 class ProductComputer 9 { 10 11 12 13 public: 14 void ShowDesktop() 15 { 16 17 cout << "笔记本计算机已经组装好!!!" << endl<<endl; 18 cout << "笔记本组成为:" << endl; 19 cout << "CPU" << endl; 20 cout << "内存" << endl; 21 cout << "硬盘" << endl; 22 cout << "主机" << endl; 23 cout << "************************" << endl; 24 } 25 void ShowLaptop() 26 { 27 cout << "台式机计算机已经组装好!!!" << endl<<endl; 28 cout << "台式机组成为:" << endl; 29 cout << "CPU" << endl; 30 cout << "内存" << endl; 31 cout << "硬盘" << endl; 32 cout << "主机" << endl; 33 } 34 35 }; 36 37 // Builder 38 class BuilderComputer 39 { 40 public: 41 virtual void BuildDesktop() {} 42 virtual void BuildLaptop() {} 43 virtual ProductComputer* GetProduct() { return NULL; } 44 }; 45 46 // ConcreteBuilder 47 class Desktop : public BuilderComputer 48 { 49 public: 50 Desktop() { m_Product = new ProductComputer(); } 51 void BuildDesktop() { 52 m_Product->ShowDesktop(); 53 } 54 void BuildLaptop() { 55 56 } 57 ProductComputer* GetProduct() { return m_Product; } 58 59 private: 60 ProductComputer* m_Product; 61 }; 62 63 64 class Laptop : public BuilderComputer 65 { 66 private: 67 ProductComputer* m_Product; 68 public: 69 Laptop() { m_Product = new ProductComputer(); } 70 void BuildDesktop() { 71 72 } 73 void BuildLaptop() { 74 m_Product->ShowLaptop(); 75 } 76 77 ProductComputer* GetProduct() { return m_Product; } 78 79 }; 80 // Director 81 class Director 82 { 83 public: 84 Director(BuilderComputer* builder) { m_Builder = builder; } 85 void CreateProduct() 86 { 87 m_Builder->BuildDesktop(); 88 m_Builder->BuildLaptop(); 89 } 90 91 private: 92 BuilderComputer* m_Builder; 93 }; 94 95 // main 96 int main() 97 { 98 int choice; 99 cout << "************************" << endl; 100 cout << "请选择组装计算机:" << endl; 101 cout << "1、组装笔记本:" << endl; 102 cout << "2、组装台式机:" << endl; 103 cout << "请选择:" << endl; 104 cout << "************************" << endl; 105 cin >> choice; 106 if (choice == 1) { 107 BuilderComputer* builderObj1 = new Desktop(); 108 Director directorObj1(builderObj1); 109 directorObj1.CreateProduct(); 110 ProductComputer* productObj1 = builderObj1->GetProduct(); 111 if (productObj1 == NULL) 112 { 113 return 0; 114 } 115 //productObj1->ShowDesktop(); 116 delete productObj1; 117 productObj1 = NULL; 118 delete builderObj1; 119 builderObj1 = NULL; 120 121 } 122 else if (choice == 2) { 123 124 BuilderComputer* builderObj2 = new Laptop(); 125 Director directorObj2(builderObj2); 126 directorObj2.CreateProduct(); 127 ProductComputer* productObj2 = builderObj2->GetProduct(); 128 //productObj2->ShowLaptop(); 129 delete productObj2; 130 productObj2 = NULL; 131 delete builderObj2; 132 builderObj2 = NULL; 133 } 134 else { 135 cout << "输入错误!!!!" << endl; 136 } 137 }
四、结果截图
Java