• C++中的外观模式


    目录

    外观模式(Facade Pattern)

    实际应用

    计算机启动系统

    家庭影院系统

    旅行预订系统

    总结


    外观模式(Facade Pattern)

    外观模式是一种结构型设计模式,它为复杂子系统提供一个更高级的统一接口,使得子系统更容易使用。外观模式隐藏了系统的复杂性,并向客户端提供了一个简化的接口。

    通过外观模式,客户端不需要直接与子系统的各个组件交互,而是通过一个外观对象与整个子系统进行交互,从而简化了客户端的操作。

    实际应用

    计算机启动系统

    假设有一个复杂的计算机启动系统,包括CPU、内存、硬盘等多个组件。

    1. #include
    2. // 子系统类:CPU
    3. class CPU {
    4. public:
    5. void freeze() {
    6. std::cout << "Freezing CPU\n";
    7. }
    8. void jump(long position) {
    9. std::cout << "Jumping to position " << position << "\n";
    10. }
    11. void execute() {
    12. std::cout << "Executing instructions\n";
    13. }
    14. };
    15. // 子系统类:内存
    16. class Memory {
    17. public:
    18. void load(long position, const std::string& data) {
    19. std::cout << "Loading data into memory at position " << position << "\n";
    20. }
    21. };
    22. // 子系统类:硬盘
    23. class HardDrive {
    24. public:
    25. std::string read(long lba, int size) {
    26. std::cout << "Reading " << size << " bytes from LBA " << lba << "\n";
    27. return "bootloader";
    28. }
    29. };
    30. // 外观类
    31. class ComputerFacade {
    32. private:
    33. CPU cpu;
    34. Memory memory;
    35. HardDrive hardDrive;
    36. public:
    37. void start() {
    38. cpu.freeze();
    39. memory.load(0, hardDrive.read(0, 1024));
    40. cpu.jump(0);
    41. cpu.execute();
    42. }
    43. };
    44. int main() {
    45. ComputerFacade computer;
    46. computer.start();
    47. return 0;
    48. }

    家庭影院系统

    假设有一个复杂的家庭影院系统,包括投影仪、音响、DVD播放器等多个组件。

    1. #include
    2. // 子系统类:投影仪
    3. class Projector {
    4. public:
    5. void on() {
    6. std::cout << "Turning on the projector\n";
    7. }
    8. void off() {
    9. std::cout << "Turning off the projector\n";
    10. }
    11. void setInput(const std::string& input) {
    12. std::cout << "Setting projector input to " << input << "\n";
    13. }
    14. };
    15. // 子系统类:音响
    16. class SoundSystem {
    17. public:
    18. void on() {
    19. std::cout << "Turning on the sound system\n";
    20. }
    21. void off() {
    22. std::cout << "Turning off the sound system\n";
    23. }
    24. void setVolume(int level) {
    25. std::cout << "Setting sound system volume to " << level << "\n";
    26. }
    27. };
    28. // 子系统类:DVD播放器
    29. class DVDPlayer {
    30. public:
    31. void on() {
    32. std::cout << "Turning on the DVD player\n";
    33. }
    34. void off() {
    35. std::cout << "Turning off the DVD player\n";
    36. }
    37. void play(const std::string& movie) {
    38. std::cout << "Playing movie: " << movie << "\n";
    39. }
    40. };
    41. // 外观类
    42. class HomeTheaterFacade {
    43. private:
    44. Projector projector;
    45. SoundSystem soundSystem;
    46. DVDPlayer dvdPlayer;
    47. public:
    48. void watchMovie(const std::string& movie) {
    49. std::cout << "Get ready to watch a movie...\n";
    50. projector.on();
    51. projector.setInput("DVD");
    52. soundSystem.on();
    53. soundSystem.setVolume(10);
    54. dvdPlayer.on();
    55. dvdPlayer.play(movie);
    56. }
    57. void endMovie() {
    58. std::cout << "Shutting down movie theater...\n";
    59. projector.off();
    60. soundSystem.off();
    61. dvdPlayer.off();
    62. }
    63. };
    64. int main() {
    65. HomeTheaterFacade homeTheater;
    66. homeTheater.watchMovie("Inception");
    67. homeTheater.endMovie();
    68. return 0;
    69. }

    旅行预订系统

    假设有一个复杂的旅行预订系统,包括航班预订、酒店预订和租车预订等多个组件。

    1. #include
    2. // 子系统类:航班预订
    3. class FlightBooking {
    4. public:
    5. void bookFlight(const std::string& destination) {
    6. std::cout << "Booking flight to " << destination << "\n";
    7. }
    8. };
    9. // 子系统类:酒店预订
    10. class HotelBooking {
    11. public:
    12. void bookHotel(const std::string& location) {
    13. std::cout << "Booking hotel in " << location << "\n";
    14. }
    15. };
    16. // 子系统类:租车预订
    17. class CarRentalBooking {
    18. public:
    19. void bookCar(const std::string& location) {
    20. std::cout << "Booking car rental in " << location << "\n";
    21. }
    22. };
    23. // 外观类
    24. class TravelFacade {
    25. private:
    26. FlightBooking flightBooking;
    27. HotelBooking hotelBooking;
    28. CarRentalBooking carRentalBooking;
    29. public:
    30. void bookCompleteTrip(const std::string& destination) {
    31. std::cout << "Booking complete trip to " << destination << "...\n";
    32. flightBooking.bookFlight(destination);
    33. hotelBooking.bookHotel(destination);
    34. carRentalBooking.bookCar(destination);
    35. }
    36. };
    37. int main() {
    38. TravelFacade travelFacade;
    39. travelFacade.bookCompleteTrip("Hawaii");
    40. return 0;
    41. }

    总结

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

  • 相关阅读:
    SpringMVC的常用注解,参数传递以及页面跳转的使用
    景区门票管理系统
    微软 AR 眼镜新专利:包含热拔插电池
    javascript案例35——&&
    CAS:295348-87-7_AF594 NHS 活化酯_ Alexa Fluor 594 NHS ester
    [python]basemap后安装后hello world代码
    maven仓库密码加密,对settings.xml中的password进行加密
    IO 框架
    【C/C++】string类的使用&&探索string底层原理
    【访谈】Eotalk Vol.01:Eoapi,我们希望以开源的方式构建 API 生态系统
  • 原文地址:https://blog.csdn.net/GOLOJO/article/details/139596618