结构型模式-外观(facade)

外观

为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.

实例

main.cc:

#include 
#include "runner_club.h"

/*
design_pattern:"facade"
Runner club two activitys:
recruit runner (Propaganda System, Human Resources System)
Long-distance race(Public Relation System, organizational Systems)
*/
int main(){
    RunnerClub *runner_club = new RunnerClub();
    runner_club->RecruitRunner();
    runner_club->LongDistanceRace();

    //clear
    delete runner_club;
    system("Pause");
    return 0;
}

RunnerClub:

//runner_club.h
#ifndef HELENDP_SOURCE_RUNNER_CLUB_H_
#define HELENDP_SOURCE_RUNNER_CLUB_H_
#include "human_resource_system.h"
#include "organizational_system.h"
#include "propaganda_system.h"
#include "public_relation_system.h"

class RunnerClub{
public:
    RunnerClub();
    ~RunnerClub();
    void RecruitRunner();
    void LongDistanceRace();
private:
    HumanResourceSystem *human_resource_system_;
    OrganizationalSystem *organizational_system_;
    PropagandaSystem *propaganda_system_;
    PublicRelationSystem *public_relation_system_;

};
#endif


//runner_club.cc
#include "runner_club.h"
#include 
using namespace std;

RunnerClub::RunnerClub(){
    human_resource_system_ = new HumanResourceSystem();
    organizational_system_ = new OrganizationalSystem();
    propaganda_system_ = new PropagandaSystem();
    public_relation_system_ = new PublicRelationSystem();
}

RunnerClub::~RunnerClub(){
    delete human_resource_system_;
    delete organizational_system_;
    delete propaganda_system_;
    delete public_relation_system_;
}

void RunnerClub::RecruitRunner(){
    propaganda_system_->Run();
    human_resource_system_->Run();
}

void RunnerClub::LongDistanceRace(){
    public_relation_system_->Run();
    organizational_system_->Run();
}

PublicRelationSystem:

//public_relation_system.h
#ifndef HELENDP_SOURCE_PUBLIC_RELATION_SYSTEM_H_
#define HELENDP_SOURCE_PUBLIC_RELATION_SYSTEM_H_

class PublicRelationSystem{
public:
    PublicRelationSystem();
    ~PublicRelationSystem();
    void Run();
};
#endif


//public_relation_system.cc
#include "public_relation_system.h"
#include 
using namespace std;

PublicRelationSystem::PublicRelationSystem(){

}

PublicRelationSystem::~PublicRelationSystem(){

}

void PublicRelationSystem::Run(){
    cout << "public relation system run!" << endl;
}

PropagandaSystem:

//propaganda_system.h
#ifndef HELENDP_SOURCE_PROPAGANDA_SYSTEM_H_
#define HELENDP_SOURCE_PROPAGANDA_SYSTEM_H_

class PropagandaSystem{
public:
    PropagandaSystem();
    ~PropagandaSystem();
    void Run();
};
#endif


//propaganda_system.cc
#include "propaganda_system.h"
#include 
using namespace std;

PropagandaSystem::PropagandaSystem(){

}

PropagandaSystem::~PropagandaSystem(){

}

void PropagandaSystem::Run(){
    cout << "propaganda system run!" << endl;
}

OrganizationalSystem:

//organizational_system.h
#ifndef HELENDP_SOURCE_ORGANIZATIONAL_SYSTEM_H_
#define HELENDP_SOURCE_ORGANIZATIONAL_SYSTEM_H_

class OrganizationalSystem{
public:
    OrganizationalSystem();
    ~OrganizationalSystem();
    void Run();
};
#endif


//organizational_system.cc
#include "organizational_system.h"
#include 
using namespace std;

OrganizationalSystem::OrganizationalSystem(){

}

OrganizationalSystem::~OrganizationalSystem(){

}

void OrganizationalSystem::Run(){
    cout << "organizational system run!" << endl;
}

HumanResourceSystem:

//human_resource_system.h
#ifndef HELENDP_SOURCE_HUMAN_RESOURCE_SYSTEM_H_
#define HELENDP_SOURCE_HUMAN_RESOURCE_SYSTEM_H_

class HumanResourceSystem{
public:
    HumanResourceSystem();
    ~HumanResourceSystem();
    void Run();
};
#endif


//human_resource_system.cc
#include "human_resource_system.h"
#include 
using namespace std;

HumanResourceSystem::HumanResourceSystem(){

}

HumanResourceSystem::~HumanResourceSystem(){

}

void HumanResourceSystem::Run(){
    cout << "human resource system run!" << endl;
}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

结构型模式-外观(facade)_第1张图片

结构

  • Facade(RunnerClub):外观类,作为外部访问的总接口.
  • Subsystem classes(PublicRelationSystem,PropagandaSystem,OrganizationalSystem,HumanResourceSystem):实现具体子系统的功能.

优点

  • 对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易。通过引入外观模式,客户代码将变得很简单,与之关联的对象也很少.
  • 实现了子系统与客户之间的松耦合关系,这使得子系统的组件变化不会影响到调用它的客户类,只需要调整外观类即可.
  • 降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象.
  • 只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类.

缺点

  • 当要为一个复杂子系统提供一个简单接口时可以使用外观模式。该接口可以满足大多数用户的需求,而且用户也可以越过外观类直接访问子系统.
  • 客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性.
  • 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度.

你可能感兴趣的:(设计模式,设计模式,外观模式)