- ์ธ๋ถ์์ ํด๋์ค๋ฅผ ๋ฐ๋ผ ๋ดค์ ๋, ์ฌ์ฉํด์ผ ํ ํจ์๊ฐ ๋๋ฌด ๋ง์ด ๋จ๋ฉด ์ฌ์ฉ์๋ ๋ฌด์์ ์ฌ์ฉํด์ผ ํ ์ง ํผ๋์ค๋ฌ์ธ ์ ์๋ค.
์ด๋ฅผ ๊ฐ์ ํ๊ธฐ ์ํ ๋์ ํ ์ ์๋ ๊ฐ๋ ์ด ๋ฐ๋ก '์ถ์ํ'์ด๋ค.
์ ๋ง ํ์ํ 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
-2. ์ธํฐํ์ด์ค๋ฅผ ํตํ ์ถ์ํ
์ธํฐํ์ด์ค ์ "๋๋ ํ๋ํ๋ ค๋ฉด ๋๋ ์ด๋ฌ์ด๋ฌ์ด๋ฌํ ๊ท์ฝ์ ๊ฐ์ง๊ณ ์์ด. ์ด๋ฐ ํ๋๋ค์ ํ ์ ์์ด~"๋ผ๊ณ ๋ช ์ํด๋๋ ๊ณ์ฝ์์ด์ ์ฝ์!
interface CoffeeMaker{
//์์ธํ๊ฒ ๋ช
์ธ์ ์์ฑ, ๋ช๋ฒ์ ์ท์ ๋ฐ์ ๊ฑด์ง, ์ด๋ค ํ์
์ ๋ฆฌํดํ ๊ฑด์ง
makeCoffee(shots:number):CoffeeCup;
}
์ด ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ๋ฉด CoffeeCup์ ๋ฆฌํดํ๋ makeCoffee() ํจ์๋ฅผ ์ด์ฉํ ์ ์๋ค.
์ธํฐํ์ด์ค ๊ท๊ฒฉ์ ๋ฐ๋ฅด๊ธธ ์ํ๋ ํด๋์ค์ ํ์ํ ํค์๋๋
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
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);
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 {
<ํ์ฌ๊น์ง ์งํ์ํฉ>
// //์ปคํผ๋จธ์ ๋ง๋ค๊ธฐ
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 }
์ปคํผ๋จธ์ ํ๊ตฌ๋ ์ค!
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ ์คํฌ๋ฆฝํธ] ํธ์ถ ์๊ทธ๋์ฒ(ํ์ ์๊ทธ๋์ฒ)๋? (0) | 2022.04.20 |
---|---|
TS๋ก ์ฝ๋ฐฑ ํจ์, ์ค์ฒฉ ํจ์, ๊ณ ์ฐจ ํจ์ ๊ตฌํ (0) | 2022.04.20 |
[ํ์ ์คํฌ๋ฆฝํธ] ์บก์ํ, ์ถ์ํ ์ฐจ์ด ๋ช ํํ๊ธฐ ์ก๊ธฐ (0) | 2022.04.20 |
[ํ์ ์คํฌ๋ฆฝํธ] ๋ฌธ๋งฅ์ ํ์ ํ (0) | 2022.04.20 |
[ํ์ ์คํฌ๋ฆฝํธ] ํธ์ถ ์๊ทธ๋์ฒ (0) | 2022.04.20 |