目录
[外观模式(Facade Pattern)](#外观模式(Facade Pattern))
外观模式(Facade Pattern)
外观模式是一种结构型设计模式,它为复杂子系统提供一个更高级的统一接口,使得子系统更容易使用。外观模式隐藏了系统的复杂性,并向客户端提供了一个简化的接口。
通过外观模式,客户端不需要直接与子系统的各个组件交互,而是通过一个外观对象与整个子系统进行交互,从而简化了客户端的操作。
实际应用
计算机启动系统
假设有一个复杂的计算机启动系统,包括CPU、内存、硬盘等多个组件。
cpp
#include <iostream>
// 子系统类:CPU
class CPU {
public:
void freeze() {
std::cout << "Freezing CPU\n";
}
void jump(long position) {
std::cout << "Jumping to position " << position << "\n";
}
void execute() {
std::cout << "Executing instructions\n";
}
};
// 子系统类:内存
class Memory {
public:
void load(long position, const std::string& data) {
std::cout << "Loading data into memory at position " << position << "\n";
}
};
// 子系统类:硬盘
class HardDrive {
public:
std::string read(long lba, int size) {
std::cout << "Reading " << size << " bytes from LBA " << lba << "\n";
return "bootloader";
}
};
// 外观类
class ComputerFacade {
private:
CPU cpu;
Memory memory;
HardDrive hardDrive;
public:
void start() {
cpu.freeze();
memory.load(0, hardDrive.read(0, 1024));
cpu.jump(0);
cpu.execute();
}
};
int main() {
ComputerFacade computer;
computer.start();
return 0;
}
家庭影院系统
假设有一个复杂的家庭影院系统,包括投影仪、音响、DVD播放器等多个组件。
cpp
#include <iostream>
// 子系统类:投影仪
class Projector {
public:
void on() {
std::cout << "Turning on the projector\n";
}
void off() {
std::cout << "Turning off the projector\n";
}
void setInput(const std::string& input) {
std::cout << "Setting projector input to " << input << "\n";
}
};
// 子系统类:音响
class SoundSystem {
public:
void on() {
std::cout << "Turning on the sound system\n";
}
void off() {
std::cout << "Turning off the sound system\n";
}
void setVolume(int level) {
std::cout << "Setting sound system volume to " << level << "\n";
}
};
// 子系统类:DVD播放器
class DVDPlayer {
public:
void on() {
std::cout << "Turning on the DVD player\n";
}
void off() {
std::cout << "Turning off the DVD player\n";
}
void play(const std::string& movie) {
std::cout << "Playing movie: " << movie << "\n";
}
};
// 外观类
class HomeTheaterFacade {
private:
Projector projector;
SoundSystem soundSystem;
DVDPlayer dvdPlayer;
public:
void watchMovie(const std::string& movie) {
std::cout << "Get ready to watch a movie...\n";
projector.on();
projector.setInput("DVD");
soundSystem.on();
soundSystem.setVolume(10);
dvdPlayer.on();
dvdPlayer.play(movie);
}
void endMovie() {
std::cout << "Shutting down movie theater...\n";
projector.off();
soundSystem.off();
dvdPlayer.off();
}
};
int main() {
HomeTheaterFacade homeTheater;
homeTheater.watchMovie("Inception");
homeTheater.endMovie();
return 0;
}
旅行预订系统
假设有一个复杂的旅行预订系统,包括航班预订、酒店预订和租车预订等多个组件。
cpp
#include <iostream>
// 子系统类:航班预订
class FlightBooking {
public:
void bookFlight(const std::string& destination) {
std::cout << "Booking flight to " << destination << "\n";
}
};
// 子系统类:酒店预订
class HotelBooking {
public:
void bookHotel(const std::string& location) {
std::cout << "Booking hotel in " << location << "\n";
}
};
// 子系统类:租车预订
class CarRentalBooking {
public:
void bookCar(const std::string& location) {
std::cout << "Booking car rental in " << location << "\n";
}
};
// 外观类
class TravelFacade {
private:
FlightBooking flightBooking;
HotelBooking hotelBooking;
CarRentalBooking carRentalBooking;
public:
void bookCompleteTrip(const std::string& destination) {
std::cout << "Booking complete trip to " << destination << "...\n";
flightBooking.bookFlight(destination);
hotelBooking.bookHotel(destination);
carRentalBooking.bookCar(destination);
}
};
int main() {
TravelFacade travelFacade;
travelFacade.bookCompleteTrip("Hawaii");
return 0;
}
总结
外观模式可以简化复杂子系统的使用。所以无论是计算机启动系统、家庭影院系统还是旅行预订系统,外观模式都能提供一个简化的接口,使客户端能够更容易地与复杂子系统进行交互。