typescript: Builder Pattern

news2024/10/7 6:49:28

/**
 * file: CarBuilderts.ts
 * TypeScript 实体类 Model
 * Builder Pattern
 * 生成器是一种创建型设计模式, 使你能够分步骤创建复杂对象。
 * https://stackoverflow.com/questions/12827266/get-and-set-in-typescript
 * https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
 */
class UserInfo {
    id!: number;
    userName!: string;
    email!: string;
  }


  /**
   * 车辆实体类
   */
  class Car { //export

    /**
     * 序号
     */
    id!: number;
    /**
     * 座位
     */
    seats!: number;
    /**
     * 发动机
     */
    engine!: string;

   /* constructor(id, seats,engine){
        this.id = id;
        this.seats = seats;
        this.engine=engine;
    }*/

    /**
     * 序号
     * @returns  返回序号
     */
    public  getId(): number { //get
        return this.id;
    }

    /**
     * 座位数
     * @returns 返回座位数
     */
    public  getSeats(): number { //get
        return this.seats;
    }
    /**
     * 发动机
     * @returns 返回发动机型号名称
     */
    public  getEngine(): string { //get
        return this.engine;
    }
    /**
     * 设置座位数
     * @param seats 输入数字座位数
     */
    public setSeats(seats: number) //set
    {
        this.seats=seats;
    }
    /**
     * 设置发动机型号
     * @param engine 输入型号名称
     */
    public setEngine(engine: string) //set
    {
        this.engine=engine;
    }
    /**
     * 
     * @param id 
     */
    public setId(id:number)
    {
      this.id=id;
    }

}
  
/**
 * 继承
 */
class Motorcycle extends Car
{
   /*  id!: number;
    seats!: number;
    engine!: string;

   constructor(id, seats,engine){
        this.id = id;
        this.seats = seats;
        this.engine=engine;
    }

    public  getId(): number { //get
        return this.id;
    }

    public  getSeats(): number { //get
        return this.seats;
    }

    public  getEngine(): string { //get
        return this.engine;
    }

    public setSeats(seats: number) //set
    {
        this.seats=seats;
    }

    public setEngine(engine: string) //set
    {
        this.engine=engine;
    }*/

}
/*
  interface DuBuilder<Car>()
    .id(1)
    .setSeats("")
    .setEngine("")
    .build();
*/

/**
 * 接口extends Car
 */
interface CBuilder {


    /**
     * 
     * @param seats 
     */
    setSeats(seats: number): this;
    /**
     * 
     * @param engine 
     */
    setEngine(engine: string): this;

    /**
     * 
     * @param id 
     */
    setId(id:number):this;

  }
  

/**
 * 继承 Builder
 */
class CarBuilder implements CBuilder {

    /**
     * 车信息类
     */
    private car: Car;
  
    /**
     * 实例化
     */
    constructor() {
      this.car = new Car();
    }
    /**
     * 设置座位数
     * @param seats 座位号
     * @returns 返回座位号
     */
    public setSeats(seats: number): this {
      //this.car.setSeats(seats);
    
      this.car.setSeats(seats);
      return this;
    }
    /**
     * 设置发动机型号
     * @param engine 发动机型号名称
     * @returns 
     */
    public setEngine(engine: string): this {
      this.car.setEngine(engine);
      return this;
    }
    /**
     * id 序号
     * @param id 
     * @returns 
     */
    public setId(id:number):this{
      this.car.setId(id);
      return this;
    }
    /**
     * 得到实体
     * @returns 返回车信息类
     */
    public getResult(): Car {
      return this.car;
    }
  }
  

/**
 * 
 *  */ 
class MotorcycleBuilder implements CBuilder {

    /**
     * 
     */
    private motorcycle: Motorcycle;
  
    /**
     * 
     */
    constructor() {
      this.motorcycle = new Motorcycle();
    }
    /**
     * 
     * @param seats 
     * @returns 
     */
    public setSeats(seats: number): this {
      this.motorcycle.setSeats(seats);
      return this;
    }
    /**
     * 
     * @param engine 
     * @returns 
     */
    public setEngine(engine: string): this {
      this.motorcycle.setEngine(engine);
      return this;
    }
    /**
     * 
     * @param id 
     * @returns 
     */
    public  setId(id: number): this {
        this.motorcycle.setId(id);
        return this;
    }
    /**
     * 
     * @returns 
     */
    public getResult(): Motorcycle {
      return this.motorcycle;
    }
  }
 
  /**
   * 
   */
  class DuDirector {

    /**
     * 
     * @returns 
     */
    public buildFerrari(): Car {
      return new CarBuilder().setId(1).setSeats(2).setEngine("V-12").getResult();
    }
    /**
     * 
     * @returns 
     */
    public buildToyota(): Car {
      return new CarBuilder().setId(2).setSeats(7).setEngine("V-6").getResult();
    }
    /**
     * 
     * @returns 
     */
    public buildHonda(): Motorcycle {
      return new MotorcycleBuilder().setId(3).setSeats(2).setEngine("V-4").getResult();
    }
    /**
     * 
     * @returns 
     */
    public buildYamaha(): Motorcycle {
      return new MotorcycleBuilder().setId(4).setSeats(1).setEngine("V-2").getResult();
    }
  }


