1์ฅ์์๋ ํฐ์ผ ํ๋งค ์์คํ ์ ํตํด ๊ฐ์ฒด์งํฅ ์ค๊ณ๊ฐ ๋ฌด์์ธ์ง, ๊ฐ์ฒด์งํฅ ์ค๊ณ๊ฐ ์ ํ์ํ์ง์ ๋ํด์ ์๊ธฐํ๋ค.
1์ฅ ์์ฝ
์ ์ฐจ์ ์ธ ์ฝ๋(ํ๋ก์ธ์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๋์ ๋ชจ๋์ ์์น) => ๊ฐ์ฒด์งํฅ์ ์ฝ๋(๋ฐ์ดํฐ์ ํ๋ก์ธ์ค๊ฐ ๋์ผํ ๋ชจ๋ ์์ ์์น)๋ก ๋ณ๊ฒฝํ์ฌ ์ํํธ์จ์ด ๋ชจ๋์ด ๊ฐ์ ธ์ผ ํ๋ ์ธ ๊ฐ์ง ๊ธฐ๋ฅ (1. ์คํ ์ค์ ์ ๋๋ก ๋์ํด์ผ ํ๋ฉฐ, 2. ๋ณ๊ฒฝ์ด ์ฌ์์ผ ํ๊ณ , 3. ์ดํดํ๊ธฐ ์ฌ์์ผ ํ๋ค.)์ ๋ง์กฑ์ํฌ ์ ์๋๋ก ํ์๋ค.
์์กด์ฑ(dependency)์ ์ค๊ณ๋ฅผ ์ด๋ ต๊ฒ ๋ง๋ค๊ธฐ ๋๋ฌธ์ ๋ถํ์ํ ์์กด์ฑ์ ์ ๊ฑฐํ์ฌ ๊ฐ์ฒด ์ฌ์ด์ ๊ฒฐํฉ๋(coupling)๋ฅผ ๋ฎ์ถ ์ ์๋ค. ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๋ ๋ฐฉ๋ฒ์ผ๋ก ์บก์ํ(encapsulation)๊ฐ ์๋๋ฐ ์บก์ํ๋ ๊ฐ๋ ์ ์ด๋ ๋ฌผ๋ฆฌ์ ์ผ๋ก ๊ฐ์ฒด ๋ด๋ถ์ ์ธ๋ถ์ ์ธ ์ฌํญ์ ๊ฐ์ถ์ด ๊ฐ์ฒด ๋ด๋ถ๋ก์ ์ ๊ทผ์ ์ ํํ์ฌ ๊ฐ์ฒด์ ๊ฐ์ฒด ์ฌ์ด์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์๋ค. ๋ถํ์ํ ์ธ๋ถ์ฌํญ์ ๊ฐ์ฒด ๋ด๋ถ๋ก ์บก์ํํ๋ ๊ฒ์ ๊ฐ์ฒด์ ์์จ์ฑ์ ๋์ด๊ณ ์์ง๋(cohesion) ๋์ ๊ฐ์ฒด๋ค์ ๊ณต๋์ฒด๋ฅผ ์ฐฝ์กฐํ ์ ์๊ฒ ํ๋ค. (p.29)
๊ฐ์ฒด์งํฅ์์ ๊ฐ ๊ฐ์ฒด๋ ์ค์ค๋ก ์ฑ ์์ ์ํํ๋ ์์จ์ ์ธ ์กด์ฌ์ด๋ค. ๋น๋ก ํ์ค์ธ๊ณ์์๋ Bag๊ฐ์ด ์๋์ ์ธ ์กด์ฌ๋ ์์ธํ(anthropomorphism)๋ฅผ ํตํด ๋ฅ๋์ ์ด๊ณ ์์จ์ ์ธ ์กด์ฌ๊ฐ ๋๋ค.
๊ณต๋ถ๋ฐฉ๋ฒ
- ๊ฐ๋ฐ์๋ ์ฝ๋๋ก ์๊ธฐํ๊ธฐ ๋๋ฌธ์ ๋ณธ๋ฌธ์ ๋์จ ์ฝ๋๋ฅผ ํ ์ค ํ ์ค ํด์ํ๋ฉด์ ์ฝ์๋ค. ์ค๊ฐ์ ์ดํด๊ฐ ์ ์ ๊ฐ๋ ์ฝ๋๊ฐ ๋์ค๋ฉด ๊ด๋ จ๋ ์๋ฐ๋ฅผ ๋ค์ ๋ณต์ตํ๋ค.
- ํด๋์ค ๋ค์ด์ด๊ทธ๋จ๊ณผ ์ปค๋ฎค๋์ผ์ด์ ๋ค์ด์ด๊ทธ๋จ์ ๋ณด๋ฉด์ ํด๋์ค ๊ฐ์ ๊ด๊ณ๋ฅผ ์ดํดํ๋ฉด์ ์ฝ๋๋ฅผ ๋ค์ ๋ณด์๋ค.
- ์ค๊ณ๋ฅผ ๊ฐ์ ํ๋ ๋ฐฉ๋ฒ, ์ฆ ์บก์ํ๋ฅผ ๋ค์ ํ์ด์ง๋ฅผ ๋ณด์ง ์๊ณ ์ค์ค๋ก ๋ณ๊ฒฝํด๋ณด์๋ค. ์ด 3๋ฒ์ ํผ์ ์์ฑํด ๋ณด์๊ณ 3๋ฒ ๋ค ์ฑ
๊ณผ ๋ฌ๋์ง๋ง ์ค์ค๋ก ๊ณ ๋ฏผํด ๋ณผ ์ ์์ด์ ๊ฐ์ ๋ ์ฝ๋๋ฅผ ๋ณด์์ ๋ ์ดํด๊ฐ ๋ ์ ๋์๋ค.
- ๋ค์๋ฒ์๋ ์ฝ๋ ๋ด์ฉ ์ ์ฒด๋ฅผ ๊ณ ๋ คํ๋ฉฐ, ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ๊ณ , ๋ฆฌํดํ์ ์ ๋ํด ๋ ๊ณต๋ถํด์ผ ๋ ๊ฒ ๊ฐ๋ค.
์ถ๊ฐ: ์๋ฐ ์ปฌ๋ ์ ๊ณต๋ถ ๋ด์ฉ (์๋ฐ์ ์ ์ ์ฑํฐ 11)
- List ์ธํฐํ์ด์ค, ArrayList ๊ตฌํํด๋์ค
- ์ค๋ณตํ์ฉ, ์ ์ฅ์์ ์ ์ง
- Object remove(int index) : ์ง์ ๋ ์์น(index)์ ์๋ ๊ฐ์ฒด๋ฅผ ์ ๊ฑฐ
- asList:
@SafeVarargs
public static <T> List<T> asList(T... a)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess. This method also provides a convenient way to create a fixed-size list initialized to
contain several elements:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
Type Parameters:
T - the class of the objects in the array
Parameters:
a - the array by which the list will be backed
Returns:
a list view of the specified array
์ฑ ์ ๋์จ ์ฝ๋
public class TicketOffice {
private Long amount;
private List<Ticket> tickets = new ArrayList<>();
public TicketOffice(Long amount, Ticket ... tickets) {
this.amount = amount;
this.tickets.addAll(Arrays.asList(tickets));
}
public Ticket getTicket() {
return tickets.remove(0);
}
public void minusAmount(Long amount) {
this.amount -= amount;
}
public void plusAmount(Long amount) {
this.amount += amount;
}
}
'์คํฐ๋ > ์ค๋ธ์ ํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ค๋ธ์ ํธ] 6์ฅ '๋ฉ์์ง์ ์ธํฐํ์ด์ค' (1) | 2022.11.18 |
---|---|
[์ค๋ธ์ ํธ] 5์ฅ '์ฑ ์ ํ ๋นํ๊ธฐ' (0) | 2022.11.11 |
[์ค๋ธ์ ํธ] 4์ฅ '์ค๊ณ ํ์ง๊ณผ ํธ๋ ์ด๋์คํ' (0) | 2022.11.04 |
[์ค๋ธ์ ํธ] 3์ฅ '์ญํ , ์ฑ ์, ํ๋ ฅ' (0) | 2022.10.27 |
[์ค๋ธ์ ํธ] 2์ฅ '๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ' (0) | 2022.10.21 |
๋๊ธ