外观者模式 :
根据迪米特法则 ,外界知道越少越好。
例如系统启动的时候,会启动很多相应的子系统。这个时候提供一个类去启动这个系统。
C++代码实现:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Audio {
public :
void on() {
cout <<"音响打开"<<endl;
}
void off() {
cout <<"音响关闭"<<endl;
}
};
class TV {
public :
void on() {
cout<<"电视打开"<<endl;
}
void off() {
cout<<"电视关闭"<<endl;
}
};
class KTVMode {
private :
TV* tv;
Audio* audio;
public :
KTVMode() {
this->tv = new TV();
this->audio = new Audio();
}
void mode() {
this->tv->off();
this->audio->on();
}
};
void test() {
KTVMode* ktvMode = new KTVMode();
ktvMode->mode();
}
int main() {
test();
return 0;
}