• [Design Pattern] Facde Pattern 简单案例


    Facade Pattern, 即外观模式,用于隐藏复杂的系统内部逻辑,提供简洁的接口给客户端调用,属于结构类的设计模式。我会将其名字理解为,门户模式。

    下面是 Facade Pattern 的一个简单案例。

    Shape 定义一个接口,Circle, rectangle, Square 分别实现 Shape 接口,代表系统内部的一个功能。ShapeMaker 作为一个对外类,提供简洁的接口给外部调用。

    代码实现:

    Shape 接口

    public interface Shape {
        public void draw();
    }

    Circle, Rectangle, Square 具体类实现 Shape 接口

    public class Circle implements Shape {
    
        @Override
        public void draw() {
            System.out.println(" circle draw ");
        }
    }
    public class Rectangle implements Shape {
    
        @Override
        public void draw() {
            System.out.println(" rectangle draw ");
        }
    }
    public class Square implements Shape {
    
        @Override
        public void draw() {
            System.out.println(" square draw ");
        }
    }

    ShapeMaker,提供简洁的接口给外部客户端调用

    public class ShapeMaker {
        
        private Shape circle;
        private Shape rectangle;
        private Shape square;
        
        public ShapeMaker(){
            this.circle = new Circle();
            this.rectangle = new Rectangle();
            this.square = new Square();    
        }
        
        public void drawCircle(){
            circle.draw();
        }
        
        public void drawRectangle(){
            rectangle.draw();
        }
        
        public void drawSquare(){
            square.draw();
        }
    }

    演示 Facade Pattern

    public class FacadePatternDemo {
        
        public static void main(){
            ShapeMaker shapeMaker = new ShapeMaker();
            shapeMaker.drawCircle();
            shapeMaker.drawRectangle();
            shapeMaker.drawSquare();
        }
    }

    参考资料

    Design Patterns - Facade Pattern, TutorialsPoint

  • 相关阅读:
    17-canvas绘制扇形
    16-canvas绘制圆弧
    15-canvas渐变色
    14-canvas绘制柱状图
    13-绘制矩形的简写方式
    12-es6类的方式封装折线图
    11-canvas绘制折线图
    10-canva绘制数据点
    jenkins 环境部署 (yum安装方式)
    BerkeleyDB安装
  • 原文地址:https://www.cnblogs.com/TonyYPZhang/p/5515162.html
Copyright © 2020-2023  润新知