250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ปดํฌ์ง€์…˜
  • ์œ„์ž„
  • ใ…ฃใ„ท
  • MySQL

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] Abstract ํด๋ž˜์Šค์™€ abstract method
TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] Abstract ํด๋ž˜์Šค์™€ abstract method

2022. 4. 23. 23:02
728x90

์ƒ์† ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•  ๋•Œ, ์œ„ ํด๋ž˜์Šค์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์ด ์žˆ๊ณ  ์–ด๋–ค ํŠน์ • ๊ธฐ๋Šฅ๋งŒ ์ž์‹ ํด๋ž˜์Šค์—์„œ ๊ทธ ์–‘ํƒœ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค๋ฉด

Abstract ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ๊ฒƒ์„ ๊ณ ๋ คํ•ด๋ณด์ž.

abstract ํด๋ž˜์Šค์™€ abstract ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์‹ค์ˆ˜๋ฅผ ์‚ฌ์ „์— ์˜ˆ๋ฐฉํ•ด์ฃผ๋Š” ๋งค๋ ฅ์ ์ธ ๊ธฐ๋Šฅ๋“ค์„ ๊ฐ–์ถ”๊ณ  ์žˆ๋‹ค.

 

abstract ํด๋ž˜์Šค์™€ abstract ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ–ˆ์„ ๋•Œ์˜ ์ด์ ์€,

๋ฌด์ž‘์ • ์˜ค๋ฒ„๋ผ์ด๋”ฉ + super()ํ˜ธ์ถœํ•˜๊ธฐ ํ™œ๋™์„ ์–ด๋А ์ •๋„ ๋Œ€์ฒดํ•˜๋ฉด์„œ

์ž์‹ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจํด๋ž˜์Šค์™€ ์ฐจ๋ณ„ํ™”๋˜๋Š” ์ถ”์ƒํ™” ํ•จ์ˆ˜๋“ค์„ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•  ๊ฒƒ์„ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฒ ์ด์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk?: boolean;
  hasSalt?: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์ผ๋ฐ˜ ์šฐ์œ  ๊ฑฐํ’ˆ๊ธฐ
class CheapMilkSteamer {
  //(1)์šฐ์œ  ๊ฑฐํ’ˆ์„ ๋งŒ๋“œ๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private steamMilk(): void {
    console.log("๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ");
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ๋งŒ๋“ค์–ด์ง„ ์šฐ์œ ๊ฑฐํ’ˆ(1)์„ ๋ถ€์–ด์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addMilkform(cup: CoffeeCup): CoffeeCup {
    this.steamMilk();
    return {
      ...cup,
      hasMilk: true,
    };
  }
}

//์†Œ๊ธˆ๋ฏน์„œ
class SaltMixer {
  //(1)์†Œ๊ธˆ์„ ์ง‘๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private getSalt() {
    console.log("์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค");
    return true;
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ์ง‘์€ ์†Œ๊ธˆ(1)์„ ๋ฟŒ๋ ค์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addSalt(cup: CoffeeCup): CoffeeCup {
    const salt = this.getSalt();
    return {
      ...cup,
      hasSalt: salt,
    };
  }
}
// CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” CoffeMachine ํด๋ž˜์Šค์ด๋‹ค.////////////////////////////
class CoffeMachine implements CoffeeMaker {
  protected static ONE_SHOT_CAPSULE: number = 3;
  protected static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  protected useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }

  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    const saltAdded = this.milkFrother.addMilkform(coffee);
    return this.milkFrother.addMilkform(saltAdded);
  }
}

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot);
    return this.saltTopping.addSalt(coffee);
  }
}

const machines: CoffeeMaker[] = [
  new CoffeMachine(25, 25),
  new CafeLatteMachine(15, 25, new CheapMilkSteamer()),
  new saltCoffeeMachine(5, 10, new SaltMixer()),
];

machines.forEach((machine) => {
  console.log("------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------");
  console.log(machine.makeCoffee(1));
});

<์ถ”์ƒํด๋ž˜์Šค ์ž‘์—… ์ „ ์ถœ๋ ฅ ๊ฒฐ๊ณผ>

------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 25
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„22
๋ฌผ์“ฐ๊ธฐ ์ „ 25
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 15
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 1, hasMilk: false }
------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 15
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„12
๋ฌผ์“ฐ๊ธฐ ์ „ 25
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 15
๋ฌผ ๋“์ด๋Š” ์ค‘...
๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ
๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ
{ orderShot: 1, hasMilk: true }
------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 5
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„2
๋ฌผ์“ฐ๊ธฐ ์ „ 10
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 0
๋ฌผ ๋“์ด๋Š” ์ค‘...
์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค

 

 

 

 

๋ถ€๋ชจํด๋ž˜์Šค

class CoffeMachine implements CoffeeMaker {
  protected static ONE_SHOT_CAPSULE: number = 3;
  protected static ONE_CUP_WATER: number = 10;

  static makeMachine(beans: number, water: number): CoffeMachine {
    return new CoffeMachine(beans, water);
  }

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  private extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  protected useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

 

์ž์‹ํด๋ž˜์Šค 1,2

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }

  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    const saltAdded = this.milkFrother.addMilkform(coffee);
    return this.milkFrother.addMilkform(saltAdded);
  }
}

 

์ง€๊ธˆ์€ const coffee = super.makeCoffee(orderShot); ์‹์œผ๋กœ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์ง€๋งŒ

์‹ค์ˆ˜๋กœ ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด?

makeCoffee๋ฅผ ์œ„ํ•œ ํ•ต์‹ฌ ๋‹จ๊ณ„๋ฅผ ๋†“์น  ์ˆ˜๊ฐ€ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ด๋Ÿฐ ์‹ค์ˆ˜๋“ค์„ ๋ฐฉ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•ˆ์ „ํ•˜๊ฒŒ Abstract class๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot);
    return this.saltTopping.addSalt(coffee);
  }
}

 

 

abstract class 

// CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” CoffeMachine ํด๋ž˜์Šค์ด๋‹ค.////////////////////////////
abstract class CoffeMachine implements CoffeeMaker {

 

์ด๊ฒƒ์€ ์ถ”์ƒ์ ์ธ ํด๋ž˜์Šค์ด๋‹ค. ์ด ์ž์ฒด๋กœ๋Š” ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

์ถ”์ƒ์ ์ธ ๋ถ€๋ชจํด๋ž˜์Šค๋กœ์„œ ๋ณธ์งˆ์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ •์˜ํ•ด๋†“๊ณ ,

์ž์‹ํด๋ž˜์Šค๋งˆ๋‹ค ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋Š” ํ–‰๋™์ด ์žˆ๋‹ค๋ฉด ๊ทธ ํ–‰๋™์— ํ•ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜ ์•ž์— abstract ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ์ž.

  abstract extract(shots: number): CoffeeCup {
    return {
      orderShot: shots,
      hasMilk: false,
    };
  }

ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ์ž ๋งˆ์ž ๋นจ๊ฐ„์ค„์ด ๊ทธ์–ด์ง„๋‹ค.

์œ„์—์„œ ๋นจ๊ฐ„ ๋ฐ‘์ค„์ด ๊ทธ์–ด์ง„ abstrct extract ํ•จ์ˆ˜๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ  ์ž์‹ ํด๋ž˜์Šค๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

์ ‘๊ทผ์ง€์ •์ œํ•œ์ž๋„ private๊ฐ€ ์•„๋‹Œ protected๋กœ ๋‹ฌ์•„์ฃผ์–ด์•ผ ํ•˜๊ณ ,

์ถ”์ƒ์ ์ธ ๋ฉ”์†Œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌํ˜„ ์‚ฌํ•ญ์€ ์ž‘์„ฑํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

 

๋ฐ”๋กœ ์ด๋ ‡๊ฒŒ

