首页 > 其他 > 详细

设计模式9-外观模式

时间:2019-04-17 16:42:15      阅读:104      评论:0      收藏:0      [点我收藏+]
package DesignPattern;

public class FacadePattern {
    //外观模式:向外展示简化而同意的接口
    //符合最少知识原则,向客户提供简单接口,内部复杂的组件可以在不受影响的情况下进行替换
    public static class Amplifier{
        int volume;
        void on(){
            System.out.println("Amp is on");
        }
        void setSurroundSound(){
            System.out.println("surroundSound is set");
        }
        void setVolume(int volume){
            this.volume=volume;
            System.out.println("amp volume is set "+volume);
        }
        void off(){
            System.out.println("Amp is off");
        }
    }
    public static class Dvdplyer{
        void on(){
            System.out.println("dvd is on");
        }
        void play(String move){
            System.out.println(move+" is playing");
        }
        void off(){
            System.out.println("dvd is off");
        }
    }

    public static class HomeTheaterFacade{
        Amplifier amp;
        Dvdplyer dvd;
        public HomeTheaterFacade(Amplifier amp,Dvdplyer dvd){
            this.amp=amp;
            this.dvd=dvd;
        }
        public void watchMovie(String movie){
            amp.on();
            amp.setSurroundSound();
            amp.setVolume(5);
            dvd.on();
            dvd.play(movie);
        }
        public void endMovie(){
            amp.off();
            dvd.off();
        }
    }

    public static void main(String[] args) {

        HomeTheaterFacade homeTheater = new HomeTheaterFacade(new Amplifier(),new Dvdplyer());
        homeTheater.watchMovie("a dog");
        System.out.println();
        homeTheater.endMovie();
    }

}

设计模式9-外观模式

原文:https://www.cnblogs.com/zhouyu0-0/p/10724398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!