標籤: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 */// interfacefunction Shape() {}Shape.prototype.draw = function() {throw "Shape::draw() must be overridden";};// implements Shapefunction Rectangle() { Shape.call(this);}Rectangle.prototype = new Shape();Rectangle.prototype.draw = function() { console.log("Rectangle::draw()");};// implements Shapefunction Square() { Shape.call(this);}Square.prototype = new Shape();Square.prototype.draw = function() { console.log("Square::draw()");};// implements shapefunction 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]