码迷,mamicode.com
首页 > 其他好文 > 详细

外观模式

时间:2015-04-29 01:58:40      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

   外观模式:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。——《HEAD FIRST 设计模式》

   我的c++代码:

#ifndef DESIGN_FACADE_H_
#define DESIGN_FACADE_H_

namespace facade{

class PopcornPopper
{
public:
    void On();
    void Off();
    void Pop();
};

class TheaterLights
{
public:
    void Dim();
    void On();
};

class Screen
{
public:
    void Down();
    void Up();
};

class Projector
{
public:
    void On();
    void Off();
    void WideScreenMode();
};

class Amplifier
{
public:
    void On();
    void Off();
    void SetDvd();
    void SetSurroundSound();
    void SetVolume();
};

class DvdPlayer
{
public:
    void On();
    void Play();
    void Stop();
    void Eject();
    void Off();
};

class HomeTheaterFacade
{
public:
    HomeTheaterFacade(Amplifier* a, DvdPlayer* d,
                      Projector* p, TheaterLights* l,
                      Screen* s, PopcornPopper* po);
public:
    void WatchMovie();
    void EndMovie();
private:
    Amplifier* m_amp;
    DvdPlayer* m_dvd;
    Projector* m_projector;
    TheaterLights* m_light;
    Screen* m_screen;
    PopcornPopper* m_popper;
};
}
#endif // DESIGN_FACADE_H_
#include "facade.h"
#include <iostream>
using namespace facade;

void PopcornPopper::On()
{
   std::cout << "popper on!" << std::endl;
}

void PopcornPopper::Off()
{
    std::cout << "popper off!" << std::endl;
}

void PopcornPopper::Pop()
{
    std::cout << "popper pop!" << std::endl;
}

void TheaterLights::Dim()
{
    std::cout << "theaterlights dim!" << std::endl;
}

void TheaterLights::On()
{
    std::cout << "theaterlights on!" << std::endl;
}

void Screen::Down()
{
    std::cout << "screen down!" << std::endl;
}

void Screen::Up()
{
    std::cout << "screen up!" << std::endl;
}

void Projector::On()
{
    std::cout << "projector on!" << std::endl;
}

void Projector::Off()
{
    std::cout << "projector off!"  << std::endl;
}

void Projector::WideScreenMode()
{
    std::cout << "projector widescreenmode!" << std::endl;
}

void Amplifier::On()
{
    std::cout << "amplifier on!" << std::endl;
}

void Amplifier::Off()
{
    std::cout << "amplifier off!" << std::endl;
}

void Amplifier::SetDvd()
{
    std::cout << "amplifier setdvd!" << std::endl;
}

void Amplifier::SetSurroundSound()
{
    std::cout << "amplifier setsurroundsound!" << std::endl;
}

void Amplifier::SetVolume()
{
    std::cout << "amplifier setvolume!" << std::endl;
}

void DvdPlayer::On()
{
    std::cout << "dvdplayer on!" << std::endl;
}

void DvdPlayer::Off()
{
    std::cout << "dvdplayer off!" << std::endl;
}

void DvdPlayer::Play()
{
    std::cout << "dvdplayer play!" << std::endl;
}

void DvdPlayer::Stop()
{
    std::cout << "dvdplayer stop!" << std::endl;
}

void DvdPlayer::Eject()
{
    std::cout << "dvdplayer eject!" << std::endl;
}

HomeTheaterFacade::HomeTheaterFacade(Amplifier* a, DvdPlayer* d,
                      Projector* p, TheaterLights* l,
                      Screen* s, PopcornPopper* po)
{
    this->m_amp = a;
    this->m_dvd = d;
    this->m_projector = p;
    this->m_light = l;
    this->m_screen = s;
    this->m_popper = po;
}

void HomeTheaterFacade::WatchMovie()
{
    this->m_popper->On();
    this->m_popper->Pop();
    this->m_light->Dim();
    this->m_screen->Down();
    this->m_projector->On();
    this->m_projector->WideScreenMode();
    this->m_amp->On();
    this->m_amp->SetDvd();
    this->m_amp->SetSurroundSound();
    this->m_amp->SetVolume();
    this->m_dvd->On();
    this->m_dvd->Play();
}

void HomeTheaterFacade::EndMovie()
{
    this->m_popper->Off();
    this->m_light->On();
    this->m_screen->Up();
    this->m_projector->Off();
    this->m_amp->Off();
    this->m_dvd->Stop();
    this->m_dvd->Eject();
    this->m_dvd->Off();
}

   个人感悟:待留。

外观模式

标签:

原文地址:http://www.cnblogs.com/foolbread/p/4464480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!