外观模式.cpp
# include <iostream>
# include <memory>
using namespace std;
namespace ns1
{ class graphic { graphic ( ) { } ; graphic ( const graphic & tmpobj) ; graphic & operator = ( const graphic & tmpobj) ; ~ graphic ( ) { } ; public : static graphic & getInstance ( ) { static graphic instance; return instance; } public : void display ( bool enable) { cout << "full screen->" << enable << endl; } void effect ( bool enable) { cout << "special effects->" << enable << endl; } void resolution ( int index) { cout << "resolution ratio->" << index << endl; } void antialiasing ( bool enable) { cout << "anti-aliasing->" << enable << endl; } } ; class sound { sound ( ) { } ; sound ( const sound & tmpobj) ; sound & operator = ( const sound & tmpobj) ; ~ sound ( ) { } ; public : static sound & getInstance ( ) { static sound instance; return instance; } public : void bgsound ( bool enable) { cout << "background sound->" << enable << endl; } void envirsound ( bool enable) { cout << "environmental sound effect->" << enable << endl; } void expsound ( bool enable) { cout << "expressional voice->" << enable << endl; } void setvolume ( int level) { cout << "volume->" << level << endl; } } ; class chatvoice { chatvoice ( ) { } ; chatvoice ( const chatvoice & tmpobj) ; chatvoice & operator = ( const chatvoice & tmpobj) ; ~ chatvoice ( ) { } ; public : static chatvoice & getInstance ( ) { static chatvoice instance; return instance; } public : void micvolume ( int level) { cout << "microphone volume->" << level << endl; } void micsens ( int level) { cout << "microphone sensitivity->" << level << endl; } void chatvolume ( int level) { cout << "chat volume->" << level << endl; } } ; class conffacade { conffacade ( ) { } ; conffacade ( const conffacade & tmpobj) ; conffacade & operator = ( const conffacade & tmpobj) ; ~ conffacade ( ) { } ; public : static conffacade & getInstance ( ) { static conffacade instance; return instance; } public : void LowConfComputer ( ) { graphic & g_gp = graphic:: getInstance ( ) ; g_gp. display ( true ) ; g_gp. effect ( false ) ; g_gp. resolution ( 2 ) ; g_gp. antialiasing ( false ) ; sound & g_snd = sound:: getInstance ( ) ; g_snd. bgsound ( false ) ; g_snd. envirsound ( false ) ; g_snd. expsound ( false ) ; g_snd. setvolume ( 15 ) ; chatvoice & g_cv = chatvoice:: getInstance ( ) ; g_cv. micvolume ( 20 ) ; g_cv. micsens ( 50 ) ; g_cv. chatvolume ( 60 ) ; } void HighConfComputer ( ) { graphic & g_gp = graphic:: getInstance ( ) ; g_gp. display ( false ) ; g_gp. effect ( true ) ; g_gp. resolution ( 0 ) ; g_gp. antialiasing ( true ) ; sound & g_snd = sound:: getInstance ( ) ; g_snd. bgsound ( true ) ; g_snd. envirsound ( true ) ; g_snd. expsound ( true ) ; g_snd. setvolume ( 50 ) ; chatvoice & g_cv = chatvoice:: getInstance ( ) ; g_cv. micvolume ( 100 ) ; g_cv. micsens ( 100 ) ; g_cv. chatvolume ( 100 ) ; } } ;
} namespace ns2
{ class Screen { public : void On ( ) { cout << "screen on!" << endl; } void Off ( ) { cout << "screen off!" << endl; } } ; class Light { public : void On ( ) { cout << "light open!" << endl; } void Off ( ) { cout << "light close!" << endl; } } ; class Speaker { public : void On ( ) { cout << "speaker on!" << endl; } void Off ( ) { cout << "speaker off!" << endl; } } ; class DvdPlayer { public : void On ( ) { cout << "dvd open!" << endl; } void Off ( ) { cout << "dvd close!" << endl; } } ; class PlayerStation { public : void On ( ) { cout << "playerstation on!" << endl; } void Off ( ) { cout << "playerstation off!" << endl; } } ; class HomeTheaterFacade { Screen scnobj; Light lgobj; Speaker spkobj; DvdPlayer dpobj; PlayerStation psobj; public : void WatchMovie ( ) { scnobj. On ( ) ; lgobj. Off ( ) ; spkobj. On ( ) ; dpobj. On ( ) ; psobj. Off ( ) ; } void PlayGame ( ) { scnobj. On ( ) ; lgobj. On ( ) ; spkobj. On ( ) ; dpobj. Off ( ) ; psobj. On ( ) ; } } ;
} namespace ns3
{ class Shape { public : virtual ~ Shape ( ) = default ; virtual void draw ( ) const = 0 ; } ; class Rectangle : public Shape { public : void draw ( ) const override { cout << "Rectangle::draw()" << endl; } } ; class Square : public Shape { public : void draw ( ) const override { cout << "Square::draw()" << endl; } } ; class Circle : public Shape { public : void draw ( ) const override { cout << "Circle::draw()" << endl; } } ; class ShapeMaker { shared_ptr< Shape> circle; shared_ptr< Shape> rectangle; shared_ptr< Shape> square; public : ShapeMaker ( ) { circle = make_shared < Circle> ( ) ; rectangle = make_shared < Rectangle> ( ) ; square = make_shared < Square> ( ) ; } void drawCircle ( ) const { circle-> draw ( ) ; } void drawRectangle ( ) const { rectangle-> draw ( ) ; } void drawSquare ( ) const { square-> draw ( ) ; } } ;
} int main ( )
{
# if 0 using namespace ns1; graphic & g_gp = graphic:: getInstance ( ) ; g_gp. display ( false ) ; g_gp. effect ( true ) ; g_gp. resolution ( 2 ) ; g_gp. antialiasing ( false ) ; cout << "---------------" << endl; sound & g_snd = sound:: getInstance ( ) ; g_snd. setvolume ( 80 ) ; g_snd. envirsound ( true ) ; g_snd. bgsound ( false ) ; cout << "---------------" << endl; chatvoice & g_cv = chatvoice:: getInstance ( ) ; g_cv. chatvolume ( 70 ) ; g_cv. micsens ( 65 ) ;
# endif # if 0 using namespace ns1; conffacade & g_cffde = conffacade:: getInstance ( ) ; cout << "Low-configuration computer" << endl; g_cffde. LowConfComputer ( ) ; cout << "------------------" << endl; cout << "high-configuration computer" << endl; g_cffde. HighConfComputer ( ) ;
# endif # if 0 ns2:: HomeTheaterFacade htfacobj; cout << "movie---------------" << endl; htfacobj. WatchMovie ( ) ; cout << "game---------------" << endl; htfacobj. PlayGame ( ) ;
# endif # if 1 using namespace ns3; shared_ptr< ShapeMaker> shapeMaker ( new ShapeMaker ( ) ) ; shapeMaker-> drawCircle ( ) ; shapeMaker-> drawRectangle ( ) ; shapeMaker-> drawSquare ( ) ;
# endif cout << "Over!\n" ; return 0 ;
}