스터디 내용을 전반적으로 잘 정리한 글이라서 링크를 남긴다.
🙋♀️ 나 : 아래 코드에서 ❓ 표시한 질문에 대해 어떻게 생각하세요?
👨🍳 코치님: 의존성이 있다고 볼 수 있네요?
🙋♀️ 나: 최대한 의존성이 없게 코드를 짜고 싶었는데, 어쩔 수 없이 의존해야 하는 부분이 생기는 것 같아요.
👨🍳 코치님: 맞아요. 어쩔 수 없이 그런 부분이 생깁니다.Garage
객체가 생성될 때 Car 객체를 받도록 하는 건 어때요? 그럼 의존성을 제거할 수 있지 않을까요?
🙋♀️ 나: 마찬가지일 것 같아요.Garage
객체는 불변 객체로 사용하기 때문에const car = new Car({ name, position });
로직은withInsertedCar
함수 내에서 불러줘야 합니다. 말씀해진대로 의존성을 제거하려면withCar
함수가 Car 객체를 받도록 할 수는 있어요. 하지만withCar
인자로 Car 객체를 주고 싶진 않네요.
👨🍳 코치님: 그렇군요. 이 정도의 의존성은 괜찮다고 봅니다. 언제나 의존성을 떼어내는 게 최선의 방법은 아니니까요.
import Car from "./Car.ts";
export default class Garage {
readonly cars: Car[];
constructor({ cars = [] }: { cars?: Car[] } = {}) {
this.cars = cars;
}
init() {
return new Garage({
cars: [],
});
}
withCar({ name, position }: { name: string; position: number }): Garage {
const index = this.cars.findLastIndex((i) => i.name === name);
return index < 0
? this.withInsertedCar({ name, position })
: this.withUpdatedCar({ index, change: position });
}
// ❓ new Car 이것도 의존성이 있는 건가?
private withInsertedCar({
name,
position,
}: {
name: string;
position: number;
}): Garage {
const car = new Car({ name, position });
return new Garage({
cars: [...this.cars, car],
});
}
...
}
🙋♀️ 나 : 아래 코드에서 ❓ 표시한 질문에 대해 어떻게 생각하세요?
👨🍳 코치님:RacingGame
은 Controller 인거죠?
🙋♀️ 나 : 네, 맞아요.
👨🍳 코치님: 그러면 입력 받은 값을 파싱하는 부분은 Controller에 있어도 괜찮겠는데요? Controller의 역할이 입력받은 값을 정제화해서 Domain으로 넘겨주는 걸 포함한다면요.
🙋♀️ 나 : 그렇게 생각하면 괜찮겠네요.Controller에 로직이 있는 게 맞을까?
관점으로 봤는데, 역할 관점에서 보면 데이터 정제는 해도 괜찮겠어요.
// Controller 입니다.
export default class RacingGame {
private garageStore: GarageStore;
constructor(garageStore: GarageStore) {
this.garageStore = garageStore;
}
async start() {
await this.자동차등록();
await this.자동차경주();
this.경주내용출력();
this.우승자출력();
}
...
// ❓ carNames.split(",") 어디서 처리하는 게 좋을까?
private async getCarNames() {
const input = await InputView.readCarNames();
return input.split(",");
}
...