Skip to content

Commit

Permalink
Merge branch 'depromeet:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dep1ayer authored Sep 3, 2023
2 parents 6f5df28 + 5daed25 commit 1f49c84
Showing 1 changed file with 95 additions and 0 deletions.
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) { }
}
```

0 comments on commit 1f49c84

Please sign in to comment.