设计模式之七外观模式

来源:互联网 发布:仙侣奇缘3 数据库密码 编辑:程序博客网 时间:2024/05/02 02:25



//外观模式:给接口提供一致的外观//当客户需要使用到子系统的多个功能时,客户如果逐个去调用每一个功能会引起较大代码量;//并存在潜在的错误可能性;//如果这些功能都位于不同的类中,客户需要去了解每一个类//解决方法是:为所有类的接口提供一个一致的接口,这一接口提供了所有用户需要的操作;//通俗理解:木兰从军,需要买各种从军用品如马、马鞍、配剑、靴子等,这些东西都不在同一个店里,从而导致木兰东奔西走;//现在如果有一家专门的军需用品店,那木兰在这家店里就可以买到所有需要的物品#include<iostream>using namespace std;//功能一class SubSystemOne{public:void methodOne(){cout<<"method One"<<endl;}};//功能二class SubSystemTwo{public:void methodTwo(){cout<<"method Two"<<endl;}};//功能三class SubSystemThree{public:void methodThree(){cout<<"method Three"<<endl;}};//提供一致的功能接口class Facade{public:void methodA(){systemOne.methodOne();systemTwo.methodTwo();systemThree.methodThree();cout<<"Three Operation"<<endl;}void methodB(){systemOne.methodOne();cout<<"Just one operation"<<endl;}private:SubSystemOne systemOne;SubSystemTwo systemTwo;SubSystemThree systemThree;};i:nt main(){Facade facade;facade.methodA();facade.methodB();return 0;}


0 0
原创粉丝点击