Facade—Design Pattern

Facade—Design Pattern

What is Appearance Mode?

Taking the common cases among us, when we need to install the win10 computer system, we don’t need to understand every part of the installation system, and just hand it over to the fixed program for installation; when we go to the hospital to see a doctor, we may go to registration, outpatient clinic, It is very complicated for patients or their family members to price and get medicines. If there is a receptionist, it is very convenient to only let the receptionist handle it; and so on...

The appearance mode is to encapsulate the specific details of the event, so that the user can complete the requirements more conveniently.

That is, by defining a consistent interface (interface class), the details of the internal subsystem are shielded, so that the calling end only needs to make calls with this interface, and does not need to care about the internal details of the subsystem.

Basic introduction to appearance mode

insert image description here

Making a theater management?

insert image description here

If we want to make a theater management project now, if we need to complete the function of using home theater, it is too cumbersome to call and use these methods one by one, and the structure is chaotic.

Under normal circumstances, the cinema only provides preparations for movie playback, start playback, pause playback, and end playback work. At this time, we can use the appearance mode to achieve this effect.

implementation code

popcorn

package com.design_patterns.facade;

/**
 * 定义爆米花机器类
 */
public class PopCorn {
    
    

    private static PopCorn instance = new PopCorn();

    private PopCorn(){
    
    };

    public static PopCorn getInstance(){
    
    
        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 pop ");
    }
}

DVD player class

package com.design_patterns.facade;

/**
 * 定义DVDPlayer播放类
 */
public class DVDPlayer {
    
    

    //使用单例模式(饿汉式)
    private static DVDPlayer instance = new DVDPlayer();

    //构造方法私有化
    private DVDPlayer(){
    
    };

    //获取单例模式对象
    public static DVDPlayer getInstance(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" dvd on ");
    }

    public void off(){
    
    
        System.out.println(" dvd off ");
    }

    public void play(){
    
    
        System.out.println(" dvd is playing ");
    }

    public void pause(){
    
    
        System.out.println(" dvd pause ");
    }
}

Projector class

package com.design_patterns.facade;

/**
 * 定义放映机类
 */
public class Projector {
    
    

    private static Projector instance = new Projector();

    private Projector(){
    
    };

    public static Projector getInstance(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" Projector on ");
    }

    public void off(){
    
    
        System.out.println(" Projector off ");
    }

    public void focus(){
    
    
        System.out.println(" Projector focus ");
    }


}

screen (screen)

package com.design_patterns.facade;

/**
 * 定义屏幕
 */
public class Screen {
    
    
    //使用单例模式(饿汉式)
    private static Screen instance = new Screen();

    //构造方法私有化
    private Screen(){
    
    };

    //获取单例模式对象
    public static Screen getInstance(){
    
    
        return instance;
    }

    public void up(){
    
    
        System.out.println(" screen up ");
    }

    public void down(){
    
    
        System.out.println(" screen down ");
    }

}

Audio class

package com.design_patterns.facade;

/**
 * 定义音箱,输出影片声音
 */
public class Stereo {
    
    
    //使用单例模式(饿汉式)
    private static Stereo instance = new Stereo();

    //构造方法私有化
    private Stereo(){
    
    };

    //获取单例模式对象
    public static Stereo getInstance(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" stereo on ");
    }

    public void off(){
    
    
        System.out.println(" stereo off ");
    }

    public void up(){
    
    
        System.out.println(" stereo up ");
    }

    public void down(){
    
    
        System.out.println(" stereo down ");
    }
}

Cinema lighting

package com.design_patterns.facade;

/**
 * 定义电影院内的灯光类
 */
public class TheaterLight {
    
    
    //使用单例模式(饿汉式)
    private static TheaterLight instance = new TheaterLight();

    //构造方法私有化
    private TheaterLight(){
    
    };

    //获取单例模式对象
    public static TheaterLight getInstance(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" TheaterLight on ");
    }

    public void off(){
    
    
        System.out.println(" TheaterLight off ");
    }

    public void bright(){
    
    
        System.out.println(" TheaterLight bright ");
    }

    public void dim() {
    
    
        System.out.println(" TheaterLight dim ");
    }
}

Cinema class, which is the implementation of the Appearance pattern

package com.design_patterns.facade;

/**
 * 定义电影院类,通过方法封装各个子部分的实现细节
 */
public class HomeTheaterFacade {
    
    

    //定义各个子系统的对象
    private DVDPlayer dvdPlayer;
    private PopCorn popCorn;
    private Projector projector;
    private Screen screen;
    private Stereo stereo;
    private TheaterLight theaterLight;

    //通过构造方法为电影院各个子系统部分获取实例化对象
    public HomeTheaterFacade(){
    
    
        this.dvdPlayer = DVDPlayer.getInstance();
        this.popCorn = PopCorn.getInstance();
        this.projector = Projector.getInstance();
        this.screen = Screen.getInstance();
        this.stereo = Stereo.getInstance();
        this.theaterLight = TheaterLight.getInstance();
    }


    //操作分为 4 步

    /**
     * 进行准备工作
     */
    public void ready(){
    
    
        System.out.println("----- 电影院准备放映影片 -----");
        popCorn.on();
        popCorn.pop();
        screen.down();
        projector.on();
        stereo.on();
        dvdPlayer.on();
        theaterLight.dim();

    }

    /**
     * 电影院开始放电影工作
     */
    public void play(){
    
    
        System.out.println("----- 电影院开始放映影片 -----");
        dvdPlayer.play();
    }

    /**
     * 电影院进行暂停工作
     */
    public void pause(){
    
    
        System.out.println("----- 电影院暂停放映影片 -----");
        dvdPlayer.pause();
    }

    /**
     * 电影院电影放映结束动作
     */
    public void end(){
    
    
        System.out.println("----- 电影院结束放映影片 -----");
        popCorn.off();
        theaterLight.bright();
        screen.up();
        projector.off();
        stereo.off();
        dvdPlayer.off();
    }
}

Client (test) class

package com.design_patterns.facade;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();

        homeTheaterFacade.ready();      //电影院准备工作
        homeTheaterFacade.play();       //电影院开始放电影
        homeTheaterFacade.pause();      //电影院暂停放电影
        homeTheaterFacade.end();        //电影院结束影片放映
    }
}

operation result

----- 电影院准备放映影片 -----
 popcorn on 
 popcorn pop 
 screen down 
 Projector on 
 stereo on 
 dvd on 
 TheaterLight dim 
----- 电影院开始放映影片 -----
 dvd is playing 
----- 电影院暂停放映影片 -----
 dvd pause 
----- 电影院结束放映影片 -----
 popcorn off 
 TheaterLight bright 
 screen up 
 Projector off 
 stereo off 
 dvd off 

Summarize

Hee hee, I feel that the appearance mode is very easy to understand. The essence is to encapsulate the implementation details of the event through a consistent interface, and finally call the encapsulation implementation function . Okay, here we go, come on~~~

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/107614468