일이 진행되는 순서대로 프로그래밍하는 방법
{
type CoffeeCup = {
shots: number;
hasMilk: boolean;
};
const BEANS_GRAMM_PER_SHOT: number = 7;
let coffeeBeans: number = 0;
function makeCoffee(shots: number): CoffeeCup {
if (coffeeBeans < shots * BEANS_GRAMM_PER_SHOT) {
throw new Error('Not enough coffee beans!');
}
coffeeBeans -= shots * BEANS_GRAMM_PER_SHOT;
return {
shots,
hasMilk: false,
};
}
coffeeBeans += 3 * BEANS_GRAMM_PER_SHOT;
const coffee = makeCoffee(2);
console.log(coffee);
}
프로그램을 객체로 정해서 객체들끼리 의사소통! = 객체의 집합으로 프로그램을 표현하려는 프로그래밍 패러다임
서로 관련있는 데이터와 함수들을 object로 정의해서 프로그래밍 하는 것
생산성 높음, 빠름, 높은 퀄리티, 유지보수 가능
{
type CoffeeCup = {
shots: number;
hasMilk: boolean;
};
class CoffeeMaker {
static BEANS_GRAMM_PER_SHOT: number = 7; // class level
coffeeBeans: number = 0; // instance (object) level
constructor(coffeeBeans: number) {
this.coffeeBeans = coffeeBeans;
}
static makeMachine(coffeeBeans: number): CoffeeMaker {
return new CoffeeMaker(coffeeBeans);
}
makeCoffee(shots: number): CoffeeCup {
if (this.coffeeBeans < shots * CoffeeMaker.BEANS_GRAMM_PER_SHOT) {
throw new Error('Not enough coffee beans!');
}
this.coffeeBeans -= shots * CoffeeMaker.BEANS_GRAMM_PER_SHOT;
return {
shots,
hasMilk: false,
};
}
}
const maker = new CoffeeMaker(32);
console.log(maker);
const maker2 = new CoffeeMaker(14);
console.log(maker2);
const maker3 = CoffeeMaker.makeMachine(3);
}