#ifndef ABSACCOUNT_H
#define ABSACCOUNT_H
class AbsAccount {
public:
virtual double getTotalPrice(double unitPrice,int counts)=0;
virtual ~AbsAccount(){
}//need define when declare.
};
#endif // ABSACCOUNT_H
#ifndef HALFACCOUNT_H
#define HALFACCOUNT_H
#include "AbsAccount.h"
class HalfAccount : public AbsAccount{
public:
HalfAccount();
virtual double getTotalPrice(double unitPrice,int counts);
~HalfAccount(){
}
};
#endif // HALFACCOUNT_H
#include "HalfAccount.h"
double HalfAccount::getTotalPrice(double unitPrice,int counts){
return unitPrice*0.5*counts;
}
HalfAccount::HalfAccount():AbsAccount(){
}
#ifndef PROMOTEACCOUNT_H
#define PROMOTEACCOUNT_H
#include "AbsAccount.h"
class PromoteAccount : public AbsAccount{
public:
PromoteAccount();
virtual double getTotalPrice(double unitPrice,int counts);
~PromoteAccount(){
}
};
#endif // PROMOTEACCOUNT_H
#include "PromoteAccount.h"
double PromoteAccount::getTotalPrice(double unitPrice,int counts){
double d= unitPrice*counts;
if(d>100)
d=d-60;
return d;
}
PromoteAccount::PromoteAccount():AbsAccount(){
}
#ifndef ACCOUNTFACTORY_H
#define ACCOUNTFACTORY_H
#include "AbsAccount.h"
class AccountFactory{
public:
static AbsAccount* createAccount(int type_id);
static const int TYPE_HALF_ACCOUNT=1;
static const int TYPE_PROMOTE_ACCOUNT=2;
};
#endif // ACCOUNTFACTORY_H
#include "AccountFactory.h"
#include "HalfAccount.h"
#include "PromoteAccount.h"
AbsAccount* AccountFactory::createAccount(int type_id){
switch(type_id){
case TYPE_HALF_ACCOUNT:
return new HalfAccount;
case TYPE_PROMOTE_ACCOUNT:
return new PromoteAccount;
default:
return 0;
}
}
#include "AccountFactory.h"
#include <iostream>
using namespace std;
int main(){
double total1,total2;
AbsAccount* promoteimpl=0;
AbsAccount* halfimpl=0;
halfimpl=AccountFactory::createAccount(AccountFactory::TYPE_HALF_ACCOUNT);
if(!halfimpl)
goto exit;
total1=halfimpl->getTotalPrice(10,10);
cout<<"total1:"<<total1<<endl;
delete halfimpl;
promoteimpl=AccountFactory::createAccount(AccountFactory::TYPE_PROMOTE_ACCOUNT);
if(!promoteimpl)
goto exit;
total2=promoteimpl->getTotalPrice(10,10);
cout<<"total2:"<<total2<<endl;
delete promoteimpl;
exit:
return 0;
}