Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .idea/2021-Java-Study.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions CQRS.md
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) : 역할에 따라 구성 요소 나누기

### 명령과 조회에 단일 모델을 사용한다면?
🔍 명령과 쿼리가 다루는 데이터가 다르기 때문에 이도 저도 아닌 잡탕이 된다.
- 코드 역할/책임 모호
- 의미/가독성 등 나빠짐
- 유지보수성이 떨어짐
- 명령과 쿼리는 코드 변경 빈도/사용자가 다르기 때문
- 기능마다 요구하는 성능이 다르기 때문



8 changes: 8 additions & 0 deletions java/Application.java
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();
}
}
33 changes: 33 additions & 0 deletions java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package racingcar;

import utils.RandomUtils;

public class Car {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상태 값을 전부 포장해 캡슐화를 한 것 처럼 보이지만
결국 게터를 통해 내부 상태가 다른 객체에게 다 노출되고 있습니다.
게터를 사용하지 않고 코드를 작성해보는 습관을 들이는 것이 좋을 것 같습니다

private static final int FIRST_DIGIT = 0;
private static final int LAST_DIGIT = 9;
private static final int MOVING_NUMBER = 4;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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++;
}
}


}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 클래스 파일에서는 괜찮은데 마지막 줄이 생략됐습니다.
마지막 줄도 컨벤션 중에 한 종류입니다

28 changes: 28 additions & 0 deletions java/racingcar/Cars.java
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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 내용이 과연 우승자일까요?
우승자가 직접 우승자를 찾는 걸까요?

.mapToInt(Car::getPosition)
.max()
.getAsInt());
}
}
21 changes: 21 additions & 0 deletions java/racingcar/Game.java
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());
}
}
24 changes: 24 additions & 0 deletions java/racingcar/Winner.java
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){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인덴트는 최대 1입니다

winnerNames.add(cars.get(i).getName());
}
}
return winnerNames;
}
}
27 changes: 27 additions & 0 deletions java/utils/RandomUtils.java
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);
}
}
25 changes: 25 additions & 0 deletions java/view/InputView.java
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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정적메서드 만을 제공하는 정적 클래스 입니다
생성자를 막아줘서 사용의 혼동을 막아주면 좋겠습니다.

private static final Scanner scanner = new Scanner(System.in);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final Scanner scanner = new Scanner(System.in);
private static final Scanner SCANNER= new Scanner(System.in);


public static List<String> inputCarName(){
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
List<String> carName = Arrays.asList(scanner.nextLine().split(","));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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");
}
}
}
33 changes: 33 additions & 0 deletions java/view/ResultView.java
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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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());
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모두 해당 객체의 내부 구조를 잘 알고 있어야 가능 할 것 같네요
몰라도 가능하도록 구현을 개선해야 할 것 같습니다.

Loading