设计模式的目标是:封装变化,其中必然会用到接口+多态!

https://github.com/Waleon/DesignPatterns

builder

某各类的构造函数参数过多(定制型高),则可以使用内部类来抽象下,内部类负责new出母类,然后按需调用内部类的各个func,修饰母类对象.注意返回*this,以便支持级联.

Abstract Factory

工厂也分类型(came from FactoryMethod came from SimpleFactory).

简单工厂: 根据传入的参数来返回不同类型的对象(他们都实现了某个接口)

class Button;  // Abstract Class
class Border;  // Abstract Class
class MacButton : public Button {};
class WinButton : public Button {};
class MacBorder : public Border {};
class WinBorder : public Border {};
class AbstractFactory {
public:
  virtual Button* CreateButton() = 0;
  virtual Border* CreateBorder() = 0;
}
class MacFactory : public AbstractFactory {
public:
  MacButton* CreateButton() { return new MacButton; }
  MacBorder* CreateBorder() { return new MacBorder; }
}
class WinFactory : public AbstractFactory {
public:
  WinButton* CreateButton() { return new WinButton; }
  WinBorder* CreateBorder() { return new WinBorder; }
}
AbstractFactory* fac = new MacFactory;  // 客户端代码与对象的创建分离开来
Button* button = fac->CreateButton();

Decorator

class ITheme  {
public:
  virtual string name();
  virtual bool apply();
}
class DarkTheme  {
public:
  DarkTheme(ITheme* theme) : itheme_(theme) {}
  virtual string name();
  virtual bool apply() { itheme_->apply(); /* + myself decorator */ }
private:
  ITheme* itheme_;
}

Observer

一对多的关系!客户(observer)向报社(subject,数据源)订阅报纸就类似于观察者模式

class Observer;
class Subject  {
  list<Observer*> observers_;
  long status_;  // 感兴趣的东西
public:
  Subject ();
  virtual ~Subject ();
  void attach(Observer* o) { observers_.push_back(o); };
  void detach(Observer* o) { observers_.remove(o); };
  void nofity() {
    for (list<Observer*>::iterator it=observers_.begin(); it!=observers_.end(); it++) {
      (*it)->update();
    }
  };
  virtual void set_message(long s) { status_ = s; };  // 发布新东西给所有订阅者
  virtual long get_message() { return status_; };  // 接收新东西,所有订阅者的update可能会调用
};
class Observer  {
  Subject* subject_;
public:
  Observer (Subject* s) { subject_ = s; };
  virtual ~Observer ();   
  virtual void update() {  // 查看是否有新的订阅
    subject_->get_message();
  };
}

Strategy

class ReplaceAlgorithm  {
public:
  ReplaceAlgorithm ();
  virtual ~ReplaceAlgorithm ();
};
class LRU_ReplaceAlgorithm : public ReplaceAlgorithm  {
public:
  LRU_ReplaceAlgorithm ();
  virtual ~LRU_ReplaceAlgorithm ();
  void repalce() { cout<<"LRU replace algorithm"<<endl; };
};
class FIFO_RepalceAlgorithm : public ReplaceAlgorithm  {
public:
  FIFO_RepalceAlgorithm ();
  virtual ~FIFO_RepalceAlgorithm ();
  void repalce() { cout<<"FIFO repalce algorithm"<<endl; };
};
class Random_ReplaceAlgorithm : public RepalceAlgoritm  {
public:
  Random_ReplaceAlgorithm ();
  virtual ~Random_ReplaceAlgorithm ();
  void replace() { cout<<"Random repalce algorithm"<<endl; };
}
// 定义客户直接使用的类,该类负责调用具体某个算法
template <typename RA>
class Cache  {
public:
  Cache () {};
  virtual ~Cache () {};
  void replace() { m_replaceAlgorithm.replace(); };
private:
  RA m_replaceAlgorithm;
}
Cache<Random_ReplaceAlgorithm> ch;
ch.replace();

Chain of Responsibility

如果某件事需要多重的处理,具体怎么处理是动态变化的;则首先应该划分该处理的类别,责任链可以是一条直线、一个环、一个树.

Singleton

单例模式一般无法使用继承.

如果非要一堆公共的东西,则可以使用子类去继承那一堆基类.子类单独套用单例模式!

class Singleton final
{
  Singleton() = default;
  ~Singleton() = default;
  Singleton(Singleton const&) = delete;
  Singleton& operator = (Singleton const&) = delete;
  Singleton(Singleton&&) = delete;
  Singleton& operator = (Singleton&&) = delete;
public:
  static Singleton& instance() {
    static Singleton ins;
    return ins;
  }
}