设计模式9-外观模式

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();
    }

}

猜你喜欢

转载自www.cnblogs.com/zhouyu0-0/p/10724398.html