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๐Ÿ“’

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ถ”์ƒํ™”๋ฅผ ์ด์šฉํ•ด์„œ ํด๋ž˜์Šค ๊ฐœ์„ ํ•˜๊ธฐ
TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ถ”์ƒํ™”๋ฅผ ์ด์šฉํ•ด์„œ ํด๋ž˜์Šค ๊ฐœ์„ ํ•˜๊ธฐ

2022. 4. 20. 18:30
728x90

 

- ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค๋ฅผ ๋ฐ”๋ผ ๋ดค์„ ๋•Œ,  ์‚ฌ์šฉํ•ด์•ผ ํ•  ํ•จ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋œจ๋ฉด ์‚ฌ์šฉ์ž๋Š” ๋ฌด์—‡์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ์ง€ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋…์ด ๋ฐ”๋กœ '์ถ”์ƒํ™”'์ด๋‹ค.

 

์ •๋ง ํ•„์š”ํ•œ api๋“ค๋งŒ ๋…ธ์ถœํ•จ์œผ๋กœ์จ ํด๋ž˜์Šค๋ฅผ ๋ณด๋‹ค ์‚ฌ์šฉํ•˜๊ฒŒ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.  

 

 

๋‹ค์Œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ปคํ”ผ๋จธ์‹  ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. 

 

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

class coffeeMachine {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * coffeeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < coffeeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }

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

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

  useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= coffeeMachine.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;
  }
}

console.log("/* ์ฒซ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let normalMachine = new coffeeMachine(10, 10);
console.log(normalMachine.Water); //10
console.log(normalMachine.Capsule); //10
let order1 = normalMachine.makeCoffee(2);
console.log(order1); //{ orderShot: 2 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //0
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //4

console.log("/* ๋‘๋ฒˆ์งธ ์ฃผ๋ฌธ */");
normalMachine.fulfillWater(100);
let order2 = normalMachine.makeCoffee(1);
console.log(order2); //{ orderShot: 1 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //90
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //1

console.log("/* ์„ธ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let order3 = normalMachine.makeCoffee(3);
console.log(order3); //Error: ์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : 1

๊ต‰์žฅํžˆ ๋งŽ์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค... ์ด๋ ‡๊ฒŒ ๋งŽ์•„ ๋ฒ„๋ฆฌ๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์จ์•ผํ•  ์ˆœ์„œ๋ฅผ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿด ๋•Œ ์ถ”์ƒํ™”๊ฐ€ ๋น›์„ ๋ฐœํ•˜๊ฒŒ ๋œ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๊ณ  ์‹ฌํ”Œํ•˜๊ฒŒ ๋งŒ๋“ฌ์œผ๋กœ์จ ์‚ฌ์šฉ์ž๊ฐ€ ํฐ ๊ณ ๋ฏผ์„ ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

-1.  ์ผ๋‹จ ์™ธ๋ถ€์—์„œ ์“ธ ํ•„์š”๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋“ค์— ํ‚ค์›Œ๋“œ private ๋‹ฌ์•„์ค€๋‹ค : ์™ธ๋ถ€์— ์ •๋ง ํ•„์š”ํ•œ ํ•จ์ˆ˜๋งŒ ๋ณด์—ฌ์ฃผ์ž๋Š” ๊ด€์  (์ด๊ฑด์‚ฌ์‹ค ์บก์Аํ™” ๊ฐœ๋…)

-2.  ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•œ ์ถ”์ƒํ™”

 

-1.

์ผ๋‹จ ์™ธ๋ถ€์—์„œ ๋ถ€๋ฅผ ํ•„์š”๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋“ค์— private๋ฅผ ๋‹ฌ์•„์ฃผ๋‹ˆ

type CoffeeCup = {
  orderShot: number;
};

class coffeeMachine {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * coffeeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < coffeeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }

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

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

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= coffeeMachine.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;
  }
}

console.log("/* ์ฒซ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let normalMachine = new coffeeMachine(10, 10);
console.log(normalMachine.Water); //10
console.log(normalMachine.Capsule); //10
let order1 = normalMachine.makeCoffee(2);
console.log(order1); //{ orderShot: 2 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //0
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //4

console.log("/* ๋‘๋ฒˆ์งธ ์ฃผ๋ฌธ */");
normalMachine.fulfillWater(100);
let order2 = normalMachine.makeCoffee(1);
console.log(order2); //{ orderShot: 1 }
console.log("๋ฌผ ์ƒํƒœ" + normalMachine.Water); //90
console.log("์บก์А ์ƒํƒœ" + normalMachine.Capsule); //1

console.log("/* ์„ธ๋ฒˆ์งธ ์ฃผ๋ฌธ */");
let order3 = normalMachine.makeCoffee(3);
console.log(order3); //Error: ์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : 1

์™€! ํ™•์‹คํžˆ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค. ์ด๋ ‡๊ฒŒ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ํ˜ธ์ถœํ•  ๋™์ž‘(ํ•จ์ˆ˜)๋“ค์€ private ๋‹ฌ์•„์ฃผ์–ด, ์™ธ๋ถ€์— ๋А๋‚„ ํ˜ผ๋ž€์„ ์ค„์—ฌ์ฃผ์ž! ๋ฌผ๊ณผ ์ปคํ”ผ์บก์А ๊ฐฏ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ํ”„๋กœํผํ‹ฐ์™€ ์บก์А๊ณผ ๋ฌผ ์ถ”๊ฐ€ ๋ฐ ์ปคํ”ผ ๋งŒ๋“ค๊ธฐ ํ•จ์ˆ˜๋งŒ ๋…ธ์ถœ๋˜์–ด์„œ ๋ฒŒ์จ๋ถ€ํ„ฐ ์‹œ์ค‘์— ํŒŒ๋Š” ์ปคํ”ผ๋จธ์‹  ๊ฐ™๋‹ค~ ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์‚ฌ๋žŒ์ด ๋‚˜ ํ˜น์€ ๋™๋ฃŒ๊ฐ€ ๋  ํ„ฐ์ธ๋ฐ ์ ‘๊ทผ์ด ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ณ  ์‰ฌ์›Œ์กŒ๋‹ค!

-2. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•œ ์ถ”์ƒํ™”

 

์ธํ„ฐํŽ˜์ด์Šค ์™ˆ "๋‚˜๋ž‘ ํ–‰๋™ํ•˜๋ ค๋ฉด ๋‚˜๋Š” ์ด๋Ÿฌ์ด๋Ÿฌ์ด๋Ÿฌํ•œ ๊ทœ์•ฝ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด. ์ด๋Ÿฐ ํ–‰๋™๋“ค์„ ํ•  ์ˆ˜ ์žˆ์–ด~"๋ผ๊ณ  ๋ช…์‹œํ•ด๋†“๋Š” ๊ณ„์•ฝ์„œ์ด์ž ์•ฝ์†!

 

interface CoffeeMaker{
//์ƒ์„ธํ•˜๊ฒŒ ๋ช…์„ธ์„œ ์ž‘์„ฑ, ๋ช‡๋ฒˆ์˜ ์ƒท์„ ๋ฐ›์„ ๊ฑด์ง€, ์–ด๋–ค ํƒ€์ž…์„ ๋ฆฌํ„ดํ• ๊ฑด์ง€
  makeCoffee(shots:number):CoffeeCup;
}

์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด CoffeeCup์„ ๋ฆฌํ„ดํ•˜๋Š” makeCoffee() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅด๊ธธ ์›ํ•˜๋Š” ํด๋ž˜์Šค์— ํ•„์š”ํ•œ ํ‚ค์›Œ๋“œ๋Š”

implements ์ด๋‹ค. 
interface CoffeeMaker{
  makeCoffee(shots:number):CoffeeCup;
}

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

extends, implements ์ฐจ์ด : ์ž๋ฐ” ์ž๋ฃŒ์ง€๋งŒ ์ •๋ง ์ •๋ฆฌ๋ฅผ ์ž˜ ํ•ด๋†“์œผ์…”์„œ ๋ณธ ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ•˜๋ฉด์„œ ๋งŽ์ด ์ฐธ๊ณ ํ•˜์˜€๋‹ค. ๋งํฌ๋ฅผ ๋‹ฌ์•„๋‘”๋‹ค.

์ฐธ๊ณ ๋ธ”๋กœ๊ทธ: https://velog.io/@hkoo9329/%EC%9E%90%EB%B0%94-extends-implements-%EC%B0%A8%EC%9D%B4

 

์ž๋ฐ” extends, implements ์ฐจ์ด

์ƒ์†์ด๋ž€ (Inheritance) ์ƒ์†์„ ๋งํ•˜๊ธฐ ์ „์— ๋จผ์ € OOP๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™๋‹ค.OOP(Object-Oriented Programming, ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ) ์ด๋ž€? OOP์˜ ํŠน์ง•์œผ๋กœ 1. ์ƒ์†๊ณผ ์ธํ„ฐํŽ˜์ด์Šค (๊ณ„์ธต์„ฑ) 2. ๋‹คํ˜•์„ฑ, ์‚ฌ

velog.io

https://velog.io/@hkoo9329/OOPObject-Oriented-Programming-%EA%B0%9D%EC%B2%B4-%EC%A7%80%ED%96%A5-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EC%9D%B4%EB%9E%80

 

OOP(Object-Oriented Programming, ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ) ์ด๋ž€?

OOP๋ž€ ๋ฌด์—‡์ธ๊ฐ€? OOP (Object-Oriented Programming)์ด๋ž€ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ. ์ฆ‰, C์–ธ์–ด๊ฐ™์€ ์ ˆ์ฐจ ์ง€ํ–ฅ์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์•„๋‹Œ ๊ฐ์ฒด์˜ ๊ด€์ ์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. OOP๋Š” ๊ฐ์ฒด๋ฅผ ๊ธฐ์ค€์œผ๋กœ

velog.io


 

 

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์—์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๊ทœ์•ฝ๋œ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

์–ด์จŒ๋“ , ์ด๋ ‡๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”์ƒํ™”๋ฅผ ๊ทน๋Œ€ํ™”ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋ฉด ๊ทธ ์ธ์Šคํ„ด์Šค์—์„œ ํด๋ž˜์Šค ์•ˆ ํผ๋ธ”๋ฆญํ”„๋กœํผํ‹ฐ๋“ค์— ๋‹ค ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ

์ธํ„ฐํŽ˜์ด์Šค๋กœ ํƒ€์ž…์„ ์ œํ•œํ•ด์„œ ๋ฐ›๊ฒŒ ๋˜๋ฉด ์ด ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ •์˜(๊ทœ์•ฝ)๋œ ์•„์ด๋“ค๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

 

 

์ด๋Ÿฐ ์†Œ์Šค์— ๋Œ€ํ•˜์—ฌ

type CoffeeCup = {
  orderShot: number;
};

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

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class coffeeMachine implements CoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  private checkMaterials(orderShot: number) {
    if (this.capsule < orderShot * coffeeMachine.ONE_SHOT_CAPSULE) {
      throw new Error(`์ปคํ”ผ์บก์А ๋ถ€์กฑ!!! ํ˜„์žฌ ๋ณด์œ  ์บก์А : ${this.capsule}`);
    }
    if (this.water < coffeeMachine.ONE_CUP_WATER) {
      throw new Error(`๋ฌผ ๋ถ€์กฑ!!! ๋‚จ์€ ๋ฌผ ${this.water}`);
    }
  }

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

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

  private useWater() {
    console.log("๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.");
    this.water -= coffeeMachine.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;
  }
}

์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ณ ,

const maker: CoffeMachine = CoffeMachine.makeMachine(32, 100);
maker.makeCoffee(2);

๊ณต๊ฐœ ์ƒํƒœ์ธ class api๋“ค์ด ๋ณด์ด๋Š” ์ƒํƒœ

maker2์˜ ํƒ€์ž…์€ CoffeeMaker๋กœ ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

const maker2: CoffeeMaker = CoffeMachine.makeMachine(32, 100);
maker2.makeCoffee(2);

 

์ปคํ”ผ๋ฉ”์ด์ปค๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—๋Š” ๋ฉ”์ดํฌ์ปคํ”ผ๋ผ๋Š” ํ•จ์ˆ˜๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์—(์ฆ‰, ์บก์А, ๋ฌผ, ์บก์А์ฑ„์šฐ๊ธฐ, ๋ฌผ์ฑ„์šฐ๊ธฐ ๋“ฑ์˜ ๊ธฐํƒ€ api ๋“ค์€ ์ปคํ”ผ๋ฉ”์ด์ปค ์ธํ„ฐํŽ˜์ด์Šค์— ์—†๋‹จ ๋ง)

๊ทธ๋ž˜์„œ ์ธํ„ฐํŽ˜์ด์Šค์— ์—†๋Š” ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. ์•„๋ž˜ ์บก์ฒ˜์ฒ˜๋Ÿผ

์ธํ„ฐํŽ˜์ด์Šค์— ๊ทœ์•ฝ๋œ ํ•จ์ˆ˜๋งŒ ๋ณด์ด๋Š” ์ƒํƒœ

๊ทธ๋ž˜์„œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๋ฐœ์ž๊ฐ€ ์–ผ๋งˆ๋งŒํผ์˜ ๊ทœ์•ฝ์„ ์„ธ์šธ ๊ฒƒ์ธ์ง€, (๋ณด์žฅ-ํ—ˆ์šฉ,์˜ ๋А๋‚Œ)๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

 

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด๋ณด์ž.

์ƒ์—…์ ์œผ๋กœ ์ด์šฉ๋˜๋Š” ์ปคํ”ผ๋ฉ”์ด์ปค(์ปคํ”ผ์ „๋ฌธ์ˆ์—์„œ ์‚ฌ์šฉํ•  ๋งŒํ•œ ์ธํ„ฐํŽ˜์ด์Šค) ๋” ๋‹ค์–‘ํ•œ apis๋ฅผ ๋‹ฌ์•„์ฃผ์ž.

interface CommercialCoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
  addCapsule(capsule: number):void;
  fulfillWater(water: number):void;
  clean():void; 
}

 

 

์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์ค‘ ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

class CoffeMachine implements CoffeeMaker,CommercialCoffeeMaker {

๋‹น์—ฐํ•œ ์ถ”์ฒœ(๋ฌผ๋ก  clean ํ•จ์ˆ˜๋Š” ์ ๋‹นํžˆ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ๋‹ค.)

<ํ˜„์žฌ๊นŒ์ง€ ์ง„ํ–‰์ƒํ™ฉ>

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

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

interface CommercialCoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
  addCapsule(capsule: number): void;
  fulfillWater(water: number): void;
  clean(): void;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker, CommercialCoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  private 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,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

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

  private 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;
  }
}

const maker: CoffeMachine = CoffeMachine.makeMachine(32, 100);
maker.makeCoffee(2);

const maker2: CoffeeMaker = CoffeMachine.makeMachine(12, 12);
maker2.makeCoffee(1);

const maker3: CommercialCoffeeMaker = CoffeMachine.makeMachine(30, 60);
maker3.makeCoffee(2);
maker3.clean();

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

์บก์А์“ฐ๊ธฐ ์ „ 32
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„26
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
์บก์А์“ฐ๊ธฐ ์ „ 12
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„9
๋ฌผ์“ฐ๊ธฐ ์ „ 12
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 2
๋ฌผ ๋“์ด๋Š” ์ค‘...
์บก์А์“ฐ๊ธฐ ์ „ 30
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„24
๋ฌผ์“ฐ๊ธฐ ์ „ 60
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 50
๋ฌผ ๋“์ด๋Š” ์ค‘...
์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!

 

 

 

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์œผ๋กœ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ์˜ ๋Œ€์‘

this.๋งค๊ฐœ๋ณ€์ˆ˜์‹๋ณ„์ž. ํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์˜ api๋“ค์ด ์งœ๋ผ๋ผ๋ž€ ๋‚˜์˜ด

์•„๋ž˜ ์บก์ฒ˜ ์ฐธ๊ณ 

 

์•„๋งˆ์ถ”์–ด ์œ ์ €
ํ”„๋กœ ๋ฐ”๋ฆฌ์Šคํƒ€

<ํ˜„์žฌ๊นŒ์ง€ ์†Œ์Šค์ฝ”๋“œ>

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

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

interface CommercialCoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
  addCapsule(capsule: number): void;
  fulfillWater(water: number): void;
  clean(): void;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker, CommercialCoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  private 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,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

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

  private 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;
  }
}

// tslint:disable-next-line: max-classes-per-file
class AmateurUser {
  constructor(private machine: CoffeeMaker) {}
  makeCoffee() {
    console.log("์ดˆ์‹ฌ์ž๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ");
    const coffee = this.machine.makeCoffee(1);
    console.log(coffee);
  }
}

// tslint:disable-next-line: max-classes-per-file
class ProBarista {
  constructor(private machine: CommercialCoffeeMaker) {}
  makeCoffee() {
    console.log("ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ");
    const coffee = this.machine.makeCoffee(1);
    console.log(coffee);
    this.machine.clean();
    this.machine.fulfillWater(10);
    this.machine.addCapsule(10);
  }
}

const maker: CoffeMachine = CoffeMachine.makeMachine(32, 100);

//์•„๋งˆ์ถ”์–ด์™€ ํ”„๋กœ๋Š” ๋™์ผํ•œ ์ปคํ”ผ๋จธ์‹ ์„ ๋ฐ›์•˜์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ ,
//amateur๋Š” ํด๋ž˜์ŠคAmateurUser๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ ,
//pro๋Š” ํด๋ž˜์ŠคProBarista๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CommercialCoffeeMaker ์•ˆํ† ํŒจ์•„์ˆ˜๋ฃฐ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์—,
//๊ฐ๊ฐ ์ธํ„ฐํŽ˜์ด์Šค์— ์ œ์•ฝ๋œ ํ•ญ๋ชฉ๋“ค๋งŒ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.(์ˆœ์‹๊ฐ„์— ๊ฐ€์šฉ ๋ฒ”์œ„๊ฐ€ ๋‹ฌ๋ผ์ง!!!)

const amateur = new AmateurUser(maker);
const pro = new ProBarista(maker);
amateur.makeCoffee();
pro.makeCoffee();

<์ถœ๋ ฅ ๊ฒฐ๊ณผ> : ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€๋Š” ํ”„๋กœ๋‹ต๊ฒŒ ์ž‘์—… ํ›„ ์ปคํ”ผ๋จธ์‹ ์„ ํ—น๊ตฌ๋Š” ์ž‘์—…์„ ์žŠ์ง€ ์•Š์•˜๋‹ค๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค

์ดˆ์‹ฌ์ž๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ
์บก์А์“ฐ๊ธฐ ์ „ 32
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„29
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 1 }
ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ
์บก์А์“ฐ๊ธฐ ์ „ 29
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„26
๋ฌผ์“ฐ๊ธฐ ์ „ 90
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 80
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 1 }
์ปคํ”ผ๋จธ์‹  ํ—น๊ตฌ๋Š” ์ค‘!

 

 

์ด์ฒ˜๋Ÿผ ๋™์ผํ•œ ์˜ค๋ธŒ์ ํŠธ ์ธ์Šคํ„ด์Šค์ผ์ง€๋ผ๋„ ์ด ์˜ค๋ธŒ์ ํŠธ์˜ ํด๋ž˜์Šค๊ฐ€ ๋‘๊ฐ€์ง€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„์„ ํ–ˆ๊ธฐ์— ๋‹ค์ฑ„๋กญ๊ฒŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„๊ฐˆ ๋‘ ํด๋ž˜์Šค(์•„๋งˆ์ถ”์–ด, ํ”„๋กœ)๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์„ ๋ฐ›๊ธฐ๋กœ ๋งค๊ฐœ์ธ์ž์— ๋ช…์‹œํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์— ๋งž๊ฒŒ ์ƒ์„ฑ์ž์—์„œ ๋ฐ›์•„์˜ค๊ณ , this.์‹๋ณ„์ž.์„ ํ–ˆ์„ ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๊ทœ์•ฝ๋œ( ํด๋ž˜์Šค๋ณด๋‹ค๋Š” ๋ฒ”์œ„๊ฐ€ ์ข์€ ) api๋“ค๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ฒฐ๋ก ์ ์œผ๋กœ ์•„๋งˆ์ถ”์–ด์œ ์ €๋‚˜ ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€ ํด๋ž˜์Šค์—์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”์ง€,

๋ณธ๋ž˜ ํด๋ž˜์Šค์—์„œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ํ•จ์ˆ˜๋“ค์ด ์žˆ๋Š”์ง€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค์— ๊ทœ์•ฝ๋œ ํ•จ์ˆ˜๋“ค๋งŒ ์ด์šฉํ•ด์„œ ์ƒ์„ฑ๋œ ์˜ค๋ธŒ์ ํŠธ์™€ ์˜์‚ฌ์†Œํ†ต์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

๊ทธ๋ ‡๊ธฐ์— ์‚ฌ์šฉ์ž(ํ”„๋กœ๊ทธ๋ž˜๋จธ)๋“ค์€ ๋ณธ๋ž˜ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ๋ณต์žกํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์•Œ ํ•„์š”๋„ ์—†๊ณ ,

์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”์ง€๋ฅผ ์•Œ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค! 

 

 

 

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

type CoffeeCup = {
  orderShot: number;
};

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

interface CommercialCoffeeMaker {
  makeCoffee(shots: number): CoffeeCup;
  addCapsule(capsule: number): void;
  fulfillWater(water: number): void;
  clean(): void;
}

//์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
class CoffeMachine implements CoffeeMaker, CommercialCoffeeMaker {
  private static ONE_SHOT_CAPSULE: number = 3;
  private static ONE_CUP_WATER: number = 10;

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

  constructor(private capsule: number = 3, private 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);
  }

  private 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,
    };
  }
  private heatWater() {
    console.log("๋ฌผ ๋“์ด๋Š” ์ค‘...");
  }

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

  private 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;
  }
}

// tslint:disable-next-line: max-classes-per-file
class AmateurUser {
  constructor(private machine: CoffeeMaker) {}
  makeCoffee() {
    console.log("์ดˆ์‹ฌ์ž๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ");
    const coffee = this.machine.makeCoffee(1);
    console.log(coffee);
  }
}

// tslint:disable-next-line: max-classes-per-file
class ProBarista {
  constructor(private machine: CommercialCoffeeMaker) {}
  makeCoffee() {
    console.log("ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ");
    const coffee = this.machine.makeCoffee(1);
    console.log(coffee);
    this.machine.clean();
    this.machine.fulfillWater(10);
    this.machine.addCapsule(10);
  }
}

const maker: CoffeMachine = CoffeMachine.makeMachine(32, 100);

//์•„๋งˆ์ถ”์–ด์™€ ํ”„๋กœ๋Š” ๋™์ผํ•œ ์ปคํ”ผ๋จธ์‹ ์„ ๋ฐ›์•˜์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ ,
//amateur๋Š” ํด๋ž˜์ŠคAmateurUser๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ ,
//pro๋Š” ํด๋ž˜์ŠคProBarista๊ฐ€ ์ธ์ž๋ฅผ ๋ฐ›์„ ๋•Œ CommercialCoffeeMaker ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์—,
//๊ฐ๊ฐ ์ธํ„ฐํŽ˜์ด์Šค์— ์ œ์•ฝ๋œ ํ•ญ๋ชฉ๋“ค๋งŒ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.(์ˆœ์‹๊ฐ„์— ๊ฐ€์šฉ ๋ฒ”์œ„๊ฐ€ ๋‹ฌ๋ผ์ง!!!)

const amateur = new AmateurUser(maker);
const pro = new ProBarista(maker);
amateur.makeCoffee();
pro.makeCoffee();

 

<์ถœ๋ ฅ>

์ดˆ์‹ฌ์ž๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ
์บก์А์“ฐ๊ธฐ ์ „ 32
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„29
๋ฌผ์“ฐ๊ธฐ ์ „ 100
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 90
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 1 }
ํ”„๋กœ๋ฐ”๋ฆฌ์Šคํƒ€๊ฐ€ ๋‚ด๋ฆฐ ์ปคํ”ผ
์บก์А์“ฐ๊ธฐ ์ „ 29
์บก์А์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์บก์А ์“ฐ๊ณ  ๋‚œ ํ›„26
๋ฌผ์“ฐ๊ธฐ ์ „ 90
๋ฌผ์“ฐ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
๋ฌผ์“ฐ๊ณ  ๋‚œ ํ›„ 80
๋ฌผ ๋“์ด๋Š” ์ค‘...
{ orderShot: 1 }
์ปคํ”ผ๋จธ์‹  ํ–‰๊ตฌ๋Š” ์ค‘!

 

728x90

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

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ํ˜ธ์ถœ ์‹œ๊ทธ๋‹ˆ์ฒ˜(ํƒ€์ž…์‹œ๊ทธ๋‹ˆ์ฒ˜)๋ž€?  (0) 2022.04.20
TS๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜, ์ค‘์ฒฉ ํ•จ์ˆ˜, ๊ณ ์ฐจ ํ•จ์ˆ˜ ๊ตฌํ˜„  (0) 2022.04.20
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์บก์Аํ™”, ์ถ”์ƒํ™” ์ฐจ์ด ๋ช…ํ™•ํ•˜๊ธฐ ์žก๊ธฐ  (0) 2022.04.20
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๋ฌธ๋งฅ์  ํƒ€์ž…ํ™”  (0) 2022.04.20
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ํ˜ธ์ถœ ์‹œ๊ทธ๋‹ˆ์ฒ˜  (0) 2022.04.20
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ํ˜ธ์ถœ ์‹œ๊ทธ๋‹ˆ์ฒ˜(ํƒ€์ž…์‹œ๊ทธ๋‹ˆ์ฒ˜)๋ž€?
    • TS๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜, ์ค‘์ฒฉ ํ•จ์ˆ˜, ๊ณ ์ฐจ ํ•จ์ˆ˜ ๊ตฌํ˜„
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์บก์Аํ™”, ์ถ”์ƒํ™” ์ฐจ์ด ๋ช…ํ™•ํ•˜๊ธฐ ์žก๊ธฐ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๋ฌธ๋งฅ์  ํƒ€์ž…ํ™”
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

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