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