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

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

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

【UML图】

 

图1 UML图

(1) 定义了一个Methods基类,包含一个策略的接口MethodsInte易做图ce。

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

(3) 定义了一个策略构建者Context,包含ContextInte易做图ce方法。

 

【示例代码】

methods.h

[html]
#ifndef METHODS_H 
#define METHODS_H 
 
class Methods 

public: 
    Methods(); 
 
public: 
    virtual void MethodsInte易做图ce(); 
}; 
 
#endif // METHODS_H 
#ifndef METHODS_H
#define METHODS_H

class Methods
{
public:
    Methods();

public:
    virtual void MethodsInte易做图ce();
};

#endif // METHODS_H
 
methods.cpp

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

    qDebug()<<"construct Methods"; 

 
void Methods::MethodsInte易做图ce() 

    qDebug()<<"Methods::MethodsInte易做图ce"; 

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

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

void Methods::MethodsInte易做图ce()
{
    qDebug()<<"Methods::MethodsInte易做图ce";
}
 
methodstrategya.h

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

public: 
    MethodStrategyA(); 
 
public: 
    void MethodsInte易做图ce(); 
}; 
 
#endif // METHODSTRATEGYA_H 
#ifndef METHODSTRATEGYA_H
#define METHODSTRATEGYA_H

#include "methods.h"

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

public:
    void MethodsInte易做图ce();
};

#endif // METHODSTRATEGYA_H
 
methodstrategya.cpp

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

    qDebug()<<"construct MethodStrategyA"; 

 
void MethodStrategyA::MethodsInte易做图ce() 

    qDebug()<<"Strategy A"; 

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

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

void MethodStrategyA::MethodsInte易做图ce()
{
    qDebug()<<"Strategy A";
}
 
methodstrategyb.h

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

public: 
    MethodStrategyB(); 
 
public: 
    void MethodsInte易做图ce(); 
}; 
 
#endif // METHODSTRATEGYB_H 
#ifndef METHODSTRATEGYB_H
#define METHODSTRATEGYB_H

#include "methods.h"

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

public:
    void MethodsInte易做图ce();
};

#endif // METHODSTRATEGYB_H
 
methodstrategyb.cpp

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

    qDebug()<<"construct MethodStrategyB"; 

 
void MethodStrategyB::MethodsInte易做图ce() 

    qDebug()<<"Strategy B"; 

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

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

void MethodStrategyB::MethodsInte易做图ce()
{
    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 ContextInte易做图ce(); 
}; 
 
#endif // CONTEXT_H 
#ifndef CONTEXT_H
#define CONTEXT_H

#include "methods.h"

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

private:
    Methods *_method;

public:
    void ContextInte易做图ce();
};

#endif // CONTEXT_H
 
context.cpp

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

    _method = method; 

 
void Context::ContextInte易做图ce() 

    _method->MethodsInte易做图ce(); 

#include "context.h"

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

void Context::ContextInte易做图ce()
{
    _method->MethodsInte易做图ce();
}
 
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->ContextInte易做图ce(); 
 
    context = new Context(new MethodStrategyB); 
    context->ContextInte易做图ce(); 
 
    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->ContextInte易做图ce();

    context = new Context(new MethodStrategyB);
    context->ContextInte易做图ce();

    return 0;
}
 
 

【运行结果】

[html]
construct Methods  
construct MethodStrategyA  
Strategy A  
construc

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,