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()

<?php
/**
* The complicated, underlying logic.
*/
class CPU {
public function freeze() {
echo 'CPU freeze'.PHP_EOL;
}
public function jump($position) {
printf("CPU jumping to 0x%x %s", $position, PHP_EOL);
}
public function execute() {
echo 'CPU executing...'.PHP_EOL;
}
} class Memory {
public function load($position, $data) {
printf("Loading position 0x%x from memory, \"%s\"%s", $position, implode('', $data), PHP_EOL);
}
} class HardDrive
{
public function read($lba, $size) {
printf("HardDrive is reading sector#%d, size=%d %s", $lba, $size, PHP_EOL);
return ['H','e','l','l','o'];
}
} /**
* The facade that users would be interacting with.
*/
class ComputerFacade
{
protected $cpu;
protected $memory;
protected $hd; public function __construct()
{
$this->cpu = new CPU;
$this->ram = new Memory;
$this->hd = new HardDrive;
} public function start()
{
$this->cpu->freeze();
$this->ram->load(0x8280, $this->hd->read(0, 512));
$this->cpu->jump(0x8280);
$this->cpu->execute();
}
} /**
* How a user could start the computer.
*/
$computer = new ComputerFacade;
$computer->start();

  

OUTPUT:

CPU freeze
HardDrive is reading sector#0, size=512
Loading position 0x8280 from memory, "Hello"
CPU jumping to 0x8280
CPU executing...

javascript / PHP [Design Patterns - Facade Pattern]的更多相关文章

  1. [Design Patterns] 02. Structural Patterns - Facade Pattern

    前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...

  2. Learning JavaScript Design Patterns The Observer Pattern

    The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...

  3. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  4. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  5. [Design Patterns] 3. Software Pattern Overview

    When you're on the way which is unknown and dangerous, just follow your mind and steer the boat. 软件模 ...

  6. Design Patterns Uncovered: The Chain Of Responsibility Pattern

    Chain of Responsibility in the Real World The idea of the Chain Of Responsibility is that it avoids ...

  7. Design Patterns All in One (JavaScript Version)

    Design Patterns All in One (JavaScript Version) JavaScript 设计模式 JavaScript 数据结构 23种设计模式分为 3 大类: 创建型模 ...

  8. [Design Patterns] 4. Creation Pattern

    设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...

  9. 设计模式(Design Patterns)Java版

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

随机推荐

  1. DVWA靶场之SQL injection(blind)通关

    盲注,顾名思义,无法从界面上直接查看到执行结果,一般的SQL注入基本绝迹,最多的就是盲注 基本步骤:(由于没有回显了,相比一般的SQL注入,也就不需要确定查询字段数.判断回显位置了) 判断注入类型 破 ...

  2. 30 个极大提高开发效率超级实用的 VSCode 插件

    Visual Studio Code 的插件对于在提升编程效率和加快工作速度非常重要.这里有 30 个最受欢迎的 VSCode 插件,它们将使你成为更高效的搬砖摸鱼大师.这些插件主要适用于前端开发人员 ...

  3. 分享一个自己制作的XML在线编辑器

    前言 一年多没更新博客了,原因是疫情期间<骑马与砍杀2>发售,然后去写游戏MOD去了. 用C#大概写了7个月的游戏MOD,每天晚上肝到很晚,然后期间又因为介绍这个游戏MOD,学习了PR,然 ...

  4. SpringCloud升级之路2020.0.x版-19.Eureka的服务端设计与配置

    本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford Eureka Se ...

  5. SQL 练习34

    求每门课程的学生人数 SELECT cid,COUNT(cid) 课程人数 from sc GROUP BY cid

  6. L298N的接线和详细使用方法

    文章说明: 名词概念(为了方便易懂,我就通俗的表达): 逻辑电压:控制板子执行程序的电压. 驱动电压:输出口AB的电压. 逻辑电流:驱动板执行程序的电流. 驱动电流:输出口AB的电流. 本人调试此款L ...

  7. noip28

    东方专场? T1 %%%WYZG 话说我考场上还想二维hash来着 考虑只记录弹幕中x的相对位置. 先选定弹幕一个点作为基准点(第一个出现的x即可),然后,枚举其他的x,记录下坐标差,然后去方格图中枚 ...

  8. C++类构造函数、拷贝构造函数、复制构造函数、复制构造函数、构造函数显示调用和隐式调用

    一. 构造函数是干什么的   class Counter   {   public:            // 类Counter的构造函数            // 特点:以类名作为函数名,无返回 ...

  9. 信号量-Semaphore、SemaphoreSlim

    核心类:Semaphore,通过int数值来控制线程个数. * 通过观察构造函数 public Semaphore(int initialCount, int maximumCount);: * in ...

  10. SpringMVC之@ControllerAdvice

    @ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...