外观模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
定义:为子系统中的一组接口提供一个一致的接口,使得这些子系统更加容易被使用
栗子场景:最近股市特别火,小王也尝试着去买卖一下股票,试一下当操盘手的感觉
代码:
#include <iostream>
using namespace std;
//股票A
class SharesA{
public:
void buySharesA()
{
cout << "股票A买入" << endl;
}
void sellSharesA()
{
cout << "股票A卖出" << endl;
}
};
//股票B
class SharesB{
public:
void buySharesB()
{
cout << "股票B买入" << endl;
}
void sellSharesB()
{
cout << "股票B卖出" << endl;
}
};
//股票C
class SharesC{
public:
void buySharesC()
{
cout << "股票C买入" << endl;
}
void sellSharesC()
{
cout << "股票C卖出" << endl;
}
};
//外观类--小王
class XiaoWangFacade{
public:
//买入股票的组合1
void buyMethodOne()
{
a.buySharesA();
b.buySharesB();
}
void sellMethodOne()
{
a.sellSharesA();
b.sellSharesB();
}
//买入股票的组合2
void buymethodTwo()
{
b.buySharesB();
c.buySharesC();
}
void sellMethodTwo()
{
b.sellSharesB();
c.sellSharesC();
}
private:
SharesA a;
SharesB b;
SharesC c;
};
//客户端
int main()
{
XiaoWangFacade xmf;
//小王买入一组股票A与B
xmf.buyMethodOne();
//小王卖出一组股票A与B
xmf.sellMethodOne();
//小王买入一组股票B和C
xmf.buymethodTwo();
//小王卖出一组股票B和C
xmf.sellMethodTwo();
return 0;
}
小王这几天买卖股票,在牛市里追涨杀跌,结果还是亏损了1000块钱