首页 > 其他分享 >typescript: Facade Pattern

typescript: Facade Pattern

时间:2023-10-08 12:13:17浏览次数:43  
标签:subsystem1 typescript Pattern subsystem2 result https Facade com

 

/**
 * Facade pattern 外观是一种结构型设计模式, 能为复杂系统、 程序库或框架提供一个简单 (但有限) 的接口。
 * The Facade class provides a simple interface to the complex logic of one or
 * several subsystems. The Facade delegates the client requests to the
 * appropriate objects within the subsystem. The Facade is also responsible for
 * managing their lifecycle. All of this shields the client from the undesired
 * complexity of the subsystem.
 */
class Facade {

    protected subsystem1: Subsystem1;

    protected subsystem2: Subsystem2;

    /**
     * Depending on your application's needs, you can provide the Facade with
     * existing subsystem objects or force the Facade to create them on its own.
     */
    constructor(subsystem1?: Subsystem1, subsystem2?: Subsystem2) {
        this.subsystem1 = subsystem1 || new Subsystem1();
        this.subsystem2 = subsystem2 || new Subsystem2();
    }

    /**
     * The Facade's methods are convenient shortcuts to the sophisticated
     * functionality of the subsystems. However, clients get only to a fraction
     * of a subsystem's capabilities.
     */
    public operation(): string {
        let result = 'Facade initializes subsystems:\n';
        result += this.subsystem1.operation1();
        result += this.subsystem2.operation1();
        result += 'Facade orders subsystems to perform the action:\n';
        result += this.subsystem1.operationN();
        result += this.subsystem2.operationZ();

        return result;
    }


    public operationSub2(): string {
        let result = 'Facade initializes subsystems:\n';   
        result += this.subsystem2.operation1();
        result += 'Facade orders subsystems to perform the action:\n';    
        result += this.subsystem2.operationZ();

        return result;
    }
}

/**
 * The Subsystem can accept requests either from the facade or client directly.
 * In any case, to the Subsystem, the Facade is yet another client, and it's not
 * a part of the Subsystem.
 */
class Subsystem1 {
    public operation1(): string {
        return 'Subsystem1: Ready!\n';
    }

    // ...

    public operationN(): string {
        return 'Subsystem1: Go!\n';
    }
}

/**
 * Some facades can work with multiple subsystems at the same time.
 */
class Subsystem2 {
    public operation1(): string {
        return 'Subsystem2: Get ready!\n';
    }

    // ...

    public operationZ(): string {
        return 'Subsystem2: Fire!';
    }
}

/**
 * The client code works with complex subsystems through a simple interface
 * provided by the Facade. When a facade manages the lifecycle of the subsystem,
 * the client might not even know about the existence of the subsystem. This
 * approach lets you keep the complexity under control.
 */
function clientCodeFacade(facade: Facade) {
    // ...
    let str="";
    console.log(facade.operation());
    str=facade.operation();

    return str;

    // ...
}

/**
 * The client code may have some of the subsystem's objects already created. In
 * this case, it might be worthwhile to initialize the Facade with these objects
 * instead of letting the Facade create new instances.
 */
const subsystem1 = new Subsystem1();
const subsystem2 = new Subsystem2();
const facade = new Facade(subsystem1, subsystem2);
//clientCodeFacade(facade);

let puFac1=clientCodeFacade(facade);
const facade2 = new Facade(subsystem1, subsystem2);

let puFac2=facade2.operationSub2();

let messageFacade: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messageFacade+",one="+puFac1+",two="+puFac2+",TypeScript Facade Pattern 外观模式"

  

调用:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript:Facade Pattern 外观模式</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/>  
    </head>
    <body>
        <script src="dist/Facadets.js"></script>
    </body>
</html>

  

输出:

 


data structures and algorithms with object-oriented design patterns in c++
https://web.archive.org/web/20161220060802/http://www.brpreiss.com/books/opus4/
https://eduarmandov.files.wordpress.com/2017/05/c_c-data-structures-and-algorithms-in-c.pdf
http://www.uoitc.edu.iq/images/documents/informatics-institute/Competitive_exam/DataStructures.pdf
https://faculty.washington.edu/jstraub/dsa/Master_2_7a.pdf
https://cslab.pepperdine.edu/warford/cosc320/dp4ds-Chapter-01.pdf
https://cslab.pepperdine.edu/warford/cosc320/dp4ds-Chapter-02.pdf
https://cslab.pepperdine.edu/warford/cosc320/dp4ds-Chapter-04.pdf

 

data structures and algorithms with object-oriented design patterns in java
https://web.archive.org/web/20161130145728/http://www.brpreiss.com/books/opus5/
https://book.huihoo.com/data-structures-and-algorithms-with-object-oriented-design-patterns-in-java/html/
https://everythingcomputerscience.com/books/schoolboek-data_structures_and_algorithms_in_java.pdf
http://bedford-computing.co.uk/learning/wp-content/uploads/2016/08/Data-Structures-and-Algorithms-in-Java-6th-Edition.pdf
https://cin.ufpe.br/~grm/downloads/Data_Structures_and_Algorithms_in_Java.pdf


