美文网首页
Java 外观模式

Java 外观模式

作者: 索性流年 | 来源:发表于2021-02-20 09:23 被阅读0次

    文集地址

    一句话总结外观模式

    • 封装子类的方法,方便调用

    概述

    • 外观模式又叫作门面模式,主要作用于封装子系统,为其提供一个统一调用接口,使其更方便使用,但却不关心子系统实现方式。

    应用案例

    • 吃糖,需要先把糖拿过来,让后撕开糖纸,在把糖放嘴里,这些个步骤缺一不可,其实可以将这些步骤提前做好,这样在外人看来,你的糖就在嘴里

    实现案例

    • 吃糖的所有行为
    /**
     * 具体行为
     *
     * @author ext.liuyan10
     * @date 2021/2/7 17:11
     */
    public class HaveSweets {
        public void look() {
            System.out.println("将糖果拿过来");
        }
    
        public void tearOpen() {
            System.out.println("撕开糖纸");
        }
    
        public void eat() {
            System.out.println("把糖吃进嘴里");
        }
    }
    
    
    • 外观角色统一行为
    /**
     * 外观角色
     *
     * @author ext.liuyan10
     * @date 2021/2/7 17:13
     */
    public class Appearance {
        public void haveSweets() {
            HaveSweets haveSweets = new HaveSweets();
            haveSweets.look();
            haveSweets.tearOpen();
            haveSweets.eat();
        }
    }
    
    
    • 调用案例
    /**
     * @author ext.liuyan10
     * @date 2021/2/7 17:13
     */
    public class AppearanceApp {
        public static void main(String[] args) {
            Appearance appearance = new Appearance();
            appearance.haveSweets();
        }
    }
    
    
    • 执行结果
    Connected to the target VM, address: '127.0.0.1:64221', transport: 'socket'
    将糖果拿过来
    撕开糖纸
    把糖吃进嘴里
    Disconnected from the target VM, address: '127.0.0.1:64221', transport: 'socket'
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:Java 外观模式

          本文链接:https://www.haomeiwen.com/subject/tnuztltx.html