zoukankan      html  css  js  c++  java
  • 设计模式:strategy模式

    思想:将算法进行抽象,然后使用桥接的模式使用算法的抽象接口,达到算法整体替换的目的

    理解:和桥接模式相同,只是桥接的两边分开的思想不同

    例子:

    class Algrithm  //算法的抽象
    {
    public:
    	virtual void algrithm() = 0;
    };
    
    class AlgrithmA: public Algrithm
    {
    public:
    	void algrithm()
    	{
    		cout << "AlgrithmA" << endl;
    	}
    };
    
    class AlgrithmB: public Algrithm
    {
    public:
    	void algrithm()
    	{
    		cout << "AlgrithmB" << endl;
    	}
    };
    
    class Content
    {
    	Algrithm* pAlgrithm;   //桥接模式
    public:
    	Content(Algrithm* pAlgrithm)
    	{
    		this->pAlgrithm = pAlgrithm;
    	}
    	
    	void done()
    	{
    		this->pAlgrithm->algrithm();
    	}
    };
    
    int main() 
    {
    	Content t1(new AlgrithmA());
    	t1.done();
    	
    	Content t2(new AlgrithmB());
    	t2.done();
    	
    	return 0;
    }
  • 相关阅读:
    leetcode931
    leetcode1289
    leetcode1286
    poj Meteor Shower
    noip杂题题解
    noip2007部分题
    NOIP Mayan游戏
    某模拟题题解
    codevs 1423 骑士
    noip 邮票面值设计
  • 原文地址:https://www.cnblogs.com/chusiyong/p/11433326.html
Copyright © 2011-2022 走看看