image.png

单例模式

image.png

  1. //.h
  2. #include <iostream>
  3. using namespace std;
  4. class Singleton {
  5. public:
  6. static const Singleton* getInstance();
  7. static void doSomething(){
  8. cout << "doSomething" <<endl;
  9. }
  10. private:
  11. Singleton();
  12. ~Singleton();
  13. static Singleton* This; //使用静态变量解决资源的分配和释放
  14. };
  15. //.c
  16. #include "Singleton.h"
  17. //Singleton* Singleton::This = NULL; //懒汉
  18. Singleton* Singleton::This = new Singleton(); //饿汉
  19. const Singleton* Singleton::getInstance(){
  20. if(!This){
  21. This = new Singleton();
  22. }
  23. return This;
  24. }
  25. Singleton::Singleton() {
  26. }
  27. Singleton::~Singleton() {
  28. }
  29. int main(){
  30. Singleton::getInstance() -> doSomething();
  31. }
  1. //https://www.cnblogs.com/kohlrabi/p/9179630.html
  2. //https://blog.csdn.net/dove1202ly/article/details/81477511
  3. #include <iostream>
  4. using namespace std;
  5. class Singleton{
  6. public:
  7. static Singleton * getInstance(){
  8. return instance;
  9. }
  10. static void destroy(){
  11. if(instance != nullptr){
  12. delete instance;
  13. instance = NULL;
  14. }
  15. }
  16. private:
  17. Singleton(){
  18. }
  19. static Singleton* instance;
  20. };
  21. Singleton* Singleton::instance = new Singleton();
  22. int main(){
  23. Singleton* singleton = Singleton::getInstance();
  24. Singleton* singleton2 = Singleton::getInstance();
  25. if(singleton == singleton2){
  26. cout << "singleton1 = singleton2" << endl;
  27. }
  28. Singleton::destroy();
  29. if(Singleton::getInstance() == nullptr){
  30. cout << "singleton1 is destroy" << endl;
  31. }
  32. return 0;
  33. }

观察者模式

image.png

image.png
观察者

  1. //observer
  2. //.h
  3. #include <iostream>
  4. using namespace std;
  5. class Observer {
  6. public:
  7. Observer() { ; }
  8. virtual ~Observer() { ; }
  9. //当被观察者发生变化时,通知
  10. virtual void Update(void* pArg) = 0;
  11. };

被观察者

  1. //.h
  2. #include <list>
  3. #include <string>
  4. #include "Observer.h"
  5. class Observer;
  6. class Observerable {
  7. public:
  8. Observerable();
  9. virtual ~Observerable();
  10. void Attach(Observer* pOb);
  11. void Detach(Observer* pOb);
  12. int GetObserverCount() const {
  13. return _obs.size();
  14. }
  15. void DetachAll(){
  16. _obs.clear();
  17. }
  18. virtual void GetSomeNews(string str){
  19. SetChange(str);
  20. }
  21. protected:
  22. void SetChange(string news);
  23. private:
  24. bool _bChange;
  25. list<Observer*> _obs;
  26. void Notify(void* pArg);
  27. };
  28. //.c
  29. #include "Observerable.h"
  30. Observerable::Observerable():_bChange(false){
  31. }
  32. Observerable::~Observerable() {
  33. }
  34. void Observerable::Attach(Observer* pOb){
  35. if(pOb == NULL){
  36. return;
  37. }
  38. //auto c++11
  39. auto it = _obs.begin();
  40. for(; it != _obs.end(); it++){
  41. if(*it == pOb){
  42. return;
  43. }
  44. }
  45. _obs.push_back(pOb);
  46. }
  47. void Observerable::Detach(Observer* pOb){
  48. if((pOb == NULL) || (_obs.empty() == true)){
  49. return;
  50. }
  51. _obs.remove(pOb);
  52. }
  53. void Observerable::SetChange(string news){
  54. _bChange = true;
  55. Notify((void *) news.c_str());
  56. }
  57. void Observerable::Notify(void* pArg){
  58. if(_bChange == false){
  59. return;
  60. } else{
  61. auto it = _obs.begin();
  62. for(; it != _obs.end(); it++){
  63. (*it) -> Update(pArg);
  64. }
  65. _bChange = false;
  66. }
  67. }

使用

  1. #include "Observerable.h"
  2. class User1: public Observer{
  3. void Update(void *pArg) {
  4. cout << "User1:" << (char *)pArg << endl;
  5. }
  6. };
  7. class User2: public Observer{
  8. void Update(void *pArg) {
  9. cout << "User2:" << (char *)pArg << endl;
  10. }
  11. };
  12. class News: public Observerable{
  13. public:
  14. void GetSomeNews(string str){
  15. SetChange("News:" + str);
  16. }
  17. };
  18. int main(){
  19. //帮忙把职责关系分清了
  20. User1 u1;
  21. User2 u2;
  22. News n1;
  23. n1.GetSomeNews("T0");
  24. cout << n1.GetObserverCount() << endl;
  25. n1.Attach(&u1);
  26. n1.Attach(&u2);
  27. n1.GetSomeNews("T1");
  28. cout << n1.GetObserverCount() << endl;
  29. n1.Detach(&u2);
  30. n1.GetSomeNews("T2");
  31. cout << n1.GetObserverCount() << endl;
  32. n1.DetachAll();
  33. n1.GetSomeNews("T3");
  34. cout << n1.GetObserverCount() << endl;
  35. return 1;
  36. }

适配器(Adapter)模式

image.png

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class LegacyRectangle{
  5. public:
  6. LegacyRectangle(double x1, double y1, double x2, double y2){
  7. _x1 = x1;
  8. _y1 = y1;
  9. _x2 = y2;
  10. _y2 = y2;
  11. }
  12. void LegacyDraw(){
  13. cout << "LegacyRectangle:LegacyDraw:" << _x1 << " " << _y1 << " " << _x2 << " " << _y2 << " " <<endl;
  14. }
  15. private:
  16. double _x1;
  17. double _y1;
  18. double _x2;
  19. double _y2;
  20. };
  21. class Rectangle{
  22. public:
  23. virtual void draw(string str) = 0;
  24. };
  25. //第一种适配方式 多继承
  26. class RectangleAdapter: public Rectangle, public LegacyRectangle{
  27. public:
  28. RectangleAdapter(int x, int y, int w, int h):LegacyRectangle(x, y, x + w, y + h){
  29. cout << "RectangleAdapter(int x, int y, int w, int h)" << endl;
  30. }
  31. virtual void draw(string str){
  32. cout << "RectangleAdapter:draw" << endl;
  33. LegacyDraw();
  34. }
  35. };
  36. //第二种适配方式 组合
  37. class RectangleAdapter2: public Rectangle{
  38. public:
  39. RectangleAdapter2(int x, int y, int w, int h): _lRect(x, y, x + w, y + h){
  40. cout << "RectangleAdapter(int x, int y, int w, int h)" << endl;
  41. }
  42. virtual void draw(string str){
  43. cout << "RectangleAdapter:draw" << endl;
  44. _lRect.LegacyDraw();
  45. }
  46. private:
  47. LegacyRectangle _lRect;
  48. };
  49. int main(){
  50. int x = 20, y = 50, w = 200, h = 200;
  51. RectangleAdapter ra(x, y, w, h);
  52. Rectangle *pR = &ra;
  53. pR->draw("Rectangle1");
  54. cout << endl;
  55. RectangleAdapter2 ra2(x, y, w, h);
  56. Rectangle *pR2 = &ra2;
  57. pR2->draw("Rectangle2");
  58. return 0;
  59. }

image.png