  protected abstract extract(shots: number): CoffeeCup;

extract ํ•จ์ˆ˜๋Š” ์ถ”์ƒํด๋ž˜์Šค์˜ ์ถ”์ƒํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ƒํ•จ์ˆ˜ ๊ตฌํ˜„ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

๊ธฐ์กด ์ž์‹ ํด๋ž˜์Šค์—์„œ makeCoffeeํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค.

๋Œ€์‹ ์— extract ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค.

 

<๋ณ€๊ฒฝ ์ „>

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }

  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot); //๊ธฐ๋ณธ์ ์ธ ์บก์А ์†Œ๋น„, ๋ฌผ ์†Œ๋น„, ๋ฌผ ๊ฐ€์—ด ๋‹ค ๋๋‚˜๊ณ  ์™„์„ฑ๋œ ์ปคํ”ผ๊ฐ€ ๋ฐ˜ํ™˜
    const saltAdded = this.milkFrother.addMilkform(coffee);
    return this.milkFrother.addMilkform(saltAdded);
  }
}

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  makeCoffee(orderShot: number): CoffeeCup {
    const coffee = super.makeCoffee(orderShot);
    return this.saltTopping.addSalt(coffee);
  }
}

<๋ณ€๊ฒฝ ํ›„>

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.milkFrother.addMilkform(blackCoffee);
  }
}

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.saltTopping.addSalt(blackCoffee);
  }
}

 

<์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰๋œ ์ „์ฒด ์ฝ”๋“œ>

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk?: boolean;
  hasSalt?: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์ผ๋ฐ˜ ์šฐ์œ  ๊ฑฐํ’ˆ๊ธฐ
class CheapMilkSteamer {
  //(1)์šฐ์œ  ๊ฑฐํ’ˆ์„ ๋งŒ๋“œ๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private steamMilk(): void {
    console.log("๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ");
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ๋งŒ๋“ค์–ด์ง„ ์šฐ์œ ๊ฑฐํ’ˆ(1)์„ ๋ถ€์–ด์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addMilkform(cup: CoffeeCup): CoffeeCup {
    this.steamMilk();
    return {
      ...cup,
      hasMilk: true,
    };
  }
}

//์†Œ๊ธˆ๋ฏน์„œ
class SaltMixer {
  //(1)์†Œ๊ธˆ์„ ์ง‘๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private getSalt() {
    console.log("์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค");
    return true;
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ์ง‘์€ ์†Œ๊ธˆ(1)์„ ๋ฟŒ๋ ค์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addSalt(cup: CoffeeCup): CoffeeCup {
    const salt = this.getSalt();
    return {
      ...cup,
      hasSalt: salt,
    };
  }
}
// CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” CoffeMachine ํด๋ž˜์Šค์ด๋‹ค.////////////////////////////
abstract class CoffeMachine implements CoffeeMaker {
  protected static ONE_SHOT_CAPSULE: number = 3;
  protected static ONE_CUP_WATER: number = 10;

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  protected abstract extract(shots: number): CoffeeCup;

  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  protected useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.milkFrother.addMilkform(blackCoffee);
  }
}

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.saltTopping.addSalt(blackCoffee);
  }
}

const machines: CoffeeMaker[] = [
  new CafeLatteMachine(15, 25, new CheapMilkSteamer()),
  new saltCoffeeMachine(5, 10, new SaltMixer()),
];

machines.forEach((machine) => {
  console.log("------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------");
  console.log(machine.makeCoffee(1));
});

 

<์ถœ๋ ฅ ๊ฒฐ๊ณผ> ์ž˜ ๋™์ž‘ํ•œ๋‹ค.

------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 15
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„12
๋ฌผ์“ฐ๊ธฐ ์ „ 25
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 15
๋ฌผ ๋“์ด๋Š” ์ค‘...
๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ
{ orderShot: 1, hasMilk: true }
------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 5
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„2
๋ฌผ์“ฐ๊ธฐ ์ „ 10
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 0
๋ฌผ ๋“์ด๋Š” ์ค‘...
์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค
{ orderShot: 1, hasSalt: true }

 

 

์ •๋ฆฌํ•˜์ž๋ฉด, ์ถ”์ƒํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ๋“ค์€ ๋‹ค ์ถ”์ƒํด๋ž˜์Šค ์•ˆ์—์„œ ๊ตฌํ˜„ํ•ด์ฃผ๊ณ ,

๋‹จ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค ๋ณ„๋กœ ๋‹ฌ๋ผ์ ธ์•ผ ํ•˜๋Š” ๊ธฐ๋Šฅ(๋ฉ”์†Œ๋“œ)๊ฐ€ ์žˆ๋‹ค๋ฉด,

