当前位置:编程学习 > C/C++ >>

设计模式(3)-策略模式(Strategy)

【描述】策略模式本质上利用的是面向对象的多态特性,构建者不必自身包含实现的逻辑,而是根据需要利用其他对象中的算法。

【UML图】

 

图1 UML图

(1) 定义了一个Methods基类,包含一个策略的接口MethodsInterface。

(2) 定义了MethodsStrategyA、MethodsStrategyB两种策略。

(3) 定义了一个策略构建者Context,包含ContextInterface方法。

 

【示例代码】

methods.h

[html]
#ifndef METHODS_H 
#define METHODS_H 
 
class Methods 

public: 
    Methods(); 
 
public: 
    virtual void MethodsInterface(); 
}; 
 
#endif // METHODS_H 
#ifndef METHODS_H
#define METHODS_H

class Methods
{
public:
    Methods();

public:
    virtual void MethodsInterface();
};

#endif // METHODS_H
 
methods.cpp

[html]
#include <QDebug> 
#include "methods.h" 
 
Methods::Methods() 

    qDebug()<<"construct Methods"; 

 
void Methods::MethodsInterface() 

    qDebug()<<"Methods::MethodsInterface"; 

#include <QDebug>
#include "methods.h"

Methods::Methods()
{
    qDebug()<<"construct Methods";
}

void Methods::MethodsInterface()
{
    qDebug()<<"Methods::MethodsInterface";
}
 
methodstrategya.h

[html]
#ifndef METHODSTRATEGYA_H 
#define METHODSTRATEGYA_H 
 
#include "methods.h" 
 
class MethodStrategyA : public Methods 

public: 
    MethodStrategyA(); 
 
public: 
    void MethodsInterface(); 
}; 
 
#endif // METHODSTRATEGYA_H 
#ifndef METHODSTRATEGYA_H
#define METHODSTRATEGYA_H

#include "methods.h"

class MethodStrategyA : public Methods
{
public:
    MethodStrategyA();

public:
    void MethodsInterface();
};

#endif // METHODSTRATEGYA_H
 
methodstrategya.cpp

[html]
#include <QDebug> 
#include "methodstrategya.h" 
 
MethodStrategyA::MethodStrategyA() 

    qDebug()<<"construct MethodStrategyA"; 

 
void MethodStrategyA::MethodsInterface() 

    qDebug()<<"Strategy A"; 

#include <QDebug>
#include "methodstrategya.h"

MethodStrategyA::MethodStrategyA()
{
    qDebug()<<"construct MethodStrategyA";
}

void MethodStrategyA::MethodsInterface()
{
    qDebug()<<"Strategy A";
}
 
methodstrategyb.h

[html]
#ifndef METHODSTRATEGYB_H 
#define METHODSTRATEGYB_H 
 
#include "methods.h" 
 
class MethodStrategyB : public Methods 

public: 
    MethodStrategyB(); 
 
public: 
    void MethodsInterface(); 
}; 
 
#endif // METHODSTRATEGYB_H 
#ifndef METHODSTRATEGYB_H
#define METHODSTRATEGYB_H

#include "methods.h"

class MethodStrategyB : public Methods
{
public:
    MethodStrategyB();

public:
    void MethodsInterface();
};

#endif // METHODSTRATEGYB_H
 
methodstrategyb.cpp

[html] ‘
#include <QDebug> 
#include "methodstrategyb.h" 
 
MethodStrategyB::MethodStrategyB() 

    qDebug()<<"construct MethodStrategyB"; 

 
void MethodStrategyB::MethodsInterface() 

    qDebug()<<"Strategy B"; 

#include <QDebug>
#include "methodstrategyb.h"

MethodStrategyB::MethodStrategyB()
{
    qDebug()<<"construct MethodStrategyB";
}

void MethodStrategyB::MethodsInterface()
{
    qDebug()<<"Strategy B";
}
 
context.h

[html]
#ifndef CONTEXT_H 
#define CONTEXT_H 
 
#include "methods.h" 
 
class Context 

public: 
    Context(Methods *method); 
 
private: 
    Methods *_method; 
 
public: 
    void ContextInterface(); 
}; 
 
#endif // CONTEXT_H 
#ifndef CONTEXT_H
#define CONTEXT_H

#include "methods.h"

class Context
{
public:
    Context(Methods *method);

private:
    Methods *_method;

public:
    void ContextInterface();
};

#endif // CONTEXT_H
 
context.cpp

[html]
#include "context.h" 
 
Context::Context(Methods *method) 

    _method = method; 

 
void Context::ContextInterface() 

    _method->MethodsInterface(); 

#include "context.h"

Context::Context(Methods *method)
{
    _method = method;
}

void Context::ContextInterface()
{
    _method->MethodsInterface();
}
 
main.cpp

[html]
#include <QDebug> 
#include "context.h" 
#include "methods.h" 
#include "methodstrategya.h" 
#include "methodstrategyb.h" 
 
int main() 

    Context *context; 
 
    context = new Context(new MethodStrategyA); 
    context->ContextInterface(); 
 
    context = new Context(new MethodStrategyB); 
    context->ContextInterface(); 
 
    return 0; 

#include <QDebug>
#include "context.h"
#include "methods.h"
#include "methodstrategya.h"
#include "methodstrategyb.h"

int main()
{
    Context *context;

    context = new Context(new MethodStrategyA);
    context->ContextInterface();

    context = new Context(new MethodStrategyB);
    context->ContextInterface();

    return 0;
}
 
 

【运行结果】

[html]
construct Methods  
construct MethodStrategyA  
Strategy A  
construc

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,