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. 题解 [NOIP2017 提高组]宝藏

    传送门 这是蓝书上状压的例题啊,怎么会出现在模拟赛里 不过就算原题我也没把握写对 核心思路: 先令\(dp[s]\)为当前状态为\(s\)时的总花费最小值,\(cnt[s][i]\)为这个方案中由根节 ...

  2. 10、二进制安装K8s之部署CoreDNS 和Dashboard

    二进制安装K8s之部署CoreDNS 和Dashboard CoreDNS 和Dashboard 的yaml文件在 k8s源代码压缩包里面可以找到对应的配置文件,很多人从网上直接下载使用别人的,会导致 ...

  3. tcp为什么要三次握手,tcp为什么可靠

    转自 : https://www.cnblogs.com/LUO77/p/5771237.html大体看过,没有深入研究,有需要时继续看. 为什么不能两次握手:(防止已失效的连接请求又传送到服务器端, ...

  4. HDFS简介及基本概念

    (一)HDFS简介及其基本概念   HDFS(Hadoop Distributed File System)是hadoop生态系统的一个重要组成部分,是hadoop中的的存储组件,在整个Hadoop中 ...

  5. 关于 go-fastdfs-web 的SpringBoot 后台管理

    1.问题的产生: 1.公司需要存储图片数据,采用Go语言的fastdfs,实现存储,我的职责就是部署,SpringBoot版本的管理平台. 2.当我看见代码之后我的内心是拒绝的,没有注释....... ...

  6. Spring中Resource(资源)的获取

    1.通过Resource接口获取资源 Resource接口的实现类有: Resource接口继承了InputStreamSource 接口,InputStreamSource 接口中有一个方法:get ...

  7. 队列(Queue)\双端队列(Deque)

    队列(Queue)\双端队列(Deque) 队列(Queue) 双端队列(Deque) 算法应用 队列(Queue) 特点: 和栈不同,队列的最大特点是先进先出(FIFO),就好像按顺序排队一样.对于 ...

  8. MySql 文件导入导出

    1.将表输出到文件 select * FROM zhilianzhaopin4 INTO OUTFILE 'G:/test.csv'      --------输出位置 fields terminat ...

  9. AI 常见术语总结

      BN(Batch-normalization)在一层的输出上计算所有特征映射的均值和标准差,并且使用这些值规范化它们的响应.因此使得所有神经图(neural maps)在同样范围有响应,而且是零均 ...

  10. Android手机 自动批量发朋友圈

    做了各种对比之后,还是这个微商工具最好用