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 日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值