๊ทธ ๋ถ€๋ถ„๋งŒ abstract method๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.(๊ตฌํ˜„x)

 

์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํ•จ์ˆ˜์˜ ๊ทœ๊ฒฉ์„ ์ •์˜ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ

์ถ”์ƒํด๋ž˜์Šค์˜ abstract method๋Š” ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ ๋ฌด์—‡์ด๊ณ  ์–ด๋–ค ๊ฒƒ๋“ค์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์•ผํ•˜๋Š”์ง€ ์–ด๋–ค ๊ฒƒ์„ ๋ฆฌํ„ดํ•ด์•ผํ•˜๋Š”์ง€๋งŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณตํ†ต์ ์œผ๋กœ ์“ฐ์ด๋Š” ๊ธฐ๋Šฅ๋“ค์€ ๋‚ด๋ถ€์—์„œ๋งŒ ํ•„์š”ํ•œ ๊ฒƒ์€ private๋กœ,

์™ธ๋ถ€์—์„œ ํ˜ธ์ถœ๋˜์•ผ ํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์€ public์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ ,

๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋งˆ๋‹ค ๋‹ฌ๋ผ์ ธ์•ผ ํ•˜๋Š” abstract method๋งŒ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

(์ถ”์ƒํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  abstract method๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๋Š”๋‹ค)

 

 

์ด๋ ‡๊ฒŒ ์ถ”์ƒํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ธˆ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์˜๋„ํ•œ ๋Œ€๋กœ ๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ๋“ค์„ ์ˆ˜ํ–‰ํ•˜๊ณ 

๋‹ฌ๋ผ์ ธ์•ผ ํ•˜๋Š” ๊ฒƒ๋งŒ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค์—๊ฒŒ ๊ทธ๊ฒƒ์„ ๊ผญ ๊ตฌํ˜„ํ•  ๊ฒƒ์„ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

<์ตœ์ข… ์ฝ”๋“œ>

//   //์ปคํ”ผ๋จธ์‹  ๋งŒ๋“ค๊ธฐ
type CoffeeCup = {
  orderShot: number;
  hasMilk?: boolean;
  hasSalt?: boolean;
};

interface CoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
}

//์ผ๋ฐ˜ ์šฐ์œ  ๊ฑฐํ’ˆ๊ธฐ
class CheapMilkSteamer {
  //(1)์šฐ์œ  ๊ฑฐํ’ˆ์„ ๋งŒ๋“œ๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private steamMilk(): void {
    console.log("๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ");
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ๋งŒ๋“ค์–ด์ง„ ์šฐ์œ ๊ฑฐํ’ˆ(1)์„ ๋ถ€์–ด์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addMilkform(cup: CoffeeCup): CoffeeCup {
    this.steamMilk();
    return {
      ...cup,
      hasMilk: true,
    };
  }
}

//์†Œ๊ธˆ๋ฏน์„œ
class SaltMixer {
  //(1)์†Œ๊ธˆ์„ ์ง‘๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜
  private getSalt() {
    console.log("์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค");
    return true;
  }
  //(2)๋งŒ๋“ค์–ด์ง„ ์ปคํ”ผ์ปต์„ ๋ฐ›์•„์„œ, ์ง‘์€ ์†Œ๊ธˆ(1)์„ ๋ฟŒ๋ ค์„œ ์ด ์ปคํ”ผ์ปต์„ ๋‹ค์‹œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
  addSalt(cup: CoffeeCup): CoffeeCup {
    const salt = this.getSalt();
    return {
      ...cup,
      hasSalt: salt,
    };
  }
}
// CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” CoffeMachine ํด๋ž˜์Šค์ด๋‹ค.////////////////////////////
abstract class CoffeMachine implements CoffeeMaker {
  protected static ONE_SHOT_CAPSULE: number = 3;
  protected static ONE_CUP_WATER: number = 10;

  public constructor(
    protected capsule: number = 3,
    protected water: number = 10
  ) {}

  makeCoffee(orderShot: number): CoffeeCup {
    this.checkMaterials(orderShot);
    console.log("์บก์А์“ฐ๊ธฐ ์ „ " + this.capsule);
    this.useCapsule(orderShot);
    console.log("๋ฌผ์“ฐ๊ธฐ ์ „ " + this.water);
    this.useWater();
    this.heatWater();
    return this.extract(orderShot);
  }

