首页 > 编程语言 > 详细

javascript [Design Patterns - Facade Pattern]

时间:2018-07-25 11:07:02      阅读:145      评论:0      收藏:0      [点我收藏+]

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]

原文:https://www.cnblogs.com/mingzhanghui/p/9364541.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!