samedi 30 avril 2022

Factory method design pattern Typescript

im just learning design pattern and i do not know whats the benefit of abstraction in below code: i rewrite the code and still work the way we want.

you can see the original code from here

class Creator {
  public factoryMethod() {}

  public someOperation(): string {
    const product = this.factoryMethod();
    return `Creator: The same creator's code has just worked with ${product}`;
  }
}

class ConcreteCreator1 extends Creator {
  public factoryMethod(): string {
    return '{Result of the ConcreteProduct1}';
  }
}

class ConcreteCreator2 extends Creator {
  public factoryMethod(): string {
    return '{Result of the ConcreteProduct2}';
  }
}

function clientCode(creator: Creator) {
  console.log(
    "Client: I'm not aware of the creator's class, but it still works."
  );
  console.log(creator.someOperation());
}

console.log('App: Launched with the ConcreteCreator1.');
clientCode(new ConcreteCreator1());
console.log('');

console.log('App: Launched with the ConcreteCreator2.');
clientCode(new ConcreteCreator2());

Aucun commentaire:

Enregistrer un commentaire