typescript: Adapter pattern

发布时间 2023-10-06 20:06:20作者: ®Geovin Du Dream Park™

 

/**
 * Adapter pattern 适配器是一种结构型设计模式, 它能使不兼容的对象能够相互合作。
 * file: Adapterts.ts
 * 
 * 
 */
/**
 * The Target defines the domain-specific interface used by the client code.
 */
class Target {
    public request(): string {
        return 'Target: The default target\'s behavior.';
    }
}

/**
 * The Adaptee contains some useful behavior, but its interface is incompatible
 * with the existing client code. The Adaptee needs some adaptation before the
 * client code can use it.
 */
class Adaptee {

    /**
     * 
     * @returns 
     */
    public specificRequest(): string {
        return '.eetpadA eht fo roivaheb laicepS';
    }
}

/**
 * The Adapter makes the Adaptee's interface compatible with the Target's
 * interface.
 */
class Adapter extends Target {
    /**
     * 
     */
    private adaptee: Adaptee;
    /**
     * 
     * @param adaptee 
     */
    constructor(adaptee: Adaptee) {
        super();
        this.adaptee = adaptee;
    }
    /**
     * 
     * @returns 
     */
    public request(): string {
        const result = this.adaptee.specificRequest().split('').reverse().join('');
        return `Adapter: (TRANSLATED) ${result}`;
    }
}

/**
 * The client code supports all classes that follow the Target interface.
 * @param target 
 * @returns 
 */
 
function clientCodeadapter(target: Target) {
    let str="";
    console.log(target.request());
    str=target.request.toString();
    return str;
}

console.log('Client: I can work just fine with the Target objects:');
const target = new Target();
clientCodeadapter(target);

console.log('');

const adaptee = new Adaptee();
console.log('Client: The Adaptee class has a weird interface. See, I don\'t understand it:');
console.log(`Adaptee: ${adaptee.specificRequest()}`);

console.log('');

console.log('Client: But I can work with it via the Adapter:');
const adapter = new Adapter(adaptee);
clientCodeadapter(adapter);

let strada=clientCodeadapter(adapter);
let strada1="geovindu";
let messageada: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messageada+","+strada+","+strada1+",TypeScript 适配器模式"

  

调用:

<!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:适配器模式</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/Adapterts.js"></script>
    </body>
</html>

  

输出:

 

 

 

 

data structures and problem solving using java 4th edition
https://cscnt.savannahstate.edu/StudentFiles/Data_Structure/Data-Structures-Problem-Solving-Using-Java.pdf
http://computo.fismat.umich.mx/~htejeda/books/data/Data_Structures_and_Problem_Solving_Using_Java__4ed__Weiss.pdf
https://users.cis.fiu.edu/~weiss/dsj4/code/
https://users.cis.fiu.edu/~weiss/dsj3/code/
http://users.cs.fiu.edu/~weiss/dsj2/
http://users.cs.fiu.edu/~weiss/dsj2/code/code.html
http://users.cs.fiu.edu/~weiss/dsj/code/
https://users.cis.fiu.edu/~weiss/
https://users.cis.fiu.edu/~weiss/cs/
https://users.cis.fiu.edu/~weiss/dsaa_c++4/code/

data structures and algorithms using java
https://cin.ufpe.br/~grm/downloads/Data_Structures_and_Algorithms_in_Java.pdf
https://github.com/shshankar1/ebooks/blob/master/Data%20Structures%20and%20Algorithms%20in%20Java%2C%206th%20Edition.pdf
http://bedford-computing.co.uk/learning/wp-content/uploads/2016/08/Data-Structures-and-Algorithms-in-Java-6th-Edition.pdf
https://everythingcomputerscience.com/books/schoolboek-data_structures_and_algorithms_in_java.pdf

data structures and algorithms using c++
http://www.uoitc.edu.iq/images/documents/informatics-institute/Competitive_exam/DataStructures.pdf
https://eduarmandov.files.wordpress.com/2017/05/c_c-data-structures-and-algorithms-in-c.pdf
https://github.com/GunterMueller/Books-3/blob/master/Data%20Structure%20and%20Algorithm%20Analysis%20in%20C%2B%2B%204th%20ed.pdf
https://people.cs.vt.edu/shaffer/Book/C++3e20100119.pdf
https://github.com/GunterMueller/Books-3
https://github.com/GunterMueller/Books
https://github.com/GunterMueller/Books-2