Java教程

[设计模式]外观模式

本文主要是介绍[设计模式]外观模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

外观模式:为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。

代码如下:

#include <iostream>
using namespace std;

class Television
{
public:
	void on()
	{
		cout << "Tv on" << endl;
	}

	void off()
	{
		cout << "Tv off" << endl;
	}
};

class Light
{
public:
	void on()
	{
		cout << "Light on" << endl;
	}

	void off()
	{
		cout << "Light off" << endl;
	}
};


class Audio
{
public:
	void on()
	{
		cout << "Audio on" << endl;
	}

	void off()
	{
		cout << "Audio off" << endl;
	}
};

class Microphone
{
public:
	void on()
	{
		cout << "Microphone on" << endl;
	}

	void off()
	{
		cout << "Microphone off" << endl;
	}
};


class Dvd
{
public:
	void on()
	{
		cout << "Dvd on" << endl;
	}

	void off()
	{
		cout << "Dvd off" << endl;
	}
};



class KTVMode
{
public:
	KTVMode()
	{
		 pTv = new Television;
		 pLight = new Light;
		 pAudio = new Audio;
	    	pMicrophone = new Microphone;
	 	 pDvd = new Dvd;
	}

	void onKtv()
	{
		pTv->on();
		pLight->on();
		pAudio->on();
		pMicrophone->on();
		pDvd->on();
	}

	void offKtv()
	{
		pTv->off();
		pLight->off();
		pAudio->off();
		pMicrophone->off();
		pDvd->off();
	}

	

private:
	Television *pTv;
	Light *pLight;
	Audio *pAudio;
	Microphone *pMicrophone;
	Dvd *pDvd;

	~KTVMode()
	{
		delete pTv;
		delete pLight;
		delete pAudio;
		delete pMicrophone;
		delete pDvd;
	}
};


void test01()
{
	KTVMode *ktv = new  KTVMode;
	ktv->onKtv();
}

int main()
{
	test01();
	return 0;
}

测试结果:
在这里插入图片描述

这篇关于[设计模式]外观模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!