纵有疾风起
人生不言弃

设计模式–外观模式

 设计模式–外观模式插图

设计模式–外观模式插图(1)

设计模式–外观模式插图(2)

 

代码说明:

package com.java.jikexueyuan.facademode.hometheater;//爆米花public class Popcorn {    //单例模式,构造函数私有化    private static Popcorn instance = null;    private Popcorn() {    }    //单例模式创建实例    public static Popcorn getInstance() {        if (instance == null) {            instance = new Popcorn();        }        return instance;    }        public void on() {        System.out.println("Popcorn On");    }    public void off() {        System.out.println("Popcorn Off");    }    public void pop() {        System.out.println("Popcorn is popping");    }            }
package com.java.jikexueyuan.facademode.hometheater;//DVD播放器public class DVDPlayer {    private static DVDPlayer instance = null;    private DVDPlayer() {    }    public static DVDPlayer getInstance() {        if (instance == null) {            instance = new DVDPlayer();        }        return instance;    }        public void on() {        System.out.println("DVDPlayer On");    }    public void off() {        System.out.println("DVDPlayer Off");    }    public void play() {        System.out.println("DVDPlayer is playing");    }        public void pause() {        System.out.println("DVDPlayer  pause");    }        public void setdvd() {        System.out.println("DVDPlayer  is setting dvd");    }}
package com.java.jikexueyuan.facademode.hometheater;//投影仪public class Projector {    private int size=5;    private static Projector instance = null;    private Projector() {    }    public static Projector getInstance() {        if (instance == null) {            instance = new Projector();        }        return instance;    }        public void on() {        System.out.println("Projector On");    }    public void off() {        System.out.println("Projector Off");    }    public void focus() {        System.out.println("Popcorn is focus");    }        public void zoom(int size) {        this.size=size;        System.out.println("Popcorn zoom to"+size);    }    }
package com.java.jikexueyuan.facademode.hometheater;//屏幕public class Screen {    private static Screen instance = null;    private Screen() {    }    public static Screen getInstance() {        if (instance == null) {            instance = new Screen();        }        return instance;    }        public void up() {        System.out.println("Screen  up");    }    public void down() {        System.out.println("Screen  down");    }                }
package com.java.jikexueyuan.facademode.hometheater;//音响public class Stereo {    private static Stereo instance = null;    private int volume = 5;    private Stereo() {    }    public static Stereo getInstance() {        if (instance == null) {            instance = new Stereo();        }        return instance;    }    public void on() {        System.out.println("Stereo On");    }    public void off() {        System.out.println("Stereo Off");    }    public void setVolume(int vol) {        volume = vol;        System.out.println("the volume of Stereo is set to  " + volume);    }    public void addVolume() {        if (volume < 11) {            volume++;            setVolume(volume);        }    }    public void subVolume() {        if (volume > 0) {            volume--;            setVolume(volume);        }    }}
package com.java.jikexueyuan.facademode.hometheater;//灯光public class TheaterLights {    private static TheaterLights instance = null;    private TheaterLights() {    }    public static TheaterLights getInstance() {        if (instance == null) {            instance = new TheaterLights();        }        return instance;    }    public void on() {        System.out.println("TheaterLights On");    }    public void off() {        System.out.println("TheaterLights Off");    }    public void dim(int d) {        System.out.println("TheaterLights dim to " + d + "%");    }    public void bright() {        dim(100);        System.out.println("TheaterLights bright");    }}
package com.java.jikexueyuan.facademode.hometheater;//外观模式--遥控器public class HomeTheaterFacade {    private TheaterLights mTheaterLights;    private Popcorn mPopcorn;    private Stereo mStereo;    private Projector mProjector;    private Screen mScreen;    private DVDPlayer mDVDPlayer;    //组合对象功能    public HomeTheaterFacade() {        mTheaterLights = TheaterLights.getInstance();        mPopcorn = Popcorn.getInstance();        mStereo = Stereo.getInstance();        mProjector = Projector.getInstance();        mScreen = Screen.getInstance();        mDVDPlayer = DVDPlayer.getInstance();    }    public void ready() {        mPopcorn.on();        mPopcorn.pop();        mScreen.down();        mProjector.on();        mStereo.on();        mDVDPlayer.on();        mDVDPlayer.setdvd();        mTheaterLights.dim(10);    }    public void end() {        mPopcorn.off();        mTheaterLights.bright();        mScreen.up();        mProjector.off();        mStereo.off();                mDVDPlayer.setdvd();        mDVDPlayer.off();            }    //单个对象功能    public void play() {        mDVDPlayer.play();    }    public void pause() {        mDVDPlayer.pause();    }}
package com.java.jikexueyuan.facademode;import com.java.jikexueyuan.facademode.hometheater.HomeTheaterFacade;//代码测试public class MainTest {    public static void main(String[] args) {        HomeTheaterFacade mHomeTheaterFacade=new HomeTheaterFacade();                mHomeTheaterFacade.ready();        mHomeTheaterFacade.play();    }}

 最少知识原则

最少知识原则:尽量减少对象之间的交互,只留几个“密友” 项目设计中就是不要让太多的类耦合在一起

文章转载于:https://www.cnblogs.com/lm970585581/p/9703929.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » 设计模式–外观模式

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活