设计模式(4)-外观模式(Facade)
【描述】外观模式通过在对必需的逻辑和方法的集合前创建简单的外观接口,隐藏来自调用的复杂性。
【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 <QDebug>
#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 <QDebug>
#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 <QDebug>
#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 <QDebug>
#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 <QDebug>
#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 <QDebug>
#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 <QDebug>
#include "facade.h"
Facade::Facade()
{
qDebug()<<"construct Facade";
_airCondition = new AirConditio
补充:软件开发 , C++ ,