data structures and algorithms with object-oriented design patterns in c#
https://web.archive.org/web/20161016151655/http://www.brpreiss.com/books/opus6/
https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/CSharp/O%27Reilly%20C%23%203.0%20Design%20Patterns.pdf
https://dl.ebooksworld.ir/books/Design.Patterns.in.NET.6.3rd.Edition.Dmitri.Nesteruk.Apress.9781484282441.EBooksWorld.ir.pdf
http://programming.etherealspheres.com/backup/ebooks%20-%20Other%20Programming%20Languages/Data%20Structures%20And%20Algorithms%20With%20Object-Oriented%20Design%20Patterns%20In%20C%20Sharp.pdf


data structures and algorithms with object-oriented design patterns in python
https://web.archive.org/web/20161220091736/http://www.brpreiss.com/books/opus7/
https://github.com/cjbt/Free-Algorithm-Books/blob/master/book/Data%20Structures%20%26%20Algorithms%20in%20Python.pdf
https://github.com/cjbt/Free-Algorithm-Books/
https://edu.anarcho-copy.org/Programming%20Languages/Python/Python%20Data%20Structures%20and%20Algorithms.pdf

 

https://github.com/GauravWalia19/Free-Algorithms-Books
https://github.com/takaakit/design-pattern-examples-in-typescript
https://typescript.helpful.codes/patterns/Bridge/

 

标签:subsystem1,typescript,Pattern,subsystem2,result,https,Facade,com
From: https://www.cnblogs.com/geovindu/p/17748569.html

相关文章

  • TypeScript入门到精通——TypeScript类型系统基础——元组类型
    TypeScript类型系统基础——元组类型 元组(Tuple)表示由有限元素构成的有序列表。在JavaScript中,没有提供原生的元组数据类型。TypeScript对此进行了补充,提供了元组数据类型。由于元组数组之间存在很多共性,因此TypeScript使用数组来表示元组。 在TypeScript中,元组类型......
  • TypeScript入门到精通——TypeScript类型系统基础——数组类型
    数组类型 数组是十分常用的数据结构,它表示一组有序元素的集合。在TypeScript中,数组值的数据类型为数组类型。一、数组类型定义 TypeScript提供了以下两种方式来定义数组类型:简单数组类型表示法泛型数组类型表示法1.1、简单数组类型表示法在TypeScript中,你可以使......
  • 【愚公系列】2023年10月 二十三种设计模式(五)-单例模式(Singleton Pattern)
    ......
  • typescript: Adapter pattern
     /***Adapterpattern适配器是一种结构型设计模式,它能使不兼容的对象能够相互合作。*file:Adapterts.ts***//***TheTargetdefinesthedomain-specificinterfaceusedbytheclientcode.*/classTarget{publicrequest():string{......
  • typescript: Singleton Pattern
     /***file:Singletonts.ts*SingletonPattern单例是一种创建型设计模式,让你能够保证一个类只有一个实例,并提供一个访问该实例的全局节点。*TheSingletonclassdefinesthe`getInstance`methodthatletsclientsaccess*theuniquesingletoninstance.......
  • webpack - 构建支持TypeScript的React应用
    1.初始化package.json创建项目文件夹mkdirwebpack-react-tscdwebpack-react-ts初始化项目package.jsonyarninit-y{"name":"webpack-react-ts","version":"1.0.0","main":"index.js","license&......
  • Flutter/Dart第10天:Dart高级特性Pattern模式的全部类型(共15种)
    Dart官方文档:https://dart.dev/language/pattern-types重要说明:本博客基于Dart官网文档,但并不是简单的对官网进行翻译,在覆盖核心功能情况下,我会根据个人研发经验,加入自己的一些扩展问题和场景验证。和操作符一样,模式运算也遵循一定的优先级规则,我们可以通过增加括号()让低优先级......
  • typescript: Prototype Pattern
     /***PrototypePattern原型是一种创建型设计模式,使你能够复制对象,甚至是复杂对象,而又无需使代码依赖它们所属的类。*Theexampleclassthathascloningability.We'llseehowthevaluesoffield*withdifferenttypeswillbecloned.*/classPrototype......
  • typescript: Builder Pattern
     /***TypeScript实体类Model*BuilderPattern*生成器是一种创建型设计模式,使你能够分步骤创建复杂对象。*https://stackoverflow.com/questions/12827266/get-and-set-in-typescript*https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines*/cl......
  • 什么是 TypeScript 的类型增强功能
    TypeScript的类型增强(TypeAugmentation)是一种功能,它允许您扩展现有类型的成员,以添加新的属性或方法,以及修改已有属性或方法的类型定义。这个功能让您可以更好地适应第三方库或原始代码,以便在不修改源代码的情况下添加自定义的类型信息。在本文中,我将详细介绍TypeScript的类型增......