• 十一、结构型 外观模式(Facade Pattern)


    外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

    在这里插入图片描述

    形状接口

    /**
     * 形状接口
     *
     * @author 吴尚慧
     * @since 2022/6/26 13:11
     */
    public interface Shape {
        /**
         * 画
         */
        void draw();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    形状实现类

    /**
     * 正方形
     *
     * @author 吴尚慧
     * @since 2022/6/13 10:22
     */
    public class Square implements Shape {
    
        @Override
        public void draw() {
            System.out.println("形状:正方形");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    /**
     * 长方形
     *
     * @author 吴尚慧
     * @since 2022/6/13 10:22
     */
    public class Rectangle implements Shape {
    
        @Override
        public void draw() {
            System.out.println("形状:长方形");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    /**
     * 圆
     *
     * @author 吴尚慧
     * @since 2022/6/13 10:22
     */
    public class Circle implements Shape {
    
        @Override
        public void draw() {
            System.out.println("形状:圆");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    外观类

    /**
     * 外观类
     *
     * @author 吴尚慧
     * @since 2022/6/26 17:03
     */
    public class ShapeMaker {
    
        /**
         * 圆
         */
        private Shape circle;
        /**
         * 长方形
         */
        private Shape rectangle;
        /**
         * 正方形
         */
        private Shape square;
    
        public ShapeMaker() {
            circle = new Circle();
            rectangle = new Rectangle();
            square = new Square();
        }
    
        public void drawCircle() {
            circle.draw();
        }
    
        public void drawRectangle() {
            rectangle.draw();
        }
    
        public void drawSquare() {
            square.draw();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    测试类

    /**
     * 测试类
     *
     * @author 吴尚慧
     * @since 2022/6/26 17:05
     */
    public class FacadePatternDemo {
    
        public static void main(String[] args) {
            ShapeMaker shapeMaker = new ShapeMaker();
    
            shapeMaker.drawCircle();
            shapeMaker.drawRectangle();
            shapeMaker.drawSquare();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    结果:

    形状:圆
    形状:长方形
    形状:正方形
    
    • 1
    • 2
    • 3

    参考:
    https://www.runoob.com/design-pattern/design-pattern-intro.html

  • 相关阅读:
    C++ 多线程使用
    2022年Android面试之Jetpack(AAC框架)篇
    gpgcheck介绍
    绁炵粡缃戠粶妯″瀷鍒嗘瀽鎯呮櫙鍒嗘瀽
    2022/09/10、11 day01/02:HTML和CSS
    Flutter 开发环境搭建-VS Code篇
    跨平台安装Flex、Bison(Windows、Linux、MacOS环境下安装)
    高科技案例研究微软设备事业部 | 达索系统百世慧®
    bp神经网络是什么网络,bp神经网络和神经网络
    转载-C#学习笔记-基本概念(CLR、CTS、CLS...)
  • 原文地址:https://blog.csdn.net/weixin_45847167/article/details/125471407