javascript / PHP [Design Patterns - Facade Pattern]
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]的更多相关文章
- [Design Patterns] 02. Structural Patterns - Facade Pattern
前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...
- Learning JavaScript Design Patterns The Observer Pattern
The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...
- Learning JavaScript Design Patterns The Module Pattern
The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...
- AMD - Learning JavaScript Design Patterns [Book] - O'Reilly
AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...
- [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. 软件模 ...
- 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 ...
- Design Patterns All in One (JavaScript Version)
Design Patterns All in One (JavaScript Version) JavaScript 设计模式 JavaScript 数据结构 23种设计模式分为 3 大类: 创建型模 ...
- [Design Patterns] 4. Creation Pattern
设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...
- 设计模式(Design Patterns)Java版
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
随机推荐
- E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)E: Unable to l ...
- 使用msp432搭建的平衡小车(一)
1.前言 笔者是一名大二学生曾经荒废一年学业,现在不断学习,所以有任何问题都希望讨论提出,你们的支持就是我的动力. 关于硬件搭建的步骤,笔者就不提网上方案太多了,笔者使用编码器电机,驱动采用tb661 ...
- Jpa-操作mongodb
pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- 9、二进制安装K8s之增加node
二进制安装K8s之增加node 1.复制文件,要部署几台就直接复制即可 #二进制文件 scp /data/k8s/bin/{kubelet,kube-proxy} root@192.168.100.1 ...
- 【springcloud alibaba】配置中心之nacos
接着上一篇的[springcloud alibaba]注册中心之nacos,这一篇主要讲nacos的配置中心能力.nacos的集群部署及持久化请看上一篇. ---------------------- ...
- unitest单元测试TestCase 执行测试用例(一)
前言 unittest单元测试框架不仅可以适用于单元测试,还可以适用自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果. uni ...
- Sentinel限流、降级配置详解
安装Sentinel 下载sentinel-dashboard-1.8.2.jar 安装有jdk环境,8080端口未被占用 在jar包所在目录打开cmd,输入命令启动:java -jar sentin ...
- Git使用教程一
Git是一个分布式版本控制系统,简单的说其就是一个软件,用于记录一个或若 干文件内容变化,以便将来查阅特定版本修订情况的软件. Github (https://www.github.com) 是-一个 ...
- SSH无法正常连接服务器
远程权限没有打开 #允许root登录 PermitRootLogin yes #不允许空密码登录 PermitEmptyPasswords no 远端的ssh信息有变化,本地保存的那个需要删掉 Use ...
- 为什么Class实例可以不是全局唯一的——自定义类加载器
为什么Class实例可以不是全局唯一的 通过定义两个类加载器加载同一字节码文件来证明Class实例为什么不是全局唯一的 1.将一个名为Demo(没有后缀)的字节码文件放在D盘根目录 2.定义两个类加载 ...