设计模式-策略模式

定义一系列算法类,当算法变化时,不影响客户端.
适用场景:类中的成员以方法为主,算法经常变动.
好处:每种算法都有自己的类,可以通过自己的接口单独测试,简化了单元测试.
缺点:客户端需要判断适用何种算法.

// coperation.h
#ifndef COPERATION_H
#define COPERATION_H

class COperation
{
public:
    COperation();
public:
    int m_nFirst;
    int m_nSecond;
    virtual double GetResult();
};

#endif // COPERATION_H

// coperation.cpp
#include "coperation.h"

COperation::COperation()
{
}

double COperation::GetResult()
{
    double dResult = 0;
    return dResult;
}
// caddoperation.h
#ifndef CADDOPERATION_H
#define CADDOPERATION_H

#include "coperation.h"

class CAddOperation : public COperation
{
public:
    CAddOperation();
    CAddOperation(int a, int b);
    virtual double GetResult();
};

#endif // CADDOPERATION_H
// caddoperation.cpp
#include "caddoperation.h"

CAddOperation::CAddOperation()
{

}

double CAddOperation::GetResult()
{
    return m_nFirst + m_nSecond;
}

CAddOperation::CAddOperation(int a, int b)
{
    m_nFirst = a;
    m_nSecond = b;
}
// csuboperation.h
#ifndef CSUBOPERATION_H
#define CSUBOPERATION_H

#include "coperation.h"

class CSubOperation : public COperation
{
public:
    CSubOperation();
    CSubOperation(int a, int b);
    virtual double GetResult();
};

#endif // CSUBOPERATION_H
// csuboperation.cpp
#include "csuboperation.h"

CSubOperation::CSubOperation()
{

}

double CSubOperation::GetResult()
{
    return m_nFirst - m_nSecond;
}

CSubOperation::CSubOperation(int a, int b)
{
    m_nFirst = a;
    m_nSecond = b;
}
// context.h
#ifndef CONTEXT_H
#define CONTEXT_H

#include "coperation.h"

class Context
{
public:
    Context();
private:
    COperation *op;
public:
    Context(COperation *temp);
    double GetResult();
};

#endif // CONTEXT_H
// context.cpp
#include "context.h"

Context::Context()
{
}

Context::Context(COperation *temp)
{
    op = temp;
}

double Context::GetResult()
{
    return op->GetResult();
}

客户端(需要知道具体算法类, 并且做判断):

// main.cpp
#include <iostream>

#include "context.h"
#include "caddoperation.h"
#include "csuboperation.h"

using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    char c;
    cin >> a;
    cin >> b;
    cin >> c;
    switch (c) {
    case '+':
        {
            Context *context = new Context(new CAddOperation(a, b));
            cout << context->GetResult() << endl;
        }
        break;
    case '-':
        {
           Context *context = new Context(new CSubOperation(a, b));
            cout << context->GetResult() << endl;
        }
        break;
    default:
        break;
    }
    return 0;
}

策略模式与工厂模式结合:

// context_with_factory.h
#ifndef CONTEXT_WITH_FACTORY_H
#define CONTEXT_WITH_FACTORY_H

#include "coperation.h"

class Context_with_factory
{
public:
    Context_with_factory();
    Context_with_factory(char c, int a, int b);
    virtual double GetResult();
private:
    COperation *op;
};

#endif // CONTEXT_WITH_FACTORY_H
// context_with_factory.cpp
#include "context_with_factory.h"

#include "caddoperation.h"
#include "csuboperation.h"

Context_with_factory::Context_with_factory()
{
}

Context_with_factory::Context_with_factory(char c, int a, int b)
{
    switch (c) {
    case '+':
        {
            op = new CAddOperation(a, b);
        }
        break;
    case '-':
        {
            op = new CSubOperation(a, b);
        }
        break;
    default:
        {
            op = nullptr;
        }
        break;
    }
}

double Context_with_factory::GetResult()
{
    return op->GetResult();
}

客户端:

// main.cpp
#include <iostream>
#include "context_with_factory.h"

using namespace std;

int main(int argc, char *argv[])
{
    int a, b;
    cin >> a;
    cin >> b;
    Context_with_factory *context_with_factory = new Context_with_factory('+', a, b);
    cout << context_with_factory->GetResult() << endl;

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值