typescript: model

/**
 * TypeScript 实体类 Model
 * 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;
    }*/

    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;
    }

}
  
/**
 * 继承
 */
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()
    .id(1)
    .setSeats("")
    .setEngine("")
    .build();
*/


interface Builder {
    setSeats(seats: number): this;
    setEngine(engine: string): this;

  }
  


export class CarBuilder implements Builder {
    private car: Car;
  
    constructor() {
      this.car = new Car();
    }
  
    public setSeats(seats: number): this {
      //this.car.setSeats(seats);
    
      this.car.setSeats(seats);
      return this;
    }
  
    public setEngine(engine: string): this {
      this.car.setEngine(engine);
      return this;
    }
  
    public getResult(): Car {
      return this.car;
    }
  }
  
  class DuDirector {

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

/* */
class MotorcycleBuilder implements Builder {

    private motorcycle: Motorcycle;
  
    constructor() {
      this.motorcycle = new Motorcycle();
    }
  
    public setSeats(seats: number): this {
      this.motorcycle.setSeats(seats);
      return this;
    }
  
    public setEngine(engine: string): this {
      this.motorcycle.setEngine(engine);
      return this;
    }
  
    public getResult(): Motorcycle {
      return this.motorcycle;
    }
  }
 
/**/
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()
  .setSeats(2)
  .setEngine("V-4")
  .getResult();

你可能感兴趣的:(typescript,typescript,javascript,前端)