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

2014-11-24 11:48:37 · 作者: · 浏览: 8
trategyb.h"

int main()
{
Context *context;

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

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

return 0;
}
#include
#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
construct Methods
construct MethodStrategyB
Strategy B
construct Methods
construct MethodStrategyA
Strategy A
construct Methods
construct MethodStrategyB
Strategy B

【结果分析】

利用多态特性,实现了不同策略的调用,由于是测试代码,本身很简单,省略了析构函数。

【实例剖析】

*本例来源于《重构-改善既有代码的设计》第1章。原例采用Java语言编写,本例利用Qt C++进行了改写。

看下面一个关于电影租赁计价的例子。

movie.h

[html]
#ifndef MOVIE_H
#define MOVIE_H

#include
enum {REGULAR = 0,NEW_RELEASE = 1,DISCOUNT = 2};

class Movie
{
public:
Movie();
Movie(QString title, int priceCode);

private:
QString _title;
int _priceCode;

public:
QString getTitle();
int getPriceCode();
void setPriceCode(int arg);
};

#endif // MOVIE_H
#ifndef MOVIE_H
#define MOVIE_H

#include
enum {REGULAR = 0,NEW_RELEASE = 1,DISCOUNT = 2};

class Movie
{
public:
Movie();
Movie(QString title, int priceCode);

private:
QString _title;
int _priceCode;

public:
QString getTitle();
int getPriceCode();
void setPriceCode(int arg);
};

#endif // MOVIE_H

movie.cpp

[html]
#include
#include "movie.h"

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

Movie::Movie(QString title, int priceCode)
{
qDebug()< _title = title;
_priceCode = priceCode;
}

QString Movie::getTitle()
{
return _title;
}

int Movie::getPriceCode()
{
return _priceCode;
}

void Movie::setPriceCode(int arg)
{
_priceCode = arg;
}
#include
#include "movie.h"

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

Movie::Movie(QString title, int priceCode)
{
qDebug()< _title = title;
_priceCode = priceCode;
}

QString Movie::getTitle()
{
return _title;
}

int Movie::getPriceCode()
{
return _priceCode;
}

void Movie::setPriceCode(int arg)
{
_priceCode = arg;
}

rental.h

[html]
#ifndef RENTAL_H
#define RENTAL_H

#include "movie.h"

class Rental
{
public:
Rental(Movie movie, int daysRented);

private:
Movie _movie;
int _daysRented;

public:
Movie getMovie();
int getDaysRented();
double getCharge(int daysRented);
};

#endif // RENTAL_H
#ifndef RENTAL_H
#define RENTAL_H

#include "movie.h"

class Rental
{
public:
Rental(Movie movie, int daysRented);

private:
Movie _movie;
int _daysRented;

public:
Movie getMovie();
int getDaysRented();
double getCharge(int daysRented);
};

#endif // RENTAL_H

rental.cpp

[html]
#include
#include "rental.h"

Rental::Rental(Movie movie, int daysRented)
{
qDebug()<<"construct Rental";
_movie = movie;
_daysRented = daysRented;
}

Movie Rental::getMovie()
{
return _movie;
}

int Rental::getDaysRented()
{
return _daysRented;
}

double Rental::getCharge(int daysRented)
{
double result = 0;
qDebug()< switch(getMovie().getPriceCode())
{
case REGULAR:
result +=2;