Java大数据平台开发 学习笔记(40)—— java设计模式(外观模式)知识汇总

一、前言:

外观模式的注意事项与细节:

  1. 外观模式对外屏蔽了子系统的细节,从而降低了客户端与子系统的复杂性。
  2. 外观模式降低了客户端与子系统的耦合性,让子系统内部易于维护与扩展。
  3. 通过合理的使用外观模式,可以帮助我们更好的划分访问的层次。

二、UML图:

在这里插入图片描述


三、组合模式:

3.1、代码实例:

Step 1) 创建 DVDPlayer 类:

public class DVDPlayer {
    
    
    private static DVDPlayer instance = new DVDPlayer();

    public static DVDPlayer getInstanc(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" DVD Open !");
    }

    public void off(){
    
    
        System.out.println(" DVD Off !");
    }

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

    public void pause(){
    
    
        System.out.println(" DVD is pause !");
    }
}

Step 2) 创建 Popcorn 实现类:

public class Popcorn {
    
    
    private static Popcorn instance = new Popcorn();

    public static Popcorn getInstanc(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" Popcorn Open !");
    }

    public void off(){
    
    
        System.out.println(" Popcorn Off !");
    }

    public void pop(){
    
    
        System.out.println(" Popcorn is pop !");
    }

    public void pause(){
    
    
        System.out.println(" Popcorn is pause !");
    }
}

Step 3) 创建 Projector 实现类:

public class Projector {
    
    
    private static Projector instance = new Projector();

    public static Projector getInstanc(){
    
    
        return instance;
    }

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

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

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

Step 4) 创建 Screen 实现类:

public class Screen {
    
    
    private static Screen instance = new Screen();

    public static Screen getInstanc(){
    
    
        return instance;
    }

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

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

Step 5) 创建 Stereo 实现类:

public class Stereo {
    
    
    private static Stereo instance = new Stereo();

    public static Stereo getInstanc(){
    
    
        return instance;
    }

    public void on(){
    
    
        System.out.println(" Stereo Open !");
    }

    public void off(){
    
    
        System.out.println(" Stereo Off !");
    }

    public void up(){
    
    
        System.out.println(" Stereo is up !");
    }
}

Step 6) 创建 TheaterLight 实现类:

public class TheaterLight {
    
    
    private static TheaterLight instance = new TheaterLight();

    public static TheaterLight getInstanc(){
    
    
        return instance;
    }

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

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

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

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

Step 7) 创建 HomeTheaterFacade 实现类:

public class HomeTheaterFacade {
    
    

    private TheaterLight theaterLight;
    private Popcorn popcorn;
    private Stereo stereo;
    private Projector projector;
    private Screen screen;
    private DVDPlayer dvdPlayer;

    public HomeTheaterFacade() {
    
    
        this.theaterLight = TheaterLight.getInstanc();
        this.popcorn = Popcorn.getInstanc();
        this.stereo = Stereo.getInstanc();
        this.projector = Projector.getInstanc();
        this.screen = Screen.getInstanc();
        this.dvdPlayer = DVDPlayer.getInstanc();
    }

    public void  ready(){
    
    
        popcorn.on();
        popcorn.pop();
        screen.down();
        projector.on();
        stereo.on();
        dvdPlayer.on();
        theaterLight.dim();
    }

    public void play(){
    
    
        dvdPlayer.play();
    }

    public void pause(){
    
    
        dvdPlayer.pause();
    }

    public void end(){
    
    
        popcorn.off();
        theaterLight.bright();
        screen.up();
        projector.off();
        stereo.off();
        dvdPlayer.off();
    }
}

Step 8) 创建 main 方法:

public class Client {
    
    

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

        homeTheaterFacade.ready();
        System.out.println("------------------");
        homeTheaterFacade.play();
        System.out.println("------------------");
        homeTheaterFacade.end();
        System.out.println("------------------");
    }
}


• 由 ChiKong_Tam 写于 2020 年 10 月 18 日

猜你喜欢

转载自blog.csdn.net/qq_42209354/article/details/109148540