C++ 设计模式

/* 设计模式 23种  */
/* 开放封闭原则: 类的改动 是通过增加代码来实现的  不是修改源代码 */
/* 比如一个类 实现了各种功能 但是后期想修改某个功能就很难 那我们把它写成纯虚函数 让继承的类去实现 */


 
  1. #include <iostream>

  2. #include <windows.h>

  3.  
  4. using namespace std;

  5.  
  6. /*class Bankworker//一开始的想法 一个类实现多个操作

  7. {

  8. public:

  9.     void savemoney()

  10.     {

  11.         cout << " savemoney " << endl;

  12.     }

  13.     void getmoney()

  14.     {

  15.         cout << " getmoney " << endl;

  16.     }

  17.     void transformmoney()

  18.     {

  19.         cout << " transformmoney " << endl;

  20.     }

  21. };*/

  22.  
  23. class Bankworker//把这个类函数变成 纯虚函数

  24. {

  25. public:

  26.     /*virtual void savemoney() = 0;

  27.     

  28.     virtual void getmoney() = 0;

  29.     

  30.     virtual void transformmoney() = 0;*/

  31.     virtual void work() = 0;

  32.     

  33. };

  34.  
  35. class Savework : public Bankworker//继承的类进行实现 savemoney

  36. {

  37. public:

  38.     void work()

  39.     {

  40.         cout << " savemoney " << endl;

  41.     }

  42.  
  43. };

  44.  
  45. class Getwork : public Bankworker//继承的类进行实现 getmoney

  46. {

  47. public:

  48.     void work()

  49.     {

  50.         cout << " getmoney " << endl;

  51.     }

  52.  
  53. };

  54.  
  55. class transformwork : public Bankworker//继承的类进行实现 transformmoney

  56. {

  57. public:

  58.     void work()

  59.     {

  60.         cout << " transformmoney " << endl;

  61.     }

  62.  
  63. };

  64.  
  65. int main()

  66. {

  67.     /*Bankworker *b1 = new Bankworker;

  68.  
  69.     b1->getmoney();

  70.     b1->savemoney();*/

  71.  
  72.     Bankworker *b1 = NULL;

  73.     b1 = new Savework;

  74.     b1->work();

  75.  
  76.     b1 = new Getwork;

  77.     b1->work();

  78.  
  79.     b1 = new transformwork;

  80.     b1->work();

  81.  
  82.     system("pause");

  83.     return 0;

  84. }

/* 依赖倒置原则 : */


 
  1. /*

  2. #include <iostream>

  3. #include <windows.h>

  4.  
  5. using namespace std;

  6.  
  7. class Ram2G

  8. {

  9. public:

  10. void Ramwork()

  11. {

  12. cout << "Ramwork 2G" << endl;

  13. }

  14. };

  15.  
  16. class Rom32G

  17. {

  18. public:

  19. void Romwork()

  20. {

  21. cout << "Romwork 32G" << endl;

  22. }

  23. };

  24.  
  25.  
  26. class Phone//依赖于实现

  27. {

  28. private:

  29. Ram2G *Ra;

  30. Rom32G *Ro;

  31. public:

  32. Phone(Ram2G *a, Rom32G *b)

  33. {

  34. Ra = a;

  35. Ro = b;

  36. }

  37. void Phonework()

  38. {

  39. Ra->Ramwork();

  40. Ro->Romwork();

  41. }

  42.  
  43. };

  44.  
  45. int main()

  46. {

  47. Ram2G *Ra2 = new Ram2G;

  48. Rom32G *Ro32 = new Rom32G;

  49. Phone *p = new Phone(Ra2, Ro32);

  50. p->Phonework();

  51.  
  52. system("pause");

  53. return 0;

  54. }

  55. */

  56.  
  57. #include <iostream>

  58. #include <windows.h>

  59.  
  60. using namespace std;

  61.  
  62. class Ram

  63. {

  64. public:

  65. virtual void Ramwork() = 0;

  66. };

  67. class Ram2G : public Ram

  68. {

  69. public:

  70. void Ramwork()

  71. {

  72. cout << "Ramwork 2G" << endl;

  73. }

  74. };

  75. class Ram4G : public Ram

  76. {

  77. public:

  78. void Ramwork()

  79. {

  80. cout << "Ramwork 4G" << endl;

  81. }

  82. };

  83.  
  84. class Rom

  85. {

  86. public:

  87. virtual void Romwork() = 0;

  88. };

  89. class Rom32G : public Rom

  90. {

  91. public:

  92. void Romwork()

  93. {

  94. cout << "Romwork 32G" << endl;

  95. }

  96. };

  97. class Rom64G : public Rom

  98. {

  99. public:

  100. void Romwork()

  101. {

  102. cout << "Romwork 64G" << endl;

  103. }

  104. };

  105.  
  106.  
  107. class Phone

  108. {

  109. private:

  110. Ram *Ra;

  111. Rom *Ro;

  112. public:

  113. Phone(Ram *a, Rom *b)

  114. {

  115. Ra = a;

  116. Ro = b;

  117. }

  118. void Phonework()

  119. {

  120. Ra->Ramwork();

  121. Ro->Romwork();

  122. }

  123. };

  124.  
  125. int main()

  126. {

  127. /*Ram *Ra = new Ram2G;

  128. Rom *Ro = new Rom32G;

  129. Phone *p = new Phone(Ra, Ro);

  130. p->Phonework();

  131. delete Ra;

  132. delete Ro;

  133. delete p;*/

  134.  
  135. Ram *Ra1 = new Ram4G;

  136. Rom *Ro1 = new Rom64G;

  137. Phone *p1 = new Phone(Ra1, Ro1);

  138. p1->Phonework();

  139. delete Ra1;

  140. delete Ro1;

  141. delete p1;

  142.  
  143. system("pause");

  144. return 0;

  145. }

/* 工厂模式 */


 
  1. #include <iostream>

  2. #include <windows.h>

  3. #include <stdlib.h>

  4.  
  5. using namespace std;

  6.  
  7. class car

  8. {

  9. public:

  10. virtual void show() = 0;

  11. };

  12.  
  13. class benz : public car

  14. {

  15. public:

  16. void show()

  17. {

  18. cout << "benz" << endl;

  19. }

  20. };

  21.  
  22. class baoma : public car

  23. {

  24. public:

  25. void show()

  26. {

  27. cout << "baoma" << endl;

  28. }

  29. };

  30.  
  31. class carfactory

  32. {

  33. public:

  34. virtual car * showcar() = 0;

  35. };

  36.  
  37. class factorybaoma :public carfactory

  38. {

  39. public:

  40. car * showcar()

  41. {

  42. return new baoma;

  43. }

  44. };

  45.  
  46. class factorybenz :public carfactory

  47. {

  48. public:

  49. car * showcar()

  50. {

  51. return new benz;

  52. }

  53. };

  54.  
  55. int main()

  56. {

  57. carfactory *f = new factorybaoma;

  58. f->showcar()->show();

  59.  
  60. delete f;

  61.  
  62. system("pause");

  63. return 0;

  64. }

/* 抽象工厂 */


 
  1. #include <iostream>

  2. #include <windows.h>

  3. #include <stdlib.h>

  4.  
  5. using namespace std;

  6.  
  7. class fruit

  8. {

  9. public :

  10. virtual void show() = 0;

  11. };

  12.  
  13. class Northapple : public fruit

  14. {

  15. public:

  16. void show()

  17. {

  18. cout << " Northapple " << endl;

  19. }

  20. };

  21.  
  22. class Northpear : public fruit

  23. {

  24. public:

  25. void show()

  26. {

  27. cout << " Northpear " << endl;

  28. }

  29. };

  30.  
  31. class Sorthapple : public fruit

  32. {

  33. public:

  34. void show()

  35. {

  36. cout << " Sorthapple " << endl;

  37. }

  38. };

  39.  
  40. class Sorthpear : public fruit

  41. {

  42. public:

  43. void show()

  44. {

  45. cout << " Sorthpear " << endl;

  46. }

  47. };

  48.  
  49. class factory

  50. {

  51. public:

  52. virtual fruit *createapple() = 0;

  53. virtual fruit *createpear() = 0;

  54. };

  55.  
  56. class Northfactory : public factory

  57. {

  58. public:

  59. fruit *createapple()

  60. {

  61. return new Northapple;

  62. }

  63. fruit *createpear()

  64. {

  65. return new Northpear;

  66. }

  67. };

  68.  
  69. class Sorthfactory : public factory

  70. {

  71. public:

  72. fruit *createapple()

  73. {

  74. return new Sorthapple;

  75. }

  76. fruit *createpear()

  77. {

  78. return new Sorthpear;

  79. }

  80. };

  81.  
  82. int main()

  83. {

  84. factory *n1 = new Northfactory;

  85. n1->createapple()->show();

  86. n1->createpear()->show();

  87. delete n1;

  88.  
  89. factory *n2 = new Sorthfactory;

  90. n2->createapple()->show();

  91. n2->createpear()->show();

  92. delete n2;

  93.  
  94. system("pause");

  95. return 0;

  96. }

/* 建造者模式 */


 
  1. #include <iostream>

  2. #include <windows.h>

  3. #include <stdlib.h>

  4. #include <string>

  5.  
  6. using namespace std;

  7.  
  8. class house

  9. {

  10. private:

  11. string wall;

  12. string door;

  13. string window;

  14. public:

  15. void setwall(string w)

  16. {

  17. wall = w;

  18. }

  19. void setdoor(string d)

  20. {

  21. door = d;

  22. }

  23. void setwindow(string wi)

  24. {

  25. window = wi;

  26. }

  27.  
  28. string getwall()

  29. {

  30. return wall;

  31. }

  32. string getdoor()

  33. {

  34. return door;

  35. }

  36. string getwindow()

  37. {

  38. return window;

  39. }

  40. };

  41.  
  42. class builder

  43. {

  44. private:

  45. house *h;

  46. public:

  47. builder()

  48. {

  49. h = new house;

  50. }

  51.  
  52.  
  53. void constuctor()

  54. {

  55. h->setdoor("door");

  56. h->setwall("wall");

  57. h->setwindow("window");

  58. }

  59.  
  60. void getinfo()

  61. {

  62. cout << h->getdoor() << endl;

  63. cout << h->getwall() << endl;

  64. cout << h->getwindow() << endl;

  65. }

  66. };

  67.  
  68.  
  69. int main()

  70. {

  71. builder *b1 = new builder;

  72. b1->constuctor();

  73. b1->getinfo();

  74.  
  75. system("pause");

  76. return 0;

  77. }

/* 建造者模式改良版 */


 
  1. #include <iostream>

  2. #include <windows.h>

  3. #include <stdlib.h>

  4. #include <string>

  5.  
  6. using namespace std;

  7.  
  8. class house

  9. {

  10. private:

  11. string wall;

  12. string door;

  13. string window;

  14. public:

  15. void setwall(string w)

  16. {

  17. wall = w;

  18. }

  19. void setdoor(string d)

  20. {

  21. door = d;

  22. }

  23. void setwindow(string wi)

  24. {

  25. window = wi;

  26. }

  27.  
  28. string getwall()

  29. {

  30. return wall;

  31. }

  32. string getdoor()

  33. {

  34. return door;

  35. }

  36. string getwindow()

  37. {

  38. return window;

  39. }

  40. };

  41.  
  42. class builder

  43. {

  44. private:

  45. house *h;

  46. public:

  47. virtual void constuctor() = 0;

  48. virtual void getinfo() = 0;

  49. };

  50.  
  51. class buildernormal : public builder

  52. {

  53. private:

  54. house *h;

  55. public:

  56. buildernormal()

  57. {

  58. h = new house;

  59. }

  60.  
  61.  
  62. void constuctor()

  63. {

  64. h->setdoor("doornormal");

  65. h->setwall("wallnormal");

  66. h->setwindow("windownormal");

  67. }

  68.  
  69. void getinfo()

  70. {

  71. cout << h->getdoor() << endl;

  72. cout << h->getwall() << endl;

  73. cout << h->getwindow() << endl;

  74. }

  75. };

  76.  
  77. class buildervilla : public builder

  78. {

  79. private:

  80. house *h;

  81. public:

  82. buildervilla()

  83. {

  84. h = new house;

  85. }

  86.  
  87.  
  88. void constuctor()

  89. {

  90. h->setdoor("doorvilla");

  91. h->setwall("wallvilla");

  92. h->setwindow("windowvilla");

  93. }

  94.  
  95. void getinfo()

  96. {

  97. cout << h->getdoor() << endl;

  98. cout << h->getwall() << endl;

  99. cout << h->getwindow() << endl;

  100. }

  101. };

  102.  
  103.  
  104. int main()

  105. {

  106. builder *b = new buildernormal;

  107.  
  108. b->constuctor();

  109. b->getinfo();

  110.  
  111. builder *b1 = new buildervilla;

  112.  
  113. b1->constuctor();

  114. b1->getinfo();

  115.  
  116. system("pause");

  117. return 0;

  118. }

