zoukankan      html  css  js  c++  java
  • 设计模式-Strategy Strategy将算法封装到类中,通过组合的方式 将具体算法的实现在组合对象中实现

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

    //strategy.h

    #pragma once
    class Strategy
    {
    public:
        Strategy();
        virtual ~Strategy();
        virtual void AlgrithmInference() = 0;
    protected:
    private:
    };
    
    class ConcreateStrategyA :public Strategy
    {
    public:
        ConcreateStrategyA();
        virtual ~ConcreateStrategyA();
        void AlgrithmInference();
    protected:
    private:
    };
    
    class ConcreateStrategyB :public Strategy
    {
    public:
        ConcreateStrategyB();
        virtual ~ConcreateStrategyB();
        void AlgrithmInference();
    protected:
    private:
    };

    //strategy.cpp

    #include"strategy.h"
    #include<iostream>
    
    Strategy::Strategy(){}
    Strategy::~Strategy()
    {
        std::cout << "~Strategy....." << std::endl;
    }
    void Strategy::AlgrithmInference()
    {
    
    }
    
    ConcreateStrategyA::ConcreateStrategyA(){}
    ConcreateStrategyA::~ConcreateStrategyA()
    {
        std::cout << "~ConcreateStrategyA" << std::endl;
    }
    
    void ConcreateStrategyA::AlgrithmInference()
    {
        std::cout << "test ConcreateStrategyA" << std::endl;
    }
    
    ConcreateStrategyB::ConcreateStrategyB(){}
    ConcreateStrategyB::~ConcreateStrategyB()
    {
        std::cout << "~ConcreateStrategyB" << std::endl;
    }
    void ConcreateStrategyB::AlgrithmInference()
    {
        std::cout << "test ConcreateStrategyB...." << std::endl;
    }

    //Context.h

    #pragma once
    
    class Strategy;
    //Strategy模式与Template模式的根本区别是 Strategy通过 组合(委托)方式实现算法异构,Template使用继承的方式
    class Context
    {
    public:
        Context(Strategy* stg);
        virtual ~Context();
        void DoAction();
    protected:
    private:
        Strategy* _stg;
    };

    //Context.cpp

    #include"Context.h"
    #include"strategy.h"
    #include<iostream>
    
    Context::Context(Strategy* stg)
    {
        _stg = stg;
    }
    Context::~Context()
    {
        if (!_stg)
            delete _stg;
    }
    void Context::DoAction()
    {
        _stg->AlgrithmInference();
    }

    //main.cpp

    #include"Context.h"
    #include"strategy.h"
    #include<iostream>
    #include<string>
    
    int main(int args, char* argv)
    {
        Strategy* ps = new ConcreateStrategyA();
        Context* pc = new Context(ps);
        pc->DoAction();
        if (NULL != pc)
            delete pc;
        getchar();
        return 0;
    }
  • 相关阅读:
    zabbix 添加 微信、邮件 媒介详解
    zabbix使用之常用功能使用心得
    Nginx1.8源码包编译安装
    httpd启动显示Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName'
    apache-httpd2.4编译安装
    apache-httpd2.2编译安装
    MYSQL5.7源码包编译安装
    MYSQL常见安装错误集:[ERROR] --initialize specified but the data directory has files in it. Abort
    MYSQL启用数据库错误:ERROR 2002 (HY000)
    编译mysql时CMake Error at cmake/readline.cmake:85 (MESSAGE)
  • 原文地址:https://www.cnblogs.com/fourmi/p/12079671.html
Copyright © 2011-2022 走看看