typescript: Facade Pattern

发布时间 2023-10-08 12:02:57作者: ®Geovin Du Dream Park™

 

/**
 * 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/