/**
 * 
 */

const directorBu = new DuDirector();

directorBu.buildFerrari();
directorBu.buildToyota();

directorBu.buildHonda();
directorBu.buildYamaha();

const car = new CarBuilder().setSeats(2).setEngine("V-12").getResult();

const motorcycle = new MotorcycleBuilder()
  .setId(100)
  .setSeats(2)
  .setEngine("V-4")
  .getResult();

let pucarid=""+motorcycle.getId();
let pucar1=""+motorcycle.getSeats();
let pucar2=""+motorcycle.getEngine();

let messageCar: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messageCar+"<br/>,id:"+pucarid+",座位数:"+pucar1+",发动机型号:"+pucar2+","+car.getSeats()+","+car.getEngine()+","+directorBu.buildFerrari().getEngine()+",TypeScript 生成器方法模式";
console.log(motorcycle.getId());
console.log(motorcycle.getSeats());
console.log(motorcycle.getEngine());

调用:

<!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/CarBuilderts.js"></script>
    </body>
</html>

输出:

“The best strategy in life is diligence."         --Chinese Proverb

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1062275.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

制作 3 档可调灯程序编写

PWM 0~255 可以将数据映射到0 75 150 225 尽可能均匀电压间隔

Python的NumPy库(一)基础用法

NumPy库并不是Python的标准库&#xff0c;但其在机器学习、大数据等很多领域有非常广泛的应用&#xff0c;NumPy本身就有比较多的内容&#xff0c;全部的学习可能涉及许多的内容&#xff0c;但我们在这里仅学习常见的使用&#xff0c;这些内容对于我们日常使用NumPy是足够的。 …

【Python】datetime 库

# timedelta(days, seconds, microseconds,milliseconds, minutes, hours, weeks) 默认按顺序传递参数 # 主要介绍 datetime.datetime 类 # 引入 from datetime import datetime today datetime.now() # 获取当前时间 2023-10-05 15:58:03.218651 today1 datetime.utcnow() #…

经典算法-----汉诺塔问题

前言 今天我们学习一个老经典的问题-----汉诺塔问题&#xff0c;可能在学习编程之前我们就听说过这个问题&#xff0c;那这里我们如何去通过编程的方式去解决这么一个问题呢&#xff1f;下面接着看。 汉诺塔问题 问题描述 这里是引用汉诺塔问题源自印度一个古老的传说&#x…

Ubuntu 22.04 安装Nvidia显卡驱动、CUDA、cudnn

GPU做深度学习比CPU要快很多倍&#xff0c;用Ubuntu跑也有一定的优势&#xff0c;但是安装Nvidia驱动有很多坑 Ubuntu版本&#xff1a;22.04.3 LTS 分区&#xff1a; /boot分配 1G &#xff0c;剩下都分给根目录/ 显卡&#xff1a;GTX 1050 Ti 坑1&#xff1a;用Ubuntu自带的 …

ESP32上电到app_main()的过程梳理

前言 &#xff08;1&#xff09;如果有嵌入式企业需要招聘校园大使&#xff0c;湖南区域的日常实习&#xff0c;任何区域的暑假Linux驱动实习岗位&#xff0c;可C站直接私聊&#xff0c;或者邮件&#xff1a;zhangyixu02gmail.com&#xff0c;此消息至2025年1月1日前均有效 &am…

【单片机】16-LCD1602和12864和LCD9648显示器

1.LCD显示器相关背景 1.LCD简介 &#xff08;1&#xff09;显示器&#xff0c;常见显示器&#xff1a;电视&#xff0c;电脑 &#xff08;2&#xff09;LCD&#xff08;Liquid Crystal Display&#xff09;&#xff0c;液晶显示器&#xff0c;原理介绍 &#xff08;3&#xff…

哈希表的总结

今天刷了力扣的第一题&#xff08;1. 两数之和 - 力扣&#xff08;LeetCode&#xff09;&#xff09;&#xff0c;是一道用暴力解法就可以完成的题目&#xff08;两个for循环&#xff09;,但是官方解答给出了用哈希表的解法&#xff0c;用空间换时间&#xff0c;时间复杂度从O(…

