“Wzór projektu fabrycznego maszynopism” Kod odpowiedzi

Wzór projektu fabrycznego maszynopism

abstract class Ecomerce {
   abstract products(): Record<string, any>[]
}

class Tokopedia extends Ecomerce {
  products(): Record<string, any>[] {
     return [
      {name: 'PS4', price: 5000000},
      {name: 'PSV', price: 3500000},
      {name: 'Gameboy', price: 350000}
     ]
  }
}

class Bukalapak extends Ecomerce {
  products(): Record<string, any>[] {
     return [
      {name: 'PS4', price: 4500000},
      {name: 'PSV', price: 2500000},
      {name: 'Gameboy', price: 250000}
     ]
  }
}

class Shopee extends Ecomerce {
  products(): Record<string, any>[] {
     return [
      {name: 'PS4', price: 3500000},
      {name: 'PSV', price: 1500000},
      {name: 'Gameboy', price: 150000}
     ]
  }
}

// delacation factory metode here

interface EcomerceFactory {
   getEcomerce(name: string): Tokopedia | Bukalapak | Shopee
}

class EcomerceFactory implements EcomerceFactory {
  getEcomerce(name: string): Tokopedia | Bukalapak | Shopee {
      if(name.toLowerCase() == 'tokopedia') {
         return new Tokopedia()
      } else if(name.toLowerCase() == 'bukalapak') {
         return new Bukalapak()
      } else if(name.toLowerCase() == 'shopee') {
         return new Shopee()
      }
  }
}

const res = new EcomerceFactory()
const tokopedia = res.getEcomerce('tokopedia')
const bukalapak = res.getEcomerce('bukalapak')
const shopee = res.getEcomerce('shopee')

console.log(`Tokopedia: ${tokopedia.products()}`)
console.log(`Bukalapak: ${bukalapak.products()}`)
console.log(`Shopee: ${shopee.products()}`)
Restu Wahyu Saputra

Wzór fabryczny TS

class VehicleFactory {
  public createVehicle(type: string): Vehicle {
    switch (type) {
      case 'car':
        return new Car();
      case 'truck':
        return new Truck();
      default:
        throw new Error(`Vehicle of type ${type} not found`);
    }
  }
}

const factory = new VehicleFactory();
const car = factory.createVehicle('car');
const truck = factory.createVehicle('truck');
Puzzled Puffin

Wzór fabryczny TS

/*
The Observer pattern is a design pattern lets you define a subscription 
mechanism to notify multiple objects and it's used in the event driven 
programming paradigm.
*/

class Subject {
  private observers: Observer[] = [];

  public subscribe(observer: Observer) {
    this.observers.push(observer);
  }

  public unsubscribe(observer: Observer) {
    const index = this.observers.indexOf(observer);
    this.observers.splice(index, 1);
  }

  public notify(data: any) {
    this.observers.forEach(observer => observer.update(data));
  }
}

class Observer {
  public update(data: any) {
    console.log(data);
  }
}


const subject = new Subject();
const observer = new Observer();
subject.subscribe(observer);
subject.notify('Hello World');

subject.unsubscribe(observer);
Puzzled Puffin

Odpowiedzi podobne do “Wzór projektu fabrycznego maszynopism”

Pytania podobne do “Wzór projektu fabrycznego maszynopism”

Więcej pokrewnych odpowiedzi na “Wzór projektu fabrycznego maszynopism” w TypeScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu