外观模式(c++)

#include<iostream>

using namespace std;

class TV {
public:
	void open() {
		cout << "打开电视" << endl;
	}
	void off() {
		cout << "关闭电视" << endl;
	}
};
class Light {
public:
	void open() {
		cout << "打开灯" << endl;
	}
	void off() {
		cout << "关闭灯" << endl;
	}
};
class YinXiang {
public:
	void open() {
		cout << "打开音响" << endl;
	}
	void off() {
		cout << "关闭音响" << endl;
	}
};
class MainKeFeng {
public:
	void open() {
		cout << "打开麦克风" << endl;
	}
	void off() {
		cout << "关闭麦克风" << endl;
	}
};
class DVD {
public:
	void open() {
		cout << "打开DvD" << endl;
	}
	void off() {
		cout << "关闭DvD" << endl;
	}
};
class Youxi {
public:
	void open() {
		cout << "打开游戏机" << endl;
	}
	void off() {
		cout << "关闭游戏机" << endl;
	}
};

//外观
class Control {
public:
	void KTVModel() {
		TV* tv = new TV;
		tv->open();
		Light* l = new Light;
		l->open();
		YinXiang* yx = new YinXiang;
		yx->open();
		MainKeFeng* mkf = new MainKeFeng;
		mkf->open();
		DVD* dvd = new DVD;
		dvd->open();

		delete tv;
		delete l;
		delete yx;
		delete mkf;
		delete dvd;
	}
	void YouxiModel() {
		TV* tv = new TV;
		tv->open();
		
		YinXiang* yx = new YinXiang;
		yx->open();
		
		Youxi* dvd = new Youxi;
		dvd->open();

		delete tv;
		delete yx;
		delete dvd;
	}
};

int main() {
	Control c;
	c.KTVModel();
	cout << "***************************" << endl;
	c.YouxiModel();


	cin.get();
	return 0;
}

  

posted @ 2024-03-25 17:58  dmfsimle  阅读(0)  评论(0编辑  收藏  举报