标签:
1.意图
为子系统中的一组接口提供一个一致的接口,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
2.动机
将一个系统划分成若干子系统有利于降低系统的复杂性。一个常见的设计目标是使子系统间的通信和相互依赖达到最小。达到该目标的途径之一是引入一个外观对象,它为子系统提供了一个统一的接口。
3.适用性
4.结构
5.代码实例
//Facade.h #include <memory> class House; class FacadeHouse { public: FacadeHouse(); void Build(); private: std::shared_ptr<House> m_ptrHouse; };
//House.h #include <string> class House { public: House(); void Bulid(); void SetDoorColor(std::string sColor); void SetFloorColor(std::string sColor); void SetBedColor(std::string sColor); private: std::string m_sDoorColor; std::string m_sFloorColor; std::string m_sBedColor; };
//Facade.cpp #include "Facade.h" #include "House.h" FacadeHouse::FacadeHouse() { m_ptrHouse = std::shared_ptr<House>(new House); } void FacadeHouse::Build() { m_ptrHouse->Bulid(); }
//House.cpp #include <iostream> #include "House.h" House::House() : m_sDoorColor("Red"), m_sFloorColor("Red"), m_sBedColor("Red") { } void House::Bulid() { std::cout<< "Build House with DoorColor: " << m_sDoorColor <<std::endl; std::cout<< "Build House with FloorColor: " << m_sFloorColor <<std::endl; std::cout<< "Build House with BedColor: " << m_sBedColor <<std::endl; } void House::SetDoorColor(std::string sColor) { m_sDoorColor = sColor; } void House::SetFloorColor(std::string sColor) { m_sFloorColor = sColor; } void House::SetBedColor(std::string sColor) { m_sBedColor = sColor; }
//Client.cpp #include <memory> #include <iostream> #include "House.h" #include "Facade.h" int main() { std::cout<<"Facade Default House:" << std::endl; std::shared_ptr<FacadeHouse> pFacadeHouse(new FacadeHouse); pFacadeHouse->Build(); std::cout<<"User define house:" << std::endl; std::shared_ptr<House> pHouse(new House); pHouse->SetBedColor("Green"); pHouse->SetDoorColor("Green"); pHouse->SetFloorColor("Green"); pHouse->Bulid(); while(1); }
6.测试结果
7.效果
标签:
原文地址:http://www.cnblogs.com/mgp200866130/p/5551170.html