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. Skywalking-07:OAL原理——解释器实现

    OAL 解释器实现 OAL 解释器是基于 Antlr4 实现的,我们先来了解下 Antlr4 Antlr4 基本介绍 Antlr4 使用案例 参考Antlr4的使用简介这篇文章,我们实现了一个简单的案 ...

  2. Java角度制向弧度制转化

    1.第一次写博客啊写博客啊写啊写0.0..0. 2.输入正多边形的边长·边数·求正多边形的面积 3.超级简单,可是在转弧度制那里有点懵,刚开始学Java,所以难免走弯路 4.代码如下: 1 publi ...

  3. 【Tools】SSHUsage

    SSH(Secure Shell 的缩写)是一种网络协议,用于加密两台计算机之间的通信,并且支持各种身份验证机制.还能对操作者进行认证(authentication)和授权(authorization ...

  4. C#多线程详解(一) Thread.Join()的详解

    bicabo   C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...

  5. C# 计算文件的MD5

    MD5的作用详见:https://baike.baidu.com/item/MD5/212708?fr=aladdin public static string GetFileMD5(string f ...

  6. Spring第一课:基于XML装配bean(四),三种实例化方式:默认构造、静态工厂、实例工厂

    Spring中基于XML中的装配bean有三种方式: 1.默认构造 2.静态工厂 3.实例工厂 1.默认构造 在我们在Spring的xml文件中直接通过:     <bean id=" ...

  7. Dynamics CRM实体系列之1:N、N:1以及N:N关系

    Dynamics CRM在实施过程中会遇到很多多个实体关联的问题,这样可以实现多个实体的记录通过关联的字段实现数据的综合展示,在Sql Server里面叫做外键,在Dynamics CRM叫做关系.D ...

  8. Java如何调用C语言程序,JNI技术

    Java为什么要调用C语言编写的程序因为涉及操作系统底层的事件,Java是处理不了的,例如用户上传一个视频文件,需要后台给视频加上水印,或者后台分离视频流和音频流,这个事Java就做不了,只能交给C语 ...

  9. HTML5存储 ——Web Storage(localStorage 和 sessionStorage)

    一.localStorage对象临时储存API 方法: 1.localStorage.setItem(key,value)---设置存储内容 2.localStorage.getItem(key)-- ...

  10. 高德地图——步行路线&步行路线的坐标规划

    步行操作与开车一样 唯一区别就是src末尾加入&plugin=AMap.Walkling 以及new AMap.Walking({}) <!DOCTYPE html> <ht ...