javascript [Design Patterns - Facade Pattern]

Posted mingzhanghui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript [Design Patterns - Facade Pattern]相关的知识,希望对你有一定的参考价值。

This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

 

/**
 * Design Patterns - Facade Pattern
 * https://www.tutorialspoint.com/design_pattern/facade_pattern.htm
 */
// interface
function Shape() {}
Shape.prototype.draw = function() {throw "Shape::draw() must be overridden";};

// implements Shape
function Rectangle() {
    Shape.call(this);
}
Rectangle.prototype = new Shape();
Rectangle.prototype.draw = function() {
    console.log("Rectangle::draw()");
};

// implements Shape
function Square() {
    Shape.call(this);
}
Square.prototype = new Shape();
Square.prototype.draw = function() {
    console.log("Square::draw()");
};

// implements shape
function Circle() {
    Shape.call(this);
}
Circle.prototype = new Shape();
Circle.prototype.draw = function() {
    console.log("Circle::draw()");
};

function ShapeMaker() {
    this.circle = new Circle();
    this.rectangle = new Rectangle();
    this.square = new Square();
}
ShapeMaker.prototype.drawCircle = function() {
    this.circle.draw();
};
ShapeMaker.prototype.drawRectangle = function() {
    this.rectangle.draw();
};
ShapeMaker.prototype.drawSquare = function() {
    this.square.draw();
};

function FacadePatternDemo() {
    this.shapeMaker = new ShapeMaker();
}
FacadePatternDemo.prototype.main = function() {
    this.shapeMaker.drawCircle();
    this.shapeMaker.drawRectangle();
    this.shapeMaker.drawSquare();
};

var demo = new FacadePatternDemo();
demo.main();

  

Output:

Circle::draw()
Rectangle::draw()
Square::draw()



以上是关于javascript [Design Patterns - Facade Pattern]的主要内容,如果未能解决你的问题,请参考以下文章

Ant Design Pro 基本语法使用(JavaScript版)

Ant Design Pro V5 实现https运行(javascript版typescript通用)

Ant Design Pro ProTable受控菜单(JavaScript版和TypeScript版)

Ant Design Pro 文件上传(JavaScript版)

Ant Design Pro V5 代理设置(javascript版)

Ant Design Pro V5 去掉界面水印(JavaScript版)