forked from depromeet/effective-java-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'depromeet:main' into main
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
Ch08/item51/๋ฉ์๋_์๊ทธ๋์ฒ๋ฅผ_์ ์คํ_์ค๊ณํ๋ผ.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# item51. ๋ฉ์๋ ์๊ทธ๋์ฒ๋ฅผ ์ ์คํ ์ค๊ณํ๋ผ | ||
|
||
์ฐธ๊ณ ) ๋ฉ์๋ ์๊ทธ๋์ฒ(Method Signature) | ||
- ๋ฉ์๋์ ์ด๋ฆ๊ณผ ๋งค๊ฐ๋ณ์์ ์์, ํ์ , ๊ฐ์๋ฅผ ์๋ฏธํ๋ค | ||
- ๋ฉ์๋์ ๋ฆฌํด ํ์ ์ด๋ ์์ธ ์ฒ๋ฆฌ๋ ๋ฉ์๋ ์๊ทธ๋์ฒ๊ฐ ์๋๋ค | ||
|
||
## ๋ฉ์๋ ์๊ทธ๋์ฒ๋ฅผ ์ค๊ณํ๋ ๋ฐฉ๋ฒ | ||
### 1. ๋ฉ์๋ ์ด๋ฆ์ ์ ์คํ ์ง์ | ||
- ํ์ค ๋ช ๋ช ๊ท์น(item 68)์ ๋ฐ๋ฅธ ๊ฒ | ||
- ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ์์ ๋๋ฆฌ ๋ฐ์ผ๋ค์ฌ์ง๋ ์ด๋ฆ์ ์ฌ์ฉํ ๊ฒ | ||
- ๊ธด ์ด๋ฆ์ ํผํ ๊ฒ | ||
- ์ ๋งคํ๋ฉด ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ API ๊ฐ์ด๋๋ฅผ ์ฐธ๊ณ ํ ๊ฒ | ||
|
||
### 2. ํธ์ ๋ฉ์๋๋ฅผ ๋๋ฌด ๋ง์ด ๋ง๋ค์ง ๋ง์ | ||
- ๋ฉ์๋๊ฐ ๋๋ฌด ๋ง์ ํด๋์ค๋ ์ตํ๊ณ , ์ฌ์ฉํ๊ณ , ๋ฌธ์ํํ๊ณ , ํ ์คํธํ๊ณ , ์ ์ง๋ณด์ํ๊ธฐ ์ด๋ ต๋ค | ||
- ์์ฃผ ์ฐ์ผ ๊ฒฝ์ฐ์๋ง ๋ง๋ค๊ณ ํ์ ์ด ์์ง ์์ผ๋ฉด ๋ง๋ค์ง ๋ง ๊ฒ | ||
|
||
### 3. ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ ์งง๊ฒ ์ ์งํ์ | ||
- 4๊ฐ ์ดํ๋ฅผ ์ ์งํ ๊ฒ | ||
- ๊ฐ์ ํ์ ์ ๋งค๊ฐ๋ณ์ ์ฌ๋ฌ ๊ฐ๊ฐ ์ฐ๋ฌ์ ๋์ค๋ ๊ฒฝ์ฐ๊ฐ ํนํ ํด๋กญ๋ค | ||
- ์ค์๋ก ์์๋ฅผ ๋ฐ๊ฟ ์ ๋ ฅํด๋ ๊ทธ๋๋ก ์ปดํ์ผ๋๊ณ ์คํ๋๋ค | ||
|
||
### ๋งค๊ฐ๋ณ์ ๊ฐ์๋ฅผ ์ค์ฌ์ฃผ๋ ๊ธฐ์ ์ธ ๊ฐ์ง | ||
1. ์ฌ๋ฌ ๋ฉ์๋๋ก ์ชผ๊ฐ๊ธฐ | ||
```java | ||
/* ์ง์ ํ ๋ฒ์์ ๋ถ๋ถ ๋ฆฌ์คํธ์์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๊ธฐ */ | ||
findValue(int from, int to, Object value); // ์ | ||
|
||
nums.subList(start, end).indexOf(value); // ํ | ||
``` | ||
|
||
2. ๋งค๊ฐ๋ณ์ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฌถ๋ ๋์ฐ๋ฏธ ํด๋์ค๋ฅผ ๋ง๋ค๊ธฐ([์ฐธ๊ณ ](https://github.com/depromeet/effective-java-study/blob/main/Ch04/item24/%EB%A9%A4%EB%B2%84%20%ED%81%B4%EB%9E%98%EC%8A%A4%EB%8A%94%20%EB%90%98%EB%8F%84%EB%A1%9D%20static%20%ED%81%B4%EB%9E%98%EC%8A%A4%EB%A1%9C%20%EB%A7%8C%EB%93%A4%EB%9D%BC.md)) | ||
```java | ||
/* ์นด๋ ๊ฒ์ */ | ||
public class Deck { | ||
public void shuffle() { } | ||
|
||
public void addCard(int suit, int rank) { } | ||
|
||
public void removeCard(int suit, int rank) { } | ||
} | ||
``` | ||
|
||
```java | ||
/* ์นด๋ ๊ฒ์ */ | ||
public class Deck { | ||
static public class Card { | ||
private final int suit; // ๋ฌด๋ฌ | ||
private final int rank; // ์ซ์ | ||
} | ||
|
||
public void shuffle() { } | ||
|
||
public void addCard(Card c) { } | ||
|
||
public void removeCard(Card c) { } | ||
} | ||
``` | ||
|
||
3. ๋น๋ ํจํด์ ์ ์ฉํ ๊ฐ์ฒด๋ฅผ ๋ฉ์๋ ํธ์ถ์ ์์ฉํ๊ธฐ([์ฐธ๊ณ ](https://github.com/depromeet/effective-java-study/blob/main/Ch02/item02/%EC%83%9D%EC%84%B1%EC%9E%90%EC%97%90_%EB%A7%A4%EA%B0%9C%EB%B3%80%EC%88%98%EA%B0%80_%EB%A7%8E%EB%8B%A4%EB%A9%B4_%EB%B9%8C%EB%8D%94%EB%A5%BC_%EA%B3%A0%EB%A0%A4%ED%95%98%EB%9D%BC.md)) | ||
```java | ||
Item item = Item.builder() | ||
.user(user) | ||
.song(song) | ||
.albumCover(song.getAlbum().getAlbumCover()) | ||
.content(itemCreateRequestDto.getContent()) | ||
.build(); | ||
``` | ||
|
||
### 4. ๋งค๊ฐ๋ณ์์ ํ์ ์ผ๋ก๋ ํด๋์ค๋ณด๋ค๋ ์ธํฐํ์ด์ค๊ฐ ๋ ๋ซ๋ค | ||
- ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์ฐํจ์ ์ ๊ณตํ๋ค | ||
- List๋ฅผ ์ ๋ ฌํ๋ sort ๋ฉ์๋๋ฅผ ๋ง๋ ๋ค๋ฉด List์ธํฐํ์ด์ค๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ฉด ๋๋ค. ๊ตฌํ ํด๋์ค์ธ ArrayList๋ LinkedList๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ์ธํ ์ด์ ๋ ์๋ค | ||
```java | ||
/* Collections ํด๋์ค์ sort ๋ฉ์๋ */ | ||
public static <T extends Comparable<? super T>> void sort(List<T> list) { | ||
list.sort(null); | ||
} | ||
``` | ||
|
||
### 5. boolean๋ณด๋ค๋ ์์ 2๊ฐ์ง๋ฆฌ ์ด๊ฑฐ ํ์ ์ด ๋ซ๋ค | ||
- ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์์ ์ฑ์ ๋ํ์ค๋ค | ||
```java | ||
public class Temperature { | ||
public Temperature(boolean isCelsius, double value) { } | ||
} | ||
``` | ||
|
||
```java | ||
public class Temperature { | ||
static public enum TemperatureScale { | ||
FAHRENHEIT, CELSIUS;} | ||
|
||
public Temperature(TemperatureScale temperatureScale, double value) { } | ||
} | ||
``` |