电商网络交易平台模拟
策略模式
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//策略,模拟电商平台
class BusinessOnline
{
public:
virtual void Select() = 0;
virtual void Cart() = 0;
virtual void Account() = 0;
virtual void Exchange(BusinessOnline *pBOline) =0;
string OnlineName;
string ServiceTel;
string getOnlineName()
{
return OnlineName;
}
string getServiceTel()
{
return ServiceTel;
}
BusinessOnline(string name,string tel)
{
this->OnlineName = name;
this->ServiceTel = tel;
}
};
class TB:public BusinessOnline
{
public:
virtual void Select()
{
cout<<"当前界面在淘宝中购买商品"<<endl;
}
virtual void Cart()
{
cout<<"将当前商品添加到淘宝购物车中"<<endl;
}
virtual void Account(){
cout<<"结算当前淘宝购物车中的商品"<<endl;
}
virtual void Exchange(BusinessOnline *pBOline)
{
cout<<"淘宝中商品已经售空,可前往 "<<pBOline->getOnlineName()<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问淘宝客服,电话为:"<<ServiceTel<<endl;
}
TB():BusinessOnline("TB","10011")
{}
};
class JD:public BusinessOnline
{
public:
virtual void Select()
{
cout<<"当前界面在京东中购买商品"<<endl;
}
virtual void Cart()
{
cout<<"将当前商品添加到京东购物车中"<<endl;
}
virtual void Account(){
cout<<"结算当前京东购物车中的商品"<<endl;
}
virtual void Exchange(BusinessOnline *pBOline)
{
cout<<"京东中商品已经售空,可前往 "<<pBOline->getOnlineName()<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问京东客服,电话为:"<<ServiceTel<<endl;
}
JD():BusinessOnline("JB","10022")
{}
};
class PDD:public BusinessOnline
{
public:
virtual void Select()
{
cout<<"当前界面在拼多多中购买商品"<<endl;
}
virtual void Cart()
{
cout<<"将当前商品添加到拼多多购物车中"<<endl;
}
virtual void Account(){
cout<<"结算当前拼多多购物车中的商品"<<endl;
}
virtual void Exchange(BusinessOnline *pBOline)
{
cout<<"拼多多中商品已经售空,可前往 "<<pBOline->getOnlineName()<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问拼多多客服,电话为:"<<ServiceTel<<endl;
}
PDD():BusinessOnline("PDD","10033")
{}
};
int main()
{
TB tb;
JD jd;
PDD pdd;
tb.Select();
cout<<"\n";
tb.Cart();
cout<<"\n";
tb.Exchange(&jd);
cout<<"\n";
tb.Exchange(&pdd);
cout<<"\n";
return 0;
}
分析:
假设之前没有PDD这个平台,那么我们在添加是仅仅需要新创建一个类将基本功能补全,在主函数中进行对象创建即可,对于代码的修改集中于一个位置,即创建PDD类的位置。
非策略模式
#include <iostream>
#include <string.h>
using namespace std;
class TB
{
public:
string OnlineName;
string ServiceTel;
string getOnlineName()
{
return OnlineName;
}
string getServiceTel()
{
return ServiceTel;
}
void Select()
{
cout<<"当前界面在淘宝中购买商品"<<endl;
}
void Cart()
{
cout<<"将当前商品添加到淘宝购物车中"<<endl;
}
void Account()
{
cout<<"结算当前淘宝购物车中的商品"<<endl;
}
void Exchange()
{
cout<<"淘宝中商品已经售空,可前往 "<<"<电商名称>"<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问淘宝客服,电话为:"<<ServiceTel<<endl;
}
TB()
{
this->OnlineName = "TB";
this->ServiceTel = "10011";
}
};
class JD
{
public:
string OnlineName;
string ServiceTel;
string getOnlineName()
{
return OnlineName;
}
string getServiceTel()
{
return ServiceTel;
}
void Select()
{
cout<<"当前界面在京东中购买商品"<<endl;
}
void Cart()
{
cout<<"将当前商品添加到京东购物车中"<<endl;
}
void Account()
{
cout<<"结算当前京东购物车中的商品"<<endl;
}
void Exchange()
{
cout<<"京东中商品已经售空,可前往 "<<"<电商名称>"<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问京东客服,电话为:"<<ServiceTel<<endl;
}
JD()
{
this->OnlineName = "JD";
this->ServiceTel = "10022";
}
};
class PDD
{
public:
string OnlineName;
string ServiceTel;
string getOnlineName()
{
return OnlineName;
}
string getServiceTel()
{
return ServiceTel;
}
void Select()
{
cout<<"当前界面在拼多多中购买商品"<<endl;
}
void Cart()
{
cout<<"将当前商品添加到拼多多购物车中"<<endl;
}
void Account()
{
cout<<"结算当前拼多多购物车中的商品"<<endl;
}
void Exchange()
{
cout<<"拼多多中商品已经售空,可前往 "<<"<电商名称>"<<" 进行购买"<<endl;
cout<<"您也可以致电客服进行询问拼多多客服,电话为:"<<ServiceTel<<endl;
}
PDD()
{
this->OnlineName = "PDD";
this->ServiceTel = "10033";
}
};
int main()
{
int t;
cout<<"请输入选择的平台:(1:淘宝;2:京东;3:拼多多):";
cin>>t;
if(t==1)
{
TB tb;
tb.Select();
cout<<"\n";
tb.Cart();
cout<<"\n";
tb.Exchange();
cout<<"\n";
tb.Exchange();
cout<<"\n";
}
else if(t==2)
{
JD jd;
jd.Select();
cout<<"\n";
jd.Cart();
cout<<"\n";
jd.Exchange();
cout<<"\n";
jd.Exchange();
cout<<"\n";
}
else if(t==3)
{
PDD pdd;
pdd.Select();
cout<<"\n";
pdd.Cart();
cout<<"\n";
pdd.Exchange();
cout<<"\n";
pdd.Exchange();
cout<<"\n";
}
else
cout<<"输入错误"<<endl;
return 0;
}
分析:
如果最初没有拼多多平台,在后来需求中需要添加拼多多平台,那么非策略模式需要修改主函数中的大部分代码,同时创建新的PDD类,这种情况下整个源代码的改动程度较大,同时对于Exchange()函数,其中可以跳转不同平台,不适用策略模式,就需要为每个平台创建一个对应的Exchange()函数,较为繁琐,且容易出错。
因此,策略模式更适合,应用于模式创建。