Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

factory design pattern typescript

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()}`)
Comment

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');
Comment

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);
Comment

PREVIOUS NEXT
Code Example
Typescript :: ERROR Error: mat-form-field must contain a MatFormFieldControl. 
Typescript :: Electron WebContents context-menu 
Typescript :: Request exceeded the limit of 10 internal redirects due to probable configuration error 
Typescript :: delete array typescript 
Typescript :: typescript json to interface 
Typescript :: open ports for remote access on linux 
Typescript :: react typescript append to array 
Typescript :: nestjs graphql schema description 
Typescript :: how to show code conflicts in git 
Typescript :: angular type of string 
Typescript :: Comparison method violates its general contract! 
Typescript :: typescript to c# converter 
Typescript :: Angular Compiler Options to enable AOT compilation 
Typescript :: path represents file or directory java 
Typescript :: package minted missing pygments output 
Typescript :: welsh cup electrodes have 
Typescript :: excel separate input cell contents by space 
Typescript :: how to call an action from another action slice in redux 
Typescript :: typescript -g doesnst read tsconfog 
Typescript :: apache poi get all worksheets from file input stream 
Typescript :: sum the digits in c 
Typescript :: add padding between elements of lazyrow jetpack compose 
Typescript :: mergensherts meaning 
Typescript :: Correct this attempt to modify "product" or use "let" in its declaration. [+1 location] 
Typescript :: angular child routes not working 
Typescript :: how to find matching brackets in eclipse 
Typescript :: Named types just give a name to a type 
Typescript :: omgsmhwthwhyomgnvm 
Typescript :: optional or required depending on param is true react typescript 
Typescript :: typescript get a number param 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =