設計模式:外觀模式(7.2)C++版

外觀模式:提供統一的接口來訪問子系統,使子系統更容易使用。


C++示例代碼如下:

#include "stdafx.h"

#include <string>
#include <iostream>

using namespace std;

/*
* CONTENTS: DESIGN PATTERN, FACADE PATTERN
*   AUTHOR: YAO H. WANG
*     TIME: 2013-11-4 10:12:51
*  EDITION: 1
*     LINK: http://blog.csdn.net/yaohwang  
*
* ALL RIGHTS RESERVED!
*/

//家族影院
class DvdPlayer
{
private:
	string movie;
public:
	void on()
	{
		cout << "Top-O-Line DVD Player on" << endl;
	}

	void play()
	{
		cout << "Top-O-Line DVD Player on" << endl;
	}

	void play(string movie)
	{
		this->movie = movie;
		cout << "Top-O-Line DVD Player playing \"" << movie << "\"" << endl;
	}

	void stop()
	{
		cout << "Top-O-Line DVD Player stopped \"" << movie << "\"" << endl;
	}

	void eject()
	{
		cout << "Top-O-Line DVD Player eject" << endl;
	}

	void off()
	{
		cout << "Top-O-Line DVD Player off" << endl;
	}
};

class Amplifier
{
public:
	void on()
	{
		cout << "Top-O-Line Amplifier on" << endl;
	}

	void setDvd(DvdPlayer dvd)
	{
		cout << "Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player" << endl;
	}

	void setSurroundSound()
	{
		cout << "Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer)" << endl;
	}

	void setVolume(int v)
	{
		cout << "Top-O-Line Amplifier setting volume to " << v << endl;
	}

	void off()
	{
		cout << "Top-O-Line Amplifier off" << endl;
	}
};

class Tuner
{

};

class CdPlayer
{

};

class Projector
{
public:
	void on()
	{
		cout << "Top-O-Line Projector on" << endl;
	}

	void wideScreenMode()
	{
		cout << "Top-O-Line Projector in widescreen mode (16x9 aspect ratio)" << endl;
	}

	void off()
	{
		cout << "Top-O-Line Projector off" << endl;
	}
};

class TheaterLights
{
public:
	void dim(int d)
	{
		cout << "Theater Ceiling Lights dimming to " << d << "%" << endl;
	}

	void on()
	{
		cout << "Theater Ceiling Lights on" << endl;
	}
};

class Screen
{
public:
	void down()
	{
		cout << "Theater Screen going down" << endl;
	}

	void up()
	{
		cout << "Theater screen going up" << endl;
	}
};

class PopcornPopper
{
public:
	void on()
	{
		cout << "Popcorn Popper on" << endl;
	}

	void pop()
	{
		cout << "Popcorn Popper popping popcorn!" << endl;
	}

	void off()
	{
		cout << "Popcorn Popper off" << endl;
	}
};

//外觀
class HomeTheaterFacade
{
private:
	Amplifier amp;
	Tuner tuner;
	DvdPlayer dvd;
	CdPlayer cd;
	Projector projector;
	TheaterLights lights;
	Screen screen;
	PopcornPopper popper;

public:
	HomeTheaterFacade(
			Amplifier amp,
			Tuner tuner,
			DvdPlayer dvd,
			CdPlayer cd,
			Projector projector,
			TheaterLights lights,
			Screen screen,
			PopcornPopper popper)
	{
		this->amp = amp;
		this->tuner = tuner;
		this->dvd = dvd;
		this->cd = cd;
		this->projector = projector;
		this->lights = lights;
		this->screen = screen;
		this->popper = popper;
	}

	void WatchMovie(string movie)
	{
		cout << "Get ready to watch a movie..." << endl;
		popper.on();
		popper.pop();
		lights.dim(10);
		screen.down();
		projector.on();
		projector.wideScreenMode();
		amp.on();
		amp.setDvd(dvd);
		amp.setSurroundSound();
		amp.setVolume(5);
		dvd.on();
		dvd.play(movie);
	}

	void endMovie()
	{
		cout << "Shutting movie theater down..." << endl;
		popper.off();
		lights.on();
		screen.up();
		projector.off();
		amp.off();
		dvd.stop();
		dvd.eject();
		dvd.off();
	}
};

//測試
void main()
{
	Amplifier amp;
	Tuner tuner;
	DvdPlayer dvd;
	CdPlayer cd;
	Projector projector;
	TheaterLights lights;
	Screen screen;
	PopcornPopper popper;

	HomeTheaterFacade homeTheater(amp, tuner, dvd, cd, projector, lights, screen, popper);
	homeTheater.WatchMovie("Raiders of the Lost Ark");
	homeTheater.endMovie();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章