码迷,mamicode.com
首页 > 编程语言 > 详细

javascript [Design Patterns - Facade Pattern]

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

标签:function   pattern   pre   als   raw   require   ons   rri   script   

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]

标签:function   pattern   pre   als   raw   require   ons   rri   script   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!