-
Notifications
You must be signed in to change notification settings - Fork 7
[8주차] nyj #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: nyj
Are you sure you want to change the base?
[8주차] nyj #54
Changes from 5 commits
c64f5dd
ef60cb3
5c603ad
e9835f9
ec301b5
4200125
e31904a
2ef574b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 자바 기초 | ||
| ## CQRS | ||
| > CQRS는 Command and Query Responsibility Segregation(명령과 조회의 책임 분리)을 나타낸다. 이름처럼 시스템에서 명령을 처리하는 책임과 조회를 처리하는 책임을 분리하는 것이 CQRS의 핵심이다. | ||
|
|
||
| <img src = "https://blog.nebrass.fr/wp-content/uploads/cqrs-simple-diagram-886x1024.png" height="500"> | ||
|
|
||
| - 명령(Command) : 시스템의 상태를 변경하는 작업 | ||
| - ex) 주문 취소, 배송 완료 | ||
| - 쿼리(Query) : 시스템의 상태를 반환하는 작업 | ||
| - ex) 주문 목록 | ||
| - 책임(Responsibility) : 구성 요소의 역할 | ||
| - 구성 요소 : 클래스, 함수, 모듈/패키지, 웹서버/DB | ||
|
|
||
| - 분리(Segregation) : 역할에 따라 구성 요소 나누기 | ||
|
|
||
| ### 명령과 조회에 단일 모델을 사용한다면? | ||
| 🔍 명령과 쿼리가 다루는 데이터가 다르기 때문에 이도 저도 아닌 잡탕이 된다. | ||
| - 코드 역할/책임 모호 | ||
| - 의미/가독성 등 나빠짐 | ||
| - 유지보수성이 떨어짐 | ||
| - 명령과 쿼리는 코드 변경 빈도/사용자가 다르기 때문 | ||
| - 기능마다 요구하는 성능이 다르기 때문 | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import racingcar.Game; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Game game = new Game(); | ||
| game.start(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package racingcar; | ||
|
|
||
| import utils.RandomUtils; | ||
|
|
||
| public class Car { | ||
| private static final int FIRST_DIGIT = 0; | ||
| private static final int LAST_DIGIT = 9; | ||
| private static final int MOVING_NUMBER = 4; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3개의 상수 추출 좋습니다 👍 |
||
|
|
||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition(){ | ||
| return position; | ||
| } | ||
|
|
||
| public void move() { | ||
| int randomNumber = RandomUtils.nextInt(FIRST_DIGIT, LAST_DIGIT); | ||
| if (randomNumber >= MOVING_NUMBER) { | ||
| position++; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 클래스 파일에서는 괜찮은데 마지막 줄이 생략됐습니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Cars { | ||
| private List<Car> cars; | ||
|
|
||
| public Cars(List<String> carNames) { | ||
| this.cars = carNames.stream() | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<Car> run() { | ||
| for (Car car: cars) { | ||
| car.move(); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| public Winner winner() { | ||
| return new Winner(cars ,cars.stream() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 내용이 과연 우승자일까요? |
||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .getAsInt()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package racingcar; | ||
|
|
||
| import view.InputView; | ||
| import view.ResultView; | ||
|
|
||
| public class Game { | ||
|
|
||
| public void start() { | ||
| Cars cars = new Cars(InputView.inputCarName()); | ||
| int gameNumber = InputView.inputNumber(); | ||
|
|
||
| ResultView.printResultTitle(); | ||
|
|
||
| for (int i = 0; i < gameNumber; i++) { | ||
| ResultView.printResultPosition(cars.run()); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| ResultView.printWinners(cars.winner()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Winner { | ||
| private List<Car> cars; | ||
| private int maxPosition; | ||
|
|
||
| public Winner(List<Car> cars, int maxPosition) { | ||
| this.cars = cars; | ||
| this.maxPosition = maxPosition; | ||
| } | ||
|
|
||
| public List<String> getWinnersName() { | ||
| List<String> winnerNames = new ArrayList<>(); | ||
| for (int i = 0; i < cars.size(); i++) { | ||
| if(cars.get(i).getPosition() == maxPosition){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인덴트는 최대 1입니다 |
||
| winnerNames.add(cars.get(i).getName()); | ||
| } | ||
| } | ||
| return winnerNames; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package utils; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomUtils { | ||
| private static final Random RANDOM = new Random(); | ||
|
|
||
| private RandomUtils() { | ||
|
|
||
| } | ||
|
|
||
| public static int nextInt(final int startInclusive, final int endInclusive) { | ||
| if (startInclusive > endInclusive) { | ||
| throw new IllegalArgumentException("[ERROR] Random 값이 잘못 되었습니다."); | ||
| } | ||
|
|
||
| if (startInclusive < 0) { | ||
| throw new IllegalArgumentException("[ERROR] Random 값이 잘못 되었습니다."); | ||
| } | ||
|
|
||
| if (startInclusive == endInclusive) { | ||
| return startInclusive; | ||
| } | ||
|
|
||
| return startInclusive + RANDOM.nextInt(endInclusive - startInclusive + 1); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| package view; | ||||||
|
|
||||||
| import java.util.Arrays; | ||||||
| import java.util.List; | ||||||
| import java.util.Scanner; | ||||||
|
|
||||||
| public class InputView { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정적메서드 만을 제공하는 정적 클래스 입니다 |
||||||
| private static final Scanner scanner = new Scanner(System.in); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| public static List<String> inputCarName(){ | ||||||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||||||
| List<String> carName = Arrays.asList(scanner.nextLine().split(",")); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 불필요한 inline 변수 같습니다 |
||||||
| return carName; | ||||||
| } | ||||||
|
|
||||||
| public static int inputNumber(){ | ||||||
| System.out.println("시도할 회수는 몇회인가요?"); | ||||||
| try { | ||||||
| int number = scanner.nextInt(); | ||||||
| return number; | ||||||
| } catch (IllegalArgumentException e) { | ||||||
| throw new IllegalArgumentException("[ERROR] 숫자가 아닌 문자를 입력하셨습니다.\n"); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package view; | ||
|
|
||
| import racingcar.Car; | ||
| import racingcar.Winner; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ResultView { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InputView와 마찬가지 |
||
| public static final String Bar = "-"; | ||
|
|
||
| public static void printResultTitle(){ | ||
| System.out.println(); | ||
| System.out.println("실행 결과"); | ||
| } | ||
|
|
||
| public static void printResultPosition(List<Car> cars){ | ||
| for(Car car : cars) { | ||
| printCarPosition(car); | ||
| } | ||
| } | ||
|
|
||
| public static void printCarPosition(Car car) { | ||
| System.out.print(car.getName() + " : "); | ||
| for (int i = 0; i < car.getPosition(); i++) { | ||
| System.out.print(Bar); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public static void printWinners(Winner winner) { | ||
| System.out.println("최종 우승자: " + winner.getWinnersName()); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모두 해당 객체의 내부 구조를 잘 알고 있어야 가능 할 것 같네요 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
상태 값을 전부 포장해 캡슐화를 한 것 처럼 보이지만
결국 게터를 통해 내부 상태가 다른 객체에게 다 노출되고 있습니다.
게터를 사용하지 않고 코드를 작성해보는 습관을 들이는 것이 좋을 것 같습니다