首页 > 其他分享 >typescript: Adapter pattern

typescript: Adapter pattern

时间:2023-10-06 20:13:06浏览次数:43  
标签:code Target pattern Adapter ts typescript install edu true

 

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

  

输出:

 

 

 

 tsconfig.json:

{
    "compilerOptions": {
   
      "target": "ES6",  //"target": "es5",
      "skipLibCheck": true,
      "module": "CommonJS", //CommonJS  //esnext
      "outDir": "./dist",
      "rootDir": "./src",      
      "esModuleInterop": true,
      "resolveJsonModule": true,
      "composite": true,  // required on the dependency project for references to work https://github.com/microsoft/TypeScript/issues/30693
      "sourceMap": true, //true //false
      "moduleResolution": "node",     
      "allowSyntheticDefaultImports": true,
      //"strict": true,   
      //"strictPropertyInitialization": false
      /*"emitDecoratorMetadata": false,
      "experimentalDecorators": true,
      "removeComments": false,
      "strict": true,
      "strictNullChecks": true,*/
      //"declaration": false
      /*
      "strict": true,
      "experimentalDecorators": true,
      "useDefineForClassFields": false,
      "skipLibCheck": true, 
      "forceConsistentCasingInFileNames": true,
      "esModuleInterop": true,
      "moduleResolution": "node",
        // Tells TypeScript to read JS files, as normally they are ignored as source files
        "allowJs": true,
        // Generate d.ts files
        "declaration": true,
        // go to js file when using IDE functions like "Go to Definition" in VSCode
        "declarationMap": true,
         // This compiler run should only output d.ts files
        "emitDeclarationOnly": true
        "lib": [
          "es2017",
          "es2018"
        ]*/

    },
    "includes": [
    "src/**/*.ts",
    "other-src/**/*.ts"
    ],
    "exclude":[
      "rollup.config.js",
      "test",
      "dist",
      "node_modules",
    ],
  }

  

npm init -y
npm install -g typescript
tsc --version

npm install -g ts-node
npm install eslint --save-dev

npm install typescript typescript-eslint-parser --save-dev
npm install eslint-plugin-typescript --save-dev

npm install --save-dev ts-loader
npm install --save-dev typescript ts-loader
npm install --save-dev webpack webpack-cli
npm install webpack
npm i -D webpack webpack-cli webpack-dev-server
npm install webpack webpack-cli typescript ts-loader --save-dev

npm i -D @babel/core @babel/preset-env babel-loader core-js

npm i -D less less-loader css-loader style-loader
npm i -D postcss postcss-loader postcss-preset-env


https://www.typescriptlang.org/docs/handbook/2/modules.html
https://www.typescriptlang.org/tsconfig#module

 

 

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

标签:code,Target,pattern,Adapter,ts,typescript,install,edu,true
From: https://www.cnblogs.com/geovindu/p/17744932.html

相关文章

  • 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的类型增......
  • TypeScript入门到精通——TypeScript类型系统基础——单元类型、顶端类型、尾端类型
    单元类型 单元类型(UnitType)也叫作单例类型(SingletonType),指的是仅包含一个可能值的类型。由于这个特殊的性质,编译器在处理单元类型时甚至不需要关注单元类型表示的具体值。 TypeScript中的单元类型有以下几种:undefined类型null类型uniquesymbol类型void类型......
  • SpringSecurity解决WebSecurityConfigurerAdapter过时问题
    旧版通过继承WebSecurityConfiurerAdapter,但是新版本中该类已经被标注为@Deprecated旧版代码packagecom.lzx.config;importcom.lzx.filter.JwtAuthenticationTokenFilter;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.con......
  • Flutter/Dart第09天:Dart高级特殊Pattern模式的概览和用法
    Dart官方文档:https://dart.dev/language/patterns重要说明:本博客基于Dart官网文档,但并不是简单的对官网进行翻译,在覆盖核心功能情况下,我会根据个人研发经验,加入自己的一些扩展问题和场景验证。Pattern模式匹配的定义官网定义:PatternsareasyntacticcategoryintheDartlan......
  • TypeScript入门到精通——TypeScript类型系统基础——字面量类型
    字面量类型 TypeScript支持将字面量作为类型使用,我们称之为字面量类型。每一个字面量类型都只有一个可能的值,即字面量本身。1、boolean字面量类型 boolean字面量类型只有以下两种:true字面量类型false字面量类型 原始类型boolean等同于由true字面量类型......