19 年时候学OOP(?)时候的post,更新一下记录一下遇到的新的设计模式:

std::enable_if

用 std::enable_if 增加一个新的函数参数
利用 SFINAE (Substitution Failure Is Not An Error)
模板替换失败不会报错,只会使对应模板不存在

template<int val>
void func(int x, typename std::enable_if<val, int>::type y) {
    printf("in func yes %d\n", x);
}

template<int val>
void func(int x, typename std::enable_if<!val, int>::type y) {
    printf("in func no %d\n", x);
}

int main() {
    func<0>(123, 123);
    
}

当 enable_if 为 false 时,std::enable_if<!val, int> 不存在 type 参数,模板替换错误,此时函数不存在

https://www.runoob.com/design-pattern/design-pattern-intro.html

委托模式


class I /*interface*/ {
public:
    virtual void f() = 0;
    virtual void g() = 0;
};

class A : public I {
public:
    void f(){std::cout << "A::f()" << std::endl;}
    void g(){std::cout << "A::g()" << std::endl;}
};

class B : public I {
public:
    void f(){std::cout << "B::f()" << std::endl;}
    void g(){std::cout << "B::g()" << std::endl;}
};


class C : public I {
public:
    C() { m_i = new A();/*delegation*/ }

    void f(){ m_i->f(); }
    void g(){ m_i->g(); }

    // normal attributes
    void toA(){ m_i = new A(); }
    void toB(){ m_i = new B(); }

private:
    I* m_i;
}

工厂模式

public class ShapeFactory {

   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

CRTP

静态多态



template 
struct base
{
    void interface()
    {
         // ...
         static_cast(this)->implementation();
         // ...
    }
};
 
struct derived : base
{
     void implementation()
     {
         // ...
     }
};

visitor模式

将数据与对数据的操作分离


//前向声明,访问者
class Visitor;

//被访问的抽象类,只有一个接口,用来接受访问者
class Object
{
public:
    virtual void accept(Visitor&v) = 0;
};

//具体的被访问的对象,学生。
class StudentA :public Object
{
    std::string name;
    int score;
public:
    StudentA(std::string name_);
    int getScore();
    std::string getName()
    {
        return name;
    }
    void setScore(int num);
    virtual void StudentA::accept(Visitor & v)
    {
        v.visit(this);
    }
};

//抽象访问者,只有一个接口,访问.
class Visitor
{
public:
    virtual void visit(Object* obj)=0;

};

//具体的访问者一,班主任,实现访问(学生并给学分)
class ClassLeader :public Visitor
{
public:
    virtual void visit(Object* obj);
};
//具体的访问者二,校长,实现访问(学生并批评不及格的学生)
class HeadMaster :public Visitor
{
public:
    virtual void visit(Object* obj);
};

observer模式

一些观察者观察一个数据,当数据被改动时通知所有观察者


public class Subject {
   
   private List observers 
      = new ArrayList();
   private int state;
 
   public int getState() {
      return state;
   }
 
   public void setState(int state) {
      this.state = state;
      notifyAllObservers();
   }
 
   public void attach(Observer observer){
      observers.add(observer);      
   }
 
   public void notifyAllObservers(){
      for (Observer observer : observers) {
         observer.update();
      }
   }  
}
public abstract class Observer {
   protected Subject subject;
   public abstract void update();
}
public class BinaryObserver extends Observer{
 
   public BinaryObserver(Subject subject){
      this.subject = subject;
      this.subject.attach(this);
   }
 
   @Override
   public void update() {
      System.out.println( "Binary String: " 
      + Integer.toBinaryString( subject.getState() ) ); 
   }
}

标签: none

添加新评论