设计模式(4)-外观模式(Facade) (一)

2014-11-24 11:48:39 · 作者: · 浏览: 4

【描述】外观模式通过在对必需的逻辑和方法的集合前创建简单的外观接口,隐藏来自调用的复杂性。

【UML图】

\

图1 外观模式UML图

(1) AirCondition、Fan、Light电器类定义了一个on和off的方法;

(2) Facade类定义了on和off的方法,并调用了AirCondition、Fan、Light类的on和off方法,隐藏调用的复杂性;

(3) 与组合模式的不同在于Facade和AirCondition、Fan、Light电器类是关联的关系,而不是组合的关系。

【示例代码】

aircondition.h

[html]
#ifndef AIRCONDITION_H
#define AIRCONDITION_H

class AirCondition
{
public:
AirCondition();
~AirCondition();

public:
void on();
void off();
};

#endif // AIRCONDITION_H
#ifndef AIRCONDITION_H
#define AIRCONDITION_H

class AirCondition
{
public:
AirCondition();
~AirCondition();

public:
void on();
void off();
};

#endif // AIRCONDITION_H

aircondition.cpp

[html]
#include
#include "aircondition.h"

AirCondition::AirCondition()
{
qDebug()<<"construct AirCondition";
}

AirCondition::~AirCondition()
{
qDebug()<<"destruct AirCondition";
}

void AirCondition::on()
{
qDebug()<<"AirCondition on";
}

void AirCondition::off()
{
qDebug()<<"AirCondition off";
}
#include
#include "aircondition.h"

AirCondition::AirCondition()
{
qDebug()<<"construct AirCondition";
}

AirCondition::~AirCondition()
{
qDebug()<<"destruct AirCondition";
}

void AirCondition::on()
{
qDebug()<<"AirCondition on";
}

void AirCondition::off()
{
qDebug()<<"AirCondition off";
}

fan.h

[html]
#ifndef FAN_H
#define FAN_H

class Fan
{
public:
Fan();
~Fan();

public:
void on();
void off();
};

#endif // FAN_H
#ifndef FAN_H
#define FAN_H

class Fan
{
public:
Fan();
~Fan();

public:
void on();
void off();
};

#endif // FAN_H

fan.cpp

[html]
#include
#include "fan.h"

Fan::Fan()
{
qDebug()<<"construct Fan";
}

Fan::~Fan()
{
qDebug()<<"destruct Fan";
}

void Fan::on()
{
qDebug()<<"Fan on";
}

void Fan::off()
{
qDebug()<<"Fan off";
}
#include
#include "fan.h"

Fan::Fan()
{
qDebug()<<"construct Fan";
}

Fan::~Fan()
{
qDebug()<<"destruct Fan";
}

void Fan::on()
{
qDebug()<<"Fan on";
}

void Fan::off()
{
qDebug()<<"Fan off";
}

light.h

[html]
#ifndef LIGHT_H
#define LIGHT_H

class Light
{
public:
Light();
~Light();

public:
void on();
void off();
};

#endif // LIGHT_H
#ifndef LIGHT_H
#define LIGHT_H

class Light
{
public:
Light();
~Light();

public:
void on();
void off();
};

#endif // LIGHT_H

light.cpp

[html]
#include
#include "light.h"

Light::Light()
{
qDebug()<<"construct Light";
}

Light::~Light()
{
qDebug()<<"destruct Light";
}

void Light::on()
{
qDebug()<<"Light on";
}

void Light::off()
{
qDebug()<<"Light off";
}
#include
#include "light.h"

Light::Light()
{
qDebug()<<"construct Light";
}

Light::~Light()
{
qDebug()<<"destruct Light";
}

void Light::on()
{
qDebug()<<"Light on";
}

void Light::off()
{
qDebug()<<"Light off";
}

facade.h

[html]
#ifndef FACADE_H
#define FACADE_H

#include "aircondition.h"
#include "fan.h"
#include "light.h"

class Facade
{
public:
Facade();
~Facade();

private:
AirCondition *_airCondition;
Fan *_fan;
Light *_light;

public:
void on();
void off();
};

#endif // FACADE_H
#ifndef FACADE_H
#define FACADE_H

#include "aircondition.h"
#include "fan.h"
#include "light.h"

class Facade
{
public:
Facade();
~Facade();

private:
AirCondition *_airCondition;
Fan *_fan;
Light *_light;

public:
void on();
void off();
};

#endif // FACADE_H

facade.cpp

[html]
#include
#include "facade.h"

Facade::Facade()
{
qDebug()<<"construct Facade";

_airCondition = new AirCondition;
_fan = new Fan;
_light = new Light;
}

Facade::~Facade()
{
qDebug()<<"destruct Facade";

delete _airCondition;
delete _fan;
delete _light;
}

void Facade::on()
{
qDebug()<<"Facade::on";

_airCondition->on();
_fan->on();
_l