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 :: boto3 Requests specifying Server Side Encryption with AWS KMS managed keys require AWS Signature Version 4 
Typescript :: ether.js 
Typescript :: conditional styled components with media query 
Typescript :: serverless.ps1 cannot be loaded because running scripts is disabled on this system. 
Typescript :: typeorm find with limit 
Typescript :: typescript type definition 
Typescript :: filename requests python 
Typescript :: angular rxjs 
Typescript :: typescript convert string to character array 
Typescript :: array in typescript 
Typescript :: typescript make object optional 
Typescript :: validation minlength angular 
Typescript :: can ts object be strongly typed? 
Typescript :: global declaration css ts 
Typescript :: check type of object typescript 
Typescript :: typescript array 
Typescript :: text size in plots in r 
Typescript :: Two sets of parentheses after function call 
Typescript :: how to parameterize test cases 
Typescript :: Start Angular App In Localhost 
Typescript :: react fc typescript 
Typescript :: htmlspecialchars() expects parameter 1 to be string array given in laravel blade 
Typescript :: typescript deep partial 
Typescript :: paths typescript 
Typescript :: paper menu rendered but not clickable 
Typescript :: obsidian write file 
Typescript :: derivative dots overleaf 
Typescript :: uTorrent Default Download Folder - Linux 
Typescript :: Find more than one child node with `children` in ResizeObserver. Please use ResizeObserver.Collection instead in React/ant design [antd] 
Typescript :: phaser load progress 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =