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)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
随机推荐
- AWS(amazon ec2)服务器流量查询
aws ec2流量监控 亚马逊云服务新用户绑定信用卡免费使用一年,相信很多人白嫖过,选用micro最低配置+流量免费15G,包含上下行.这种配置用来测试玩玩还行,生产使用的话容易超标.很多人想知道流量 ...
- noip20
T1 首先,这个插球,就是森林中连点,考虑dp,我们设 \(dp_{i,j}\) 表示有i个点的森林,有j个点在第一颗树中的概率,转移时考虑第i个点是否在第一颗子树里,转移方程: \[dp_{i,j} ...
- Charles 抓包 Client SSL handshake failed - Remote host closed connection during handshake
Charles 抓包 https 报错: Client SSL handshake failed - Remote host closed connection during handshake # ...
- 使用F#编写PowerShell模块
▲F#和PowerShell模块 作为可能是人类世界最强大的Shell,PowerShell最大的特点是能够直接在命令间传递.NET对象,而支持这种能力的命令被称作cmdlet.自己编写PowerSh ...
- Visual Studio 2019 使用C语言创建动态链接库(Dll)并使用C语言和C#实现调用
参考网址:https://blog.csdn.net/weixin_34976988/article/details/99625533 一.创建DLL1.建立动态链接库项目 2.创建头文件和源文件 删 ...
- 【springboot】集成Druid 作为数据库连接池
转自:https://blog.csdn.net/cp026la/article/details/86508139 1. 引言 用户的每一次请求几乎都会访问数据库,访问数据库需要向数据库获取链接,而数 ...
- sparksql字段类型转换
1.spark sql 计算时,一定要注意精度的问题,一般像金额之类的值,要先转换为double或者 decimal来进行计算了. 一.sql的方式:select shop_id,order_id,s ...
- 《redis 5设计与源码分析》:第二章 简单动态字符串
介绍 简单动态字符串(Simple Dynamic Strings, SDS)是Redis的基本数据结构之一,用于存储字符串和整型数据.它的特点是:方便扩容.二进制安全. 二进制安全 在C语言中,用& ...
- docker容器 如何精简镜像减小体积
写在前面 我们在上篇<Docker容器 关于镜像构建的安全问题>一起学习了如何构建一个基于安全的镜像,这篇小作文我们会学习镜像构建的另一个关键性问题,为何别人打造的镜像只有10MB而我的有 ...
- Asp.NetCore3.1 WebApi 获取配置json文件中的数据
下面只是做一个简单的测试: 1:定义好appsetting.Json文件的配置信息如下: { "Logging": { "LogLevel": { " ...