c++外观模式代码举栗子

本文通过具体的C++代码实例,详细解释了外观模式(Facade Pattern)的应用,旨在帮助开发者更好地理解和运用这一设计模式,提升代码的组织性和易用性。
摘要由CSDN通过智能技术生成

 

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
/*外观模式:就是将复杂的子类系统抽象到同一个接口进行管理。外界只需要通过此接口与子类系统进行交互,
而不必直接与复杂的子类系统进行交互*/
//电视
class Television
{
public:
	void on()
	{
		cout<<"电视打开"<<endl;
	}
	void off()
	{
		cout<<"电视机关闭"<<endl;
	}
};
//音响
class Audio
{
public:
	void on()
	{
		cout<<"音响打开"<<endl;
	}
	void off()
	{
		cout<<"音响关闭"<<endl;
	}
};
//灯
class Light
{
public:
	void on()
	{
		cout<<"灯打开"<<endl;
	}
	void off()
	{
		cout<<"灯关闭"<<endl;
	}
};
//游戏机
class Game
{
public:
	void on()
	{
		cout<<"游戏机打开"<<endl;
	}
	void off()
	{
		cout<<"游戏机关闭"<<endl;
	}
};
class markphone
{
public:
	void on()
	{
		cout<<"麦克风打开"<<endl;
	}
	void off()
	{
		cout<<"麦克风关闭"<<endl;
	}
};
class Dvd
{
public:
	void on()
	{
		cout<<"DVD打开"<<endl;
	}
	void off()
	{
		cout<<"DVD关闭"<<endl;
	}
};

//实现ktv模式:电视打开,灯关掉,音响打开,麦克风打开,dvd打开
class KTVMode
{
public:KTVMode ()
	   {
		   ptv=new Television ;
	       plight=new Light ;
	       paudio=new Audio ;
	       pmark=new markphone ;
	       pdvd=new Dvd ;
	   }
	   void onKTV()
	   {
		   ptv ->on ();
		   plight ->off ();
		   paudio ->on ();
		   pmark ->on ();
		   pdvd ->on ();
	   }
	   void offKTV()
	   {
		    ptv ->off  ();
		   plight ->on  ();
		   paudio ->off  ();
		   pmark ->off();
		   pdvd ->off();
	   }
	   ~KTVMode ()
	   {
			delete ptv	  ;
		    delete plight ;
		    delete paudio ;
		    delete pmark  ;
		    delete pdvd	  ;
	   }
public:
	Television  *ptv;
	Light       *plight;
	Audio       *paudio;
	markphone   *pmark;
	Dvd         *pdvd;
};
//实现游戏模式:电视打开,音响打开,游戏机打开
//
void test01()
{
	KTVMode *pktv=new KTVMode ;
	pktv->onKTV();
}
 
int main()
{
	test01();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值