Jmeter排查正则表达式提取器未生效问题

今天在使用Jmeter的时候遇到一个很简单的问题&#xff0c;使用正则表达式提取token一直未生效&#xff0c;原因是正则表达式中多了一个空格。虽然问题很简单&#xff0c;但是觉得排查问题的方法很普适&#xff0c;所以记录下&#xff0c;也希望能够给遇到问题的大家一个参考。 …

蓝桥杯每日一题2023.10.5

3420. 括号序列 - AcWing题库 题目描述 题目分析 对于这一我们需要有前缀知识完全背包 完全背包的朴素写法&#xff1a; #include<bits/stdc.h> using namespace std; const int N 1010; int n, m, v[N], w[N], f[N][N]; int main() {cin >> n >> m;fo…

MySQL数据库入门到精通——进阶篇(3)

黑马程序员 MySQL数据库入门到精通——进阶篇&#xff08;3&#xff09; 1. 锁1.1 锁-介绍1.2 锁-全局锁1.3 锁-表级锁1.3.1 表级锁-表锁1.3.2 表级锁元数据锁( meta data lock&#xff0c;MDL)1.3.3 表级锁-意向锁1.3.4 表级锁意向锁测试 1.4 锁-行级锁1.4.1 行级锁-行锁1.4.2…

计算机网络 (中科大郑烇老师)笔记(一)概论

目录 0 引言1 什么是Internet&#xff1f;1.1 网络、计算机网络、互联网1.2 什么是Internet&#xff1f;&#xff1a;从服务角度看 2 什么是协议&#xff1f;3 网络的结构&#xff08;子系统&#xff09;3.1 网络边缘3.2 网络核心&#xff1a;分组交换、线路交换3.3 接入网、物…

【13】c++设计模式——>工厂模式

简单工厂模式的弊端 简单工厂模式虽然简单&#xff0c;但是违反了设计模式中的开放封闭原则&#xff0c;即工厂类在数据增加时需要被修改&#xff0c;而我们在设计时对于已经设计好的类需要避免修改的操作&#xff0c;而选用扩展的方式。 工厂模式设计 简单工厂模式只有一个…

天地无用 - 修改朋友圈的定位: 高德地图 + 爱思助手

1&#xff0c;电脑上打开高德地图网页版 高德地图 (amap.com) 2&#xff0c;网页最下一栏&#xff0c;点击“开放平台” 高德开放平台 | 高德地图API (amap.com) 3&#xff0c;在新网页中&#xff0c;需要登录高德账户才能操作。 可以使用手机号和验证码登录。 4&#xff0c…

探秘前后端开发世界:猫头虎带你穿梭编程的繁忙街区,解锁全栈之路

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

香蕉叶病害数据集

1.数据集 第一个文件夹为数据增强&#xff08;旋转平移裁剪等操作&#xff09;后的数据集 第二个文件夹为原始数据集 2.原始数据集 Cordana文件夹&#xff08;162张照片&#xff09; healthy文件夹&#xff08;129张&#xff09; Pestalotiopsis文件夹&#xff08;173张照片&…

用Python实现一个电影订票系统!

一、整体结构图 二、代码分解 2.1 infos.py 一部电影的详细信息适合用 字典 结构来存储&#xff0c;我们可以给字典里添加多个键值对来保存电影的名称、座位表和宣传时用的字符画&#xff0c;比如电影《泰坦尼克号》的详细信息就可以按下面的形式保存到字典 titanic 中&#…

Tomcat在CentOS上的安装部署

目录 1. Tomcat简介 2. 安装 2.1 安装JDK环境 2.1.1 下载JDK软件 2.1.2 登陆Linux系统&#xff0c;切换到root用户 2.1.3 通过FinalShell&#xff0c;上传下载好的JDK安装包 2.1.4 创建文件夹&#xff0c;用来部署JDK&#xff0c;将JDK和Tomcat都安装部署到&#…

QGIS文章四——对遥感影像进行土地类型分类

关于土地类型分类&#xff0c;按照性质、用途、利用现状有不同的分类标准。 一、按照国家土地性质分类标准&#xff0c;一般分五类:商业用地、综合用地、住宅用地、工业用地和其他用地。 二、按照用途进行土地分类&#xff1a;可以分为农用地、建设用地和未利用土地&#xff0c…

vue、vuex状态管理、vuex的核心概念state状态

每一个 Vuex 应用的核心就是 store&#xff08;仓库&#xff09;。“store”基本上就是一个容器&#xff0c;它包含着你的应用中大部分的状态 (state)。Vuex 和单纯的全局对象有以下两点不同&#xff1a; Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候&…