c++之外观模式

外观模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

定义:为子系统中的一组接口提供一个一致的接口,使得这些子系统更加容易被使用

栗子场景:最近股市特别火,小王也尝试着去买卖一下股票,试一下当操盘手的感觉

代码:

#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块钱

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值