当前位置:   article > 正文

C++设计模式-建造者模式

建造者模式

目录

 

 

基本概念

代码与实例


 

基本概念

建造者模式(Builder):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

建造者模式是在当创建复杂对象的算法应该独立于该对象的组成部分以及它们装配方式时适用的模式。

下面给出一个UML实例:

 

 

代码与实例

程序运行截图如下:

源码如下:

Head.h

  1. #include <string>
  2. using namespace std;
  3. typedef string Graphics;
  4. typedef string Pen;
  5. class PersonBuilder{
  6. public:
  7. virtual void buildHead(){}
  8. virtual void buildBody(){}
  9. virtual void buildArmLeft(){}
  10. virtual void buildArmRight(){}
  11. virtual void buildLegLeft(){}
  12. virtual void buildLegRight(){}
  13. PersonBuilder(Graphics g, Pen p);
  14. virtual ~PersonBuilder();
  15. private:
  16. Graphics *g;
  17. Pen *p;
  18. };
  19. class PersonThinBuilder : public PersonBuilder{
  20. public:
  21. PersonThinBuilder(Graphics g, Pen p);
  22. void buildHead();
  23. void buildBody();
  24. void buildArmLeft();
  25. void buildArmRight();
  26. void buildLegLeft();
  27. void buildLegRight();
  28. };
  29. class PersonFatBuilder : public PersonBuilder{
  30. public:
  31. PersonFatBuilder(Graphics g, Pen p);
  32. void buildHead();
  33. void buildBody();
  34. void buildArmLeft();
  35. void buildArmRight();
  36. void buildLegLeft();
  37. void buildLegRight();
  38. };
  39. class PersonDirector{
  40. public:
  41. PersonDirector(PersonBuilder *pb);
  42. void createPersion();
  43. private:
  44. PersonBuilder *pb;
  45. };

Head.cpp

  1. #include "Head.h"
  2. #include <iostream>
  3. PersonThinBuilder::PersonThinBuilder(Graphics g, Pen p):PersonBuilder(g, p)
  4. {
  5. }
  6. void PersonThinBuilder::buildHead()
  7. {
  8. cout << "瘦子 画头" << endl;
  9. }
  10. void PersonThinBuilder::buildBody()
  11. {
  12. cout << "瘦子 画身体" << endl;
  13. }
  14. void PersonThinBuilder::buildArmLeft()
  15. {
  16. cout << "瘦子 画左手" << endl;
  17. }
  18. void PersonThinBuilder::buildArmRight()
  19. {
  20. cout << "瘦子 画右手" << endl;
  21. }
  22. void PersonThinBuilder::buildLegLeft()
  23. {
  24. cout << "瘦子 画左脚" << endl;
  25. }
  26. void PersonThinBuilder::buildLegRight()
  27. {
  28. cout << "瘦子 画右脚" << endl;
  29. }
  30. PersonBuilder::PersonBuilder(Graphics g, Pen p)
  31. {
  32. this->g = new Graphics(g);
  33. this->p = new Pen(p);
  34. }
  35. PersonBuilder::~PersonBuilder()
  36. {
  37. delete g;
  38. delete p;
  39. }
  40. PersonFatBuilder::PersonFatBuilder(Graphics g, Pen p):PersonBuilder(g, p)
  41. {
  42. }
  43. void PersonFatBuilder::buildHead()
  44. {
  45. cout << "胖子 画头" << endl;
  46. }
  47. void PersonFatBuilder::buildBody()
  48. {
  49. cout << "胖子 画身体" << endl;
  50. }
  51. void PersonFatBuilder::buildArmLeft()
  52. {
  53. cout << "胖子 画左手" << endl;
  54. }
  55. void PersonFatBuilder::buildArmRight()
  56. {
  57. cout << "胖子 画右手" << endl;
  58. }
  59. void PersonFatBuilder::buildLegLeft()
  60. {
  61. cout << "胖子 画左脚" << endl;
  62. }
  63. void PersonFatBuilder::buildLegRight()
  64. {
  65. cout << "胖子 画右脚" << endl;
  66. }
  67. PersonDirector::PersonDirector(PersonBuilder *pb)
  68. {
  69. this->pb = pb;
  70. }
  71. void PersonDirector::createPersion()
  72. {
  73. pb->buildBody();
  74. pb->buildHead();
  75. pb->buildArmLeft();
  76. pb->buildArmRight();
  77. pb->buildLegLeft();
  78. pb->buildLegRight();
  79. }

main.cpp

  1. #include "Head.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main(int *argc, int *argv){
  5. PersonThinBuilder *pT = new PersonThinBuilder("画瘦子", "红笔");
  6. PersonDirector *pD = new PersonDirector(pT);
  7. pD->createPersion();
  8. delete pT;
  9. delete pD;
  10. cout << "------------ 华 丽 的 分 割 线 ------------" << endl;
  11. PersonFatBuilder *pF = new PersonFatBuilder("画胖子", "黑笔");
  12. PersonDirector *pD2 = new PersonDirector(pF);
  13. pD2->createPersion();
  14. delete pF;
  15. delete pD2;
  16. getchar();
  17. return 0;
  18. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/174182
推荐阅读
相关标签
  

闽ICP备14008679号