“TS Factory Pattern” Ответ

Типовая модели проектирования заводского дизайна

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

TS Factory Pattern

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

TS Factory Pattern

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

Ответы похожие на “TS Factory Pattern”

Вопросы похожие на “TS Factory Pattern”

Больше похожих ответов на “TS Factory Pattern” по TypeScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования