• 2.策略模式


    UML图

    在这里插入图片描述

    代码

    main.cpp

    #include "Strategy.h"
    #include "Context.h"
    
    void test()
    {
    	Context* pContext = nullptr;
    	
    	/* StrategyA */
    	pContext = new Context(new StrategyA());
    	pContext->contextInterface();
    
    	/* StrategyB */
    	pContext = new Context(new StrategyB());
    	pContext->contextInterface();
    
    	/* StrategyC */
    	pContext = new Context(new StrategyC());
    	pContext->contextInterface();
    
    	delete pContext;
    	pContext = nullptr;
    }
    
    int main()
    {
    	test();
    	system("pause");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    Strategy.h

    #pragma once
    #include 
    using namespace std;
    
    /*
    策略模式:定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的用户。
    */
    
    /* 抽象策略基类:定义所有支持算法的公共接口 */
    class Strategy
    {
    public:
    	virtual void algorithmInterface() = 0;
    };
    
    /* 策略A */
    class StrategyA :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法A实现" << endl;
    	}
    };
    
    /* 策略B */
    class StrategyB :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法B实现" << endl;
    	}
    };
    
    /* 策略C */
    class StrategyC :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法C实现" << endl;
    	}
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    Context.h

    #pragma once
    #include "Strategy.h"
    
    /*
    笔者感受:使用类C操控类A继承体系A1,A2,A3中公有对外暴露接口。
    利用一个额外的类,(1)将strategy抽象策略基类,作为额外类的入参;
    (2)将strategy抽象策略基类作为额外类的成员变量,利用多态原理,接收外面传来的具体抽象策略。
    (3)对外暴露的接口中,使用成员变量抽象策略基类,调用策略继承体系中的子类们都需要继承的纯虚函数接口。
    即可,使用额外类操控策略继承体系中公有对外暴露的接口了。
    */
    class Context
    {
    public:
    	Context(Strategy* pStrategy) :m_pStrategy(pStrategy) {}
    	void contextInterface();
    private:
    	Strategy* m_pStrategy{ nullptr };
    };
    
    void Context::contextInterface()
    {
    	m_pStrategy->algorithmInterface();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    策略模式+简单工厂

    • 优点:策略模式+简单工厂:可以完全将策略继承体系与用户端完全剥离开来,将策略继承体系完全封装起来,对用户完全不可见。
    • 总结
      • 类C通过没什么信息含量的枚举作为入参,利用简单工厂生成类A继承体系中的各子类A1、A2、A3。同时,用基类A作为类C的成员变量,接一下刚生成的类A的子类。
      • 类C对外统一暴露一个接口,该接口中,类C的成员变量类A调用继承体系公有对外暴露的接口func()。

    main.cpp

    #include "StrategyFactory.h"
    
    /*
    策略方法+简单工厂:可以将策略继承体系完全剥离开来,完全封装起来,对用户完全不可见。
    */
    
    void test()
    {
    	StrategyFactory* pStrategyFactory = nullptr;
    	
    	/* StrategyA */
    	pStrategyFactory = new StrategyFactory(StrategyType::eStrategyA);
    	pStrategyFactory->contextInterface();
    
    	/* StrategyB */
    	pStrategyFactory = new StrategyFactory(StrategyType::eStrategyB);
    	pStrategyFactory->contextInterface();
    
    	/* StrategyC */
    	pStrategyFactory = new StrategyFactory(StrategyType::eStrategyC);
    	pStrategyFactory->contextInterface();
    
    	delete pStrategyFactory;
    	pStrategyFactory = nullptr;
    }
    
    int main()
    {
    	test();
    	system("pause");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    Strategy.h

    #pragma once
    #include 
    using namespace std;
    
    /*
    策略模式:定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的用户。
    */
    
    /* 抽象策略基类:定义所有支持算法的公共接口 */
    class Strategy
    {
    public:
    	virtual void algorithmInterface() = 0;
    };
    
    /* 策略A */
    class StrategyA :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法A实现" << endl;
    	}
    };
    
    /* 策略B */
    class StrategyB :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法B实现" << endl;
    	}
    };
    
    /* 策略C */
    class StrategyC :public Strategy
    {
    public:
    	virtual void algorithmInterface()override
    	{
    		cout << "算法C实现" << endl;
    	}
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    StrategyFactory.h

    #pragma once
    #include "Strategy.h"
    
    /*
    我的感受:使用类C操控类A继承体系A1,A2,A3中公有对外暴露接口。
    利用一个额外的类,(1)将strategy抽象策略基类,作为额外类的入参;
    (2)将strategy抽象策略基类作为额外类的成员变量,利用多态原理,接收外面传来的具体抽象策略。
    (3)对外暴露的接口中,使用成员变量抽象策略基类,调用策略继承体系中的子类们都需要继承的纯虚函数接口。
    即可,使用额外类操控策略继承体系中公有对外暴露的接口了。
    */
    enum StrategyType
    {
    	eStrategyA,
    	eStrategyB,
    	eStrategyC
    };
    
    class StrategyFactory
    {
    public:
    	StrategyFactory(StrategyType nType);
    	~StrategyFactory();
    	void contextInterface();
    private:
    	Strategy* m_pStrategy{ nullptr };
    };
    
    StrategyFactory::StrategyFactory(StrategyType nType)
    {
    	switch (nType)
    	{
    	case eStrategyA:
    		m_pStrategy = new StrategyA();
    		break;
    	case eStrategyB:
    		m_pStrategy = new StrategyB();
    		break;
    	case eStrategyC:
    		m_pStrategy = new StrategyC();
    		break;
    	}
    }
    
    StrategyFactory::~StrategyFactory()
    {
    	delete m_pStrategy;
    	m_pStrategy = nullptr;
    }
    
    void StrategyFactory::contextInterface()
    {
    	m_pStrategy->algorithmInterface();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    Spring Security:身份验证入口AuthenticationEntryPoint介绍与Debug分析
    vue.config.js 配置
    重点问题!CPU利用率过高排查思路|原创
    论人类下一代语言的可能—5.1一阶谓词逻辑简述
    丁鹿学堂:promise深入解读(一)
    任务分配——斜率优化dp——运输小猫
    【无标题】
    JVM 内存设置大小(Xms Xmx PermSize MaxPermSize 区别)
    金九银十,23届秋招信息超全汇总表!附各大名企优质岗位内推码!持续更新中···
    iOS卡顿原因与优化
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132939500