  protected checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * CoffeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < CoffeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }
  clean(): void {
    console.log("์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!");
  }

  protected abstract extract(shots: number): CoffeeCup;

  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

  private useCapsule(shot: number) {
    console.log("์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.capsule -= shot * CoffeMachine.ONE_SHOT_CAPSULE;
    console.log("์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„" + this.capsule);
  }

  protected useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= CoffeMachine.ONE_CUP_WATER;
    console.log("๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ " + this.water);
  }

  addCapsule(capsule: number) {
    this.capsule += capsule;
  }

  fulfillWater(water: number) {
    this.water += water;
  }

  get Water(): number {
    return this.water;
  }

  get Capsule(): number {
    return this.capsule;
  }
}

// CoffeMachine ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CafeLatteMachine ํด๋ž˜์Šค์ด๋‹ค.
// tslint:disable-next-line: max-classes-per-file
class CafeLatteMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private milkFrother: CheapMilkSteamer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.milkFrother.addMilkform(blackCoffee);
  }
}

// CoffeMachine ํด๋ž˜์Šค ๋ฅผ ์ƒ์†๋ฐ›์€ saltCoffeeMachine ํด๋ž˜์Šค์ด๋‹ค.
class saltCoffeeMachine extends CoffeMachine {
  public constructor(
    protected capsule: number = 3,
    protected water: number = 10,
    private saltTopping: SaltMixer
  ) {
    super(capsule, water);
  }
  protected extract(shots: number): CoffeeCup {
    const blackCoffee = {
      orderShot: shots,
    };
    return this.saltTopping.addSalt(blackCoffee);
  }
}

const machines: CoffeeMaker[] = [
  new CafeLatteMachine(15, 25, new CheapMilkSteamer()),
  new saltCoffeeMachine(5, 10, new SaltMixer()),
];

machines.forEach((machine) => {
  console.log("------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------");
  console.log(machine.makeCoffee(1));
});

 

<์ถœ๋ ฅ ๊ฒฐ๊ณผ>

------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 15
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„12
๋ฌผ์“ฐ๊ธฐ ์ „ 25
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 15
๋ฌผ ๋“์ด๋Š” ์ค‘...
๋ชฝ๊ธ€๋ชฝ๊ธ€ ํฌ์‚ญํฌ์‚ญํ•œ ์šฐ์œ ํผ
{ orderShot: 1, hasMilk: true }
------------์ปคํ”ผ๋จธ์‹  ๊ฐ€๋™~!-- ----------
์บก์А์“ฐ๊ธฐ ์ „ 5
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„2
๋ฌผ์“ฐ๊ธฐ ์ „ 10
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 0
๋ฌผ ๋“์ด๋Š” ์ค‘...
์†Œ๊ธˆ์„ ํ•œ๊ผฌ์ง‘ ์ง‘์—ˆ๋‹ค
{ orderShot: 1, hasSalt: true }
728x90

'TypeScript > ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ค๋ฆญ  (0) 2022.04.25
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ stack ๊ตฌํ˜„ํ•˜๊ธฐ  (0) 2022.04.25
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] composition ์žฅ์ ์„ ๊ทน๋Œ€ํ™”ํ•˜๋Š” ๊ฐ•๋ ฅํ•œ interface  (0) 2022.04.23
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๊ฝƒ composition(๋ถ€์ œ: ์ƒ์†์˜ ๋ฌธ์ œ์ )  (0) 2022.04.22
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๋‹คํ˜•์„ฑ(polymorphism)์ด ๋ญ๊ฐ€ ์ข‹์€ ๊ฒƒ์ผ๊นŒ?  (0) 2022.04.22
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ค๋ฆญ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ stack ๊ตฌํ˜„ํ•˜๊ธฐ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] composition ์žฅ์ ์„ ๊ทน๋Œ€ํ™”ํ•˜๋Š” ๊ฐ•๋ ฅํ•œ interface
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๊ฝƒ composition(๋ถ€์ œ: ์ƒ์†์˜ ๋ฌธ์ œ์ )
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”