Skip to content
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

๐Ÿš€ 2๋‹จ๊ณ„ - ์š”๊ธˆ ์กฐํšŒ #399

Open
wants to merge 6 commits into
base: jisu3316
Choose a base branch
from

Conversation

kimj2su
Copy link

@kimj2su kimj2su commented Aug 14, 2023

์•ˆ๋…•ํ•˜์„ธ์š” ๋ฆฌ๋ทฐ์–ด๋‹˜ ! ๋ฏธ์…˜ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค..์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ข
1๋‹จ๊ณ„ ๋ฏธ์…˜์—์„œ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋ ๋•Œ๋งˆ๋‹ค ๋ถ„๊ธฐ๋ฌธ์ด ์ถ”๊ฐ€๋  ์ˆ˜ ๋ฐ–์— ์—†์„ ๊ฒƒ ๊ฐ™์•„๋Š” ๋ง์”€์„ ํ•ด์ฃผ์…จ๋Š”๋ฐ ์ด๊ฒƒ์— ๋Œ€ํ•ด์„œ ์ฐพ์•„๋ณด๊ณ  ์ „๋žตํŒจํ„ด, ์Šคํ”„๋ง์˜ DI ๋“ฑ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•(?)๋„ ์ฐพ์•„ ๋ดค์ง€๋งŒ type์ด ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋ฉด ๊ฒฐ๊ตญ์—” ์–ด๋””์„ ๊ฐ€ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๊ณ  ์ ์šฉํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ ์‰ฝ์ง€๊ฐ€ ์•Š๋„ค์š”..
OCP ์›์น™๊ณผ DIP์›์น™์„ ์ž˜ ์ง€์ผฐ๋‹ค๋Š” ์˜ˆ์ œ๋‚˜ ์ฐธ๊ณ ํ• ๋งŒํ•œ ๊ณต๋ถ€ ์ž๋ฃŒ๋ฅผ ์•Œ๊ณ  ๊ณ„์‹œ๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ใ…œใ…œ

Copy link

@catsbi catsbi left a comment

Choose a reason for hiding this comment

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

์ง€์ˆ˜๋‹˜ ์Šฌ๋ž™ ์•Œ๋žŒ์ด ๋ˆ„๋ฝ๋˜์—ˆ๋Š”์ง€ ์•ˆ์šธ๋ ค์„œ ๋ฆฌ๋ทฐ๊ฐ€ ๋งŽ์ด ๋Šฆ์—ˆ๋„ค์š” ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜ฅ
2๋‹จ๊ณ„ ์š”๊ธˆ์กฐํšŒ ๋ฏธ์…˜ ์ž˜ ๊ตฌํ˜„ํ•ด์ฃผ์…จ์–ด์š”. ํ…Œ์ŠคํŠธ์ชฝ์€ ์ฃผ์„์ด๋‚˜ ์ผ€์ด์Šค ๋ชจ๋‘ ๊ผผ๊ผผํ•˜๊ฒŒ ์ž˜ ์ž‘์„ฑ๋œ๊ฒƒ ๊ฐ™์•„ ํฌ๊ฒŒ ํ”ผ๋“œ๋ฐฑ ๋“œ๋ฆด ๋ถ€๋ถ„์ด ์—†๋Š”๋ฐ, ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ์—์„œ ์•„ํ‚คํ…์ฒ˜์ ์œผ๋กœ ์ข€ ๊ณ ๋ฏผํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ํ”ผ๋“œ๋ฐฑ ๋‚จ๊ฒจ๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

OCP๋‚˜ DIP๊ด€๋ จ๋˜์„œ๋Š” ์Œ ๊ฐœ์ธ์ ์œผ๋กœ ํด๋ฆฐ์•„ํ‚คํ…์ฒ˜ ์ฑ…์„ ์ถ”์ฒœ๋“œ๋ ค์š”.
์•„๋‹ˆ๋ฉด ํ•ด๋‹น ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
https://catsbi.oopy.io/ded9db4f-6ed6-48c7-9d46-8a0d88b1b6d0

๊ฐ•์˜๋กœ๋Š” ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง ๊ฐ•์˜์ค‘์—์„œ SOLID์›์น™์œผ๋กœ ์Šคํ”„๋ง์˜ ํƒ„์ƒ๋ฐฐ๊ฒฝ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋Š” ๋ถ€๋ถ„๋„ ์žˆ๋Š”๋ฐ ์ด๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š” ใ…Žใ…Ž

ํ”ผ๋“œ๋ฐฑ ํ™•์ธ ํ›„ ๋‹ค์‹œ ๋ฆฌ๋ทฐ์š”์ฒญ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

private final int fare;

public Fare(int distance) {
this.fare = generateFare(distance);
Copy link

Choose a reason for hiding this comment

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

์ƒ์„ฑ์ž์— ์ดˆ๊ธฐํ™” ๋น„์ฆˆ๋‹ˆ์Šค๋กœ์ง์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์ƒ์„ฑ์— ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๋™์ž‘(๊ฐ’ ํ• ๋‹น, ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์ •๋„)๋งŒ ํ•ด์ฃผ์‹œ๋Š”๊ฑธ ๊ถŒ์žฅ๋“œ๋ ค์š”.

๊ฐœ๋ฐœ์ž ์ž…์žฅ์—์„œ ์ƒ์„ฑ์ž ํ˜ธ์ถœ์‹œ ์ถ”๊ฐ€์ ์ธ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์ƒ์„ฑ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ๋ณด๋‹จ ๋‹จ์ˆœํ•˜๊ฒŒ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ด ๋  ๊ฒƒ ์ •๋„๋งŒ ์œ ์ถ”๋ฅผ ํ•  ์ˆ˜ ์žˆ์ฃ . ๊ทธ๋ž˜์„œ ์ด๋Ÿฐ ๋กœ์ง๋“ค์ด ์ถ”๊ฐ€๋  ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด ์ •์ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜
๋ณ„๋„์˜ init๋ฉ”์„œ๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!

}

// ๋งจ์ฒ˜์Œ 55 -> ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ 45 ๋„˜์–ด์˜ด
private int calculateFare(int distance) {
Copy link

Choose a reason for hiding this comment

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

  • ์š”๊ธˆ ์ผ๊ธ‰ ๊ฐ์ฒด์ธ๋ฐ ์ž๊ธฐ์ž์‹ ์— ๋Œ€ํ•œ ์š”๊ธˆ ์ •์ฑ…์„ ๊ฑฐ๋ฆฌ๋ณ„๋กœ ์–ด๋–ป๊ฒŒ ๊ฐ€์ ธ๊ฐˆ์ง€๊นŒ์ง€ ์ฑ…์ž„์„ ์ง€๊ณ  ์žˆ์–ด์š”. ์ด๊ฒŒ ์ ์ ˆํ•œ ์ฑ…์ž„ ์ผ๊นŒ์š”? SRP์›์น™์— ์œ„๋ฐฐ๋  ๊ฒƒ ๊ฐ™๋„ค์š”! ํ•ด๋‹น ๊ฐ์ฒด๋Š” ์š”๊ธˆ์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•œ ์—ฐ์‚ฐ์ •๋„๋ฅผ ์ฑ…์ž„์ง€๋Š” ๊ฒƒ์ด์ง€ ๊ฑฐ๋ฆฌ๊นŒ์ง€ ์•Œ๊ณ  ๊ณ„์‚ฐํ•  ์ฑ…์ž„์€ ์—†์„ ๊ฒƒ ๊ฐ™์•„์š”!

  • ๋‹ค๋ฅธ ์œ„์น˜๋กœ ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ฎ๊ฒจ์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๊ฒฝ์šฐ, ์ด๋ฒˆ์—๋Š” ๋งค๋ฒˆ ์š”๊ธˆ ์ •์ฑ…์ด ์ถ”๊ฐ€๋ ๋•Œ๋งˆ๋‹ค ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์•ผ ํ• ๊นŒ์š”? ์ถ”์ƒํ™”๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์š”๊ธˆ๊ณ„์‚ฐ ๋กœ์ง์ด ๊ฑฐ๋ฆฌ๊ฐ€ ๋ช‡์ผ๋•Œ ์–ด๋–ค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ๋„ ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?

Comment on lines 31 to 36
for (FindPathType findPathType : FindPathType.values()) {
if (findPathType.equals(type)) {
return findPathType;
}
}
return null;
Copy link

Choose a reason for hiding this comment

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

๋ช…์‹œ์ ์œผ๋กœ null์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฑด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ž…์žฅ์—์„œ ํ•ญ์ƒ NPE ์œ„ํ—˜์— ๋Œ€ํ•ด ๋Œ€์‘์„ ํ•ด์•ผํ•˜๊ธฐ๋•Œ๋ฌธ์— ๊ถŒ์žฅ๋˜์ง€ ์•Š์•„์š” ์ฐจ๋ผ๋ฆฌ NONEํƒ€์ž…๊ฐ™์€ ๋ณ„๋„์˜ ํƒ€์ž…์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ Optional์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฒŒ ๋‚ซ๊ฒ ์ฃ 

๊ทธ๋ฆฌ๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฏธ FindPathType์ธ๋ฐ findPathType์„ ํ•˜๋Š” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ํ•ด๋‹น ํƒ€์ž…์ด๋ ค๋ฉด ํ•ด๋‹น ์—ด๊ฑฐ ํƒ€์ž…์— ์žˆ๋Š” ์ธ์Šคํ„ด์Šค(DISTANCE, DURATION)์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ• ํ…๋ฐ์š”. null์ด ๋  ์ˆ˜ ์žˆ์„๊นŒ์š”?

Comment on lines 8 to 9
DISTANCE("DISTANCE", "๊ฑฐ๋ฆฌ"),
DURATION("DURATION", "์‹œ๊ฐ„");
Copy link

Choose a reason for hiding this comment

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

์ถœ๋ ฅ์„ ์–ด๋–ป๊ฒŒ ํ• ์ง€์— ๋Œ€ํ•ด์„œ(์‹œ๊ฐ„, ๊ฑฐ๋ฆฌ)๋Š” ์ถœ๋ ฅ ๊ณ„์ธต์—์„œ ์•Œ์•„์„œ ์ฑ…์ž„์ ธ๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”!

Comment on lines 24 to 27
if (findPathType(type) == DISTANCE) {
return new EdgeWeightDistance();
}
return new EdgeWeightDuration();
Copy link

Choose a reason for hiding this comment

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

์ฐจ๋ผ๋ฆฌ ํ•ด๋‹น ๊ฐ„์„ ์ •๋ณด ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค (DISTANCE, DURATION)์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์žก๊ฑฐ๋‚˜ ๋žŒ๋‹ค์‹์œผ๋กœ ์ƒ์„ฑ ์ œ๊ณตํ•˜๋„๋ก ํ•ด์ฃผ๋ฉด if๋ฌธ์œผ๋กœ ์ง์ ‘ ๊ณ ๋ฅด์ง€ ์•Š์•„๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”?

@kimj2su
Copy link
Author

kimj2su commented Aug 17, 2023

์•ˆ๋…•ํ•˜์„ธ์š” ๋ฆฌ๋ทฐ์–ด๋‹˜!
๋ฆฌ๋ทฐ ์ฃผ์‹  ์ฝ”๋ฉ˜ํŠธ ์œ„์ฃผ๋กœ ์ตœ๋Œ€ํ•œ ๋ฐ˜์˜ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!!
์ด๋ฒˆ ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ํ•œ๋ฒˆ ๋” ์ƒ๊ฐํ•˜๊ฒŒ ๋˜๊ณ  ๊ณต๋ถ€ ๋ฐฉํ–ฅ์„ฑ์„ ๋˜ ํ•˜๋‚˜ ์žก์€ ๊ฒƒ ๊ฐ™์•„์„œ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค!..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants