C++中的外观模式

目录

[外观模式(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;
}

总结

外观模式可以简化复杂子系统的使用。所以无论是计算机启动系统、家庭影院系统还是旅行预订系统,外观模式都能提供一个简化的接口,使客户端能够更容易地与复杂子系统进行交互。

相关推荐
愚润求学14 分钟前
【递归、搜索与回溯】专题一:递归(二)
c++·笔记·算法·leetcode
h汉堡15 分钟前
C/C++内存管理
java·c语言·开发语言·c++·学习
大鱼BIGFISH28 分钟前
C++ 字符格式化输出
c++·字符格式化输出
愚润求学1 小时前
【Linux】基础 IO(一)
linux·运维·服务器·开发语言·c++·笔记
June`1 小时前
专题四:综合练习( 找出所有子集的异或总和再求和)
c++·算法·深度优先·剪枝
越甲八千1 小时前
windowsC++操作ADB
c++·windows·adb
孞㐑¥1 小时前
Linux之进程控制
linux·开发语言·c++·经验分享·笔记
Magnum Lehar1 小时前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎
一丝晨光2 小时前
数值溢出保护?数值溢出应该是多少?Swift如何让整数计算溢出不抛出异常?类型最大值和最小值?
java·javascript·c++·rust·go·c·swift
愚润求学2 小时前
【Linux】简单设计libc库
linux·运维·开发语言·c++·笔记