/* 原型模式 */


 
  1. #include <iostream>

  2.  
  3. using namespace std;

  4.  
  5. class Person

  6. {

  7. protected:

  8. string name;

  9. public:

  10. Person()

  11. {

  12.  
  13. }

  14. Person(string n)

  15. {

  16. name = n;

  17. }

  18. string GetName()

  19. {

  20. return name;

  21. }

  22.  
  23. virtual void show() = 0;

  24. virtual Person *clone() = 0;

  25. };

  26.  
  27. class Student : public Person

  28. {

  29. private:

  30. int id;

  31. public:

  32. Student()

  33. {

  34.  
  35. }

  36. Student(string n, int i) : Person(n)

  37. {

  38. id = i;

  39. }

  40. void show()

  41. {

  42. cout << name << " " << id << endl;

  43. }

  44. Person *clone()

  45. {

  46. Student *tmp = new Student;

  47. *tmp = *this;

  48.  
  49. return tmp;

  50. }

  51. };

  52.  
  53. int main()

  54. {

  55. Person *s1 = new Student("aa", 1);

  56. s1->show();

  57.  
  58. Person *s2 = s1->clone();

  59. s2->show();

  60.  
  61. return 0;

  62. }

/* 组合模式 */


 
  1. #include <iostream>

  2. #include <list>

  3.  
  4. using namespace std;

  5.  
  6. class BaseFile

  7. {

  8. public:

  9. virtual string GetName() = 0;

  10. virtual int Add(BaseFile *file) = 0;

  11. virtual int Remove(BaseFile *file) = 0;

  12. virtual list<BaseFile *> *GetChild() = 0;

  13. };

  14.  
  15. class IFile : public BaseFile

  16. {

  17. private:

  18. string name;

  19. public:

  20. IFile(string n)

  21. {

  22. name = n;

  23. }

  24. string GetName()

  25. {

  26. return name;

  27. }

  28. int Add(BaseFile *file)

  29. {

  30. return -1;

  31. }

  32.  
  33. int Remove(BaseFile *file)

  34. {

  35. return -1;

  36. }

  37. list<BaseFile *> *GetChild()

  38. {

  39. return NULL;

  40. }

  41. };

  42.  
  43. class Dir : public BaseFile

  44. {

  45. private:

  46. string name;

  47. list<BaseFile *> *l;

  48. public:

  49. Dir(string n)

  50. {

  51. name = n;

  52. l = new list<BaseFile *>;

  53. }

  54. string GetName()

  55. {

  56. return name;

  57. }

  58. int Add(BaseFile *file)

  59. {

  60. l->push_back(file);

  61. return 1;

  62. }

  63. int Remove(BaseFile *file)

  64. {

  65. l->remove(file);

  66. return 1;

  67. }

  68. list<BaseFile *> *GetChild()

  69. {

  70. return l;

  71. }

  72. };

  73.  
  74. void show(BaseFile *root, int gap)

  75. {

  76. for (int i = 0; i < gap; i++)

  77. {

  78. cout << "---";

  79. }

  80.  
  81. if (root != NULL)

  82. {

  83. cout << root->GetName() << endl;

  84. }

  85. else

  86. {

  87. return;

  88. }

  89.  
  90. list<BaseFile *> *l = root->GetChild();

  91. if (l != NULL)

  92. {

  93. for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++)

  94. {

  95. show((*it), gap + 1);

  96. }

  97. }

  98. }

  99.  
  100. int main()

  101. {

  102. BaseFile *root = new Dir("root");

  103. BaseFile *dir1 = new Dir("home");

  104. BaseFile *file1 = new IFile("1.c");

  105. BaseFile *file2 = new IFile("2.c");

  106. BaseFile *file3 = new IFile("3.c");

  107. BaseFile *file4 = new IFile("4.c");

  108. BaseFile *file5 = new IFile("5.c");

  109. BaseFile *dir2 = new Dir("hello");

  110.  
  111. dir1->Add(file1);

  112. dir1->Add(file2);

  113. dir1->Add(file3);

  114. dir1->Add(file4);

  115. dir1->Add(file5);

  116.  
  117. dir2->Add(file1);

  118. dir2->Add(file2);

  119. dir2->Add(file3);

  120. dir2->Add(file4);

  121. dir2->Add(file5);

  122.  
  123. root->Add(dir1);

  124. root->Add(dir2);

  125. /*list<BaseFile *> *l = dir1->GetChild();

  126. for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++)

  127. {

  128. cout << (*it)->GetName() << endl;

  129. }*/

  130.  
  131. show(root, 0);

  132.  
  133. return 0;

  134. }

阅读更多
上一篇C语言整理之消息队列
下一篇C语言整理之共享内存
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