外观模式
类图
定义
-
提供一个统一的接口去访问多个子系统的多个不同的接口。
-
定义了一个高层次的接口,使得子系统更容易被使用。
优缺点
优点:
-
提供了一个简单且公用的接口去处理复杂的子系统,并且没有子系统的功能。
-
遮蔽了子系统的复杂性,避免了客户与子系统直接连接,也减少了子系统与子系统间的连接,每个子系统都有它的Facade模式,子系统Facade模式去访问其他子系统。
缺点
- 限制了客户的自由,减少了可变性。
适用环境:
- 需要复杂的子系统提供一个简单的接口。
- 客户与抽象的实现类中存在若干依赖。
- 子系统分层是必要的或架构要求的情况下
设计原则
- 最少知识原则:只和你的密友谈话
代码
HomeTheaterFacade类:
#ifndef HOMETHEATERFACADE_H
#define HOMETHEATERFACADE_H
#include "amplifier.h"
#include "tuner.h"
#include "dvdplayer.h"
#include "cdplayer.h"
#include "projector.h"
#include "theaterlights.h"
#include "screen.h"
#include "popcornpopper.h"
#include <QString>
class HomeTheaterFacade
{
public:
HomeTheaterFacade(Amplifier *amp,
Tuner *tuner,
DvdPlayer *dvd,
CdPlayer *cd,
Projector *projector,
TheaterLights *lights,
Screen *screen,
PopcornPopper *popper);
void watchMovie(QString movie);
void endMovie();
private:
Amplifier *amp;
Tuner *tuner;
DvdPlayer *dvd;
CdPlayer *cd;
Projector *projector;
TheaterLights *lights;
Screen *screen;
PopcornPopper *popper;
};
#endif // HOMETHEATERFACADE_H
#include "hometheaterfacade.h"
HomeTheaterFacade::HomeTheaterFacade(Amplifier *amp,
Tuner *tuner,
DvdPlayer *dvd,
CdPlayer *cd,
Projector *projector,
TheaterLights *lights,
Screen *screen,
PopcornPopper *popper)
{
this->amp = amp;
this->tuner = tuner;
this->dvd = dvd;
this->cd = cd;
this->projector = projector;
this->lights = lights;
this->screen =screen;
this->popper = popper;
}
void HomeTheaterFacade::watchMovie(QString movie)
{
qDebug() << "Get ready to watch a movie....";
popper->on();
popper->pop();
lights->dim(10);
screen->down();
projector->on();
projector->wideScreenMode();
amp->on();
amp->setDvd(dvd);
amp->setSurroundSound();
amp->setVolume(5);
dvd->on();
dvd->play(movie);
}
void HomeTheaterFacade::endMovie()
{
qDebug() << "Shutting movie theater down...";
popper->off();
lights->on();
screen->up();
projector->off();
amp->off();
dvd->stop();
dvd->eject();
dvd->off();
}
Amplifier类:
#ifndef AMPLIFIER_H
#define AMPLIFIER_H
#include "dvdplayer.h"
class Amplifier
{
public:
Amplifier();
void on();
void off();
void setDvd(DvdPlayer *dvd);
void setSurroundSound();
void setVolume(int volume);
};
#endif // AMPLIFIER_H
#include "amplifier.h"
Amplifier::Amplifier()
{
}
void Amplifier::on()
{
qDebug() << "Top-O-Line Amplifier on";
}
void Amplifier::off()
{
qDebug() << "Top-O-Line Amplifier off";
}
void Amplifier::setDvd(DvdPlayer *dvd)
{
qDebug() << "Top-O-Line Amplifier setting "
<< "DVD player to Top-O-Line DVD player";
}
void Amplifier::setSurroundSound()
{
}
void Amplifier::setVolume(int volume)
{
qDebug() << "Top-O-Line Amplifier setting volume to "
<< QString::number(volume);
}
CdPlayer类:
#ifndef CDPLAYER_H
#define CDPLAYER_H
class CdPlayer
{
public:
CdPlayer();
};
#endif // CDPLAYER_H
#include "cdplayer.h"
CdPlayer::CdPlayer()
{
}
DvdPlayer类:
#ifndef DVDPLAYER_H
#define DVDPLAYER_H
#include <QDebug>
#include <QString>
class DvdPlayer
{
public:
DvdPlayer();
void on();
void play(QString movie);
void stop();
void eject();
void off();
private:
QString movie;
};
#endif // DVDPLAYER_H
#include "dvdplayer.h"
DvdPlayer::DvdPlayer()
{
}
void DvdPlayer::on()
{
qDebug() << "Top-O-Line DVD Player on";
}
void DvdPlayer::play(QString movie)
{
qDebug() << "Top-O-Line DVD Player playing "
<< movie;
this->movie = movie;
}
void DvdPlayer::stop()
{
qDebug() << "Top-O-Line DVD Player stopped "
<< movie;
}
void DvdPlayer::eject()
{
qDebug() << "Top-O-Line DVD Player eject";
}
void DvdPlayer::off()
{
qDebug() << "Top-O-Line DVD Player off";
}
PopcornPopper类:
#ifndef POPCORNPOPPER_H
#define POPCORNPOPPER_H
#include <QDebug>
class PopcornPopper
{
public:
PopcornPopper();
void on();
void off();
void pop();
};
#endif // POPCORNPOPPER_H
#include "popcornpopper.h"
PopcornPopper::PopcornPopper()
{
}
void PopcornPopper::on()
{
qDebug() << "Popcorn Popper on";
}
void PopcornPopper::off()
{
qDebug() << "Popcorn Popper off";
}
void PopcornPopper::pop()
{
qDebug() << "Popcorn Popper popping popcorn!";
}
Projector类:
#ifndef PROJECTOR_H
#define PROJECTOR_H
class Projector
{
public:
Projector();
void on();
void off();
void wideScreenMode();
};
#endif // PROJECTOR_H
#include "projector.h"
#include <QDebug>
Projector::Projector()
{
}
void Projector::on()
{
qDebug() << "Top-O-Line Projector on";
}
void Projector::off()
{
qDebug() << "Top-O-Line Projector off";
}
void Projector::wideScreenMode()
{
qDebug() << "Top-O-Line Projector in wideScreenMode (16*9 aspect ratio)";
}
Screen类:
#ifndef SCREEN_H
#define SCREEN_H
#include <QDebug>
class Screen
{
public:
Screen();
void up();
void down();
};
#endif // SCREEN_H
#include "screen.h"
Screen::Screen()
{
}
void Screen::up()
{
qDebug() << "Theater Screen going up";
}
void Screen::down()
{
qDebug() << "Theater Screen going down";
}
TheaterLights类:
#ifndef THEATERLIGHTS_H
#define THEATERLIGHTS_H
class TheaterLights
{
public:
TheaterLights();
void on();
void dim(int value);
};
#endif // THEATERLIGHTS_H
#include "theaterlights.h"
#include <QDebug>
TheaterLights::TheaterLights()
{
}
void TheaterLights::on()
{
qDebug() << "Theater Ceiling Lights on";
}
void TheaterLights::dim(int value)
{
qDebug() << "Theater Ceiling Lights dimming to" << QString::number(value) << "%";
}
Tuner类:
#ifndef TUNER_H
#define TUNER_H
class Tuner
{
public:
Tuner();
};
#endif // TUNER_H
#include "tuner.h"
Tuner::Tuner()
{
}
测试:
#include "mainwindow.h"
#include "hometheaterfacade.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
Amplifier *amp = new Amplifier();
Tuner *tuner = new Tuner();
DvdPlayer *dvd = new DvdPlayer();
CdPlayer *cd = new CdPlayer();
Projector *projector = new Projector();
Screen *screen = new Screen();
TheaterLights *lights = new TheaterLights();
PopcornPopper *popper = new PopcornPopper();
HomeTheaterFacade *homeTheater
= new HomeTheaterFacade(amp, tuner, dvd, cd,
projector, lights, screen,popper);
homeTheater->watchMovie("Raiders of the Lost Ark");
homeTheater->endMovie();
}
MainWindow::~MainWindow()
{
}