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;
}

总结

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

相关推荐
MZ_ZXD00118 分钟前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
wfserial31 分钟前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech
A星空1231 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫2 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
会叫的恐龙2 小时前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米6012 小时前
C++顺序表和vector
开发语言·c++·算法
独望漫天星辰2 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++
王老师青少年编程3 小时前
2024年信奥赛C++提高组csp-s初赛真题及答案解析(阅读程序第3题)
c++·题解·真题·csp·信奥赛·csp-s·提高组
凡人叶枫3 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
CSDN_RTKLIB3 小时前
使用三方库头文件未使用导出符号情景
c++