[Think Data Structures] 01. ์ธํฐํ์ด์ค
by Hi.Claire01. ์ธํฐํ์ด์ค
01-1. ๋ฆฌ์คํธ๊ฐ ๋ ์ข ๋ฅ์ธ ์ด์
์ด๋ฒ ์ฅ์์๋ interface์ ์ด๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ฅผ ์ดํด๋ณธ๋ค.
๋ช ๊ฐ์ง ์์ ์์ ArrayList, LinkedList์ ์ ์ฌํ ํด๋์ค๋ฅผ ๊ตฌํํ๋ฉฐ ์ด๋ค์ ๋์ ๋ฐฉ๋ฒ๊ณผ ๊ฐ๊ฐ์ ์ฅ๋จ์ ์ ๋ํด ์์๋ณด์.
01-2. ์๋ฐ interface
์๋ฐ interface๋ ๋ฉ์๋ ์งํฉ์ ์๋ฏธํ๋ค.
์ด interface๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ ๊ณตํด์ผ ํ๋ค.
์์1. Comparable interface
public interface Comparable<T> {
public int compareTo(T o);
}
์ด interface๋ ํ์ ํ๋ผ๋ฏธํฐ์ธ T๋ฅผ ์ฌ์ฉํ์ฌ Comparable์ด๋ผ๋ ์ ๋ค๋ฆญ ํ์ ์ ์ ์ํ๋ค.
์ด interface๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์์ผ ํ๋ค.
- T ํ์ ์ ๋ช ์ํด์ผ ํ๋ค.
- T ํ์ ์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ๊ณ int๋ฅผ ๋ฐํํ๋ compareTo ๋ฉ์๋๋ฅผ ์ ๊ณตํด์ผ ํ๋ค.
์์2. Integer ํด๋์ค
public class Integer extends Number implements Comparable<Integer> {
@Override
public int compareTo(Integer anotherInteger) {
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
// ๋ค๋ฅธ ๋ฉ์๋ ์๋ต
}
์ด ํด๋์ค๋ Number ํด๋์ค๋ฅผ extendsํ์ฌ Number ํด๋์ค์ ๋ฉ์๋์ ์ธ์คํด์ค ๋ณ์๋ฅผ ์์ํ๋ค.
๋ํ Comparable<Integer> interface๋ฅผ ๊ตฌํํ์ฌ Integer ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ๊ณ int๋ฅผ ๋ฐํํ๋ compareTo ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
ํด๋์ค๊ฐ interface๋ฅผ ๊ตฌํํ๋ค๊ณ ์ ์ธํ๋ฉด ์ปดํ์ผ๋ฌ๋ interface๊ฐ ์ ์ํ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋์ง ํ์ธํ๋ค.
01-3. List interface
JCF๋ List๋ผ๋ interface๋ฅผ ์ ์ํ๊ณ ArrayList์ LinkedList๋ผ๋ ๋ ๊ตฌํ ํด๋์ค๋ฅผ ์ ๊ณตํ๋ค.
interface๋ List๊ฐ ๋๋ค๋ ์๋ฏธ๊ฐ ๋ฌด์์ธ์ง๋ฅผ ์ ์ํ๋ค. ์ด interface๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ add, get, remove ๋ฑ์ ํฌํจํ๋ ์ฝ 20๊ฐ์ง ๋ฉ์๋ ์งํฉ์ ์ ๊ณตํด์ผ ํ๋ค.
ArrayList์ LinkedList ํด๋์ค๋ ์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ฏ๋ก ์ํธ๊ตํํ ์ ์๋ค.
List๋ก ๋์ํ๋ ๋ฉ์๋๋ ArrayList์ LinkedList ๋๋ List๋ฅผ ๊ตฌํํ๋ ์ด๋ค ๊ฐ์ฒด์๋ ์ ๋์ํ๋ค.
์์1. ListClientExample
package com.allendowney.thinkdast;
import java.util.LinkedList;
import java.util.List;
public class ListClientExample {
@SuppressWarnings("rawtypes")
private List list;
@SuppressWarnings("rawtypes")
public ListClientExample() {
list = new LinkedList();
}
@SuppressWarnings("rawtypes")
public List getList() {
return list;
}
public static void main(String[] args) {
ListClientExample lce = new ListClientExample();
@SuppressWarnings("rawtypes")
List list = lce.getList();
System.out.println(list);
}
}
ListClientExample ํด๋์ค๋ List๋ฅผ ์บก์ํํ๋ ํด๋์ค์ ํ์ ์์(Listํ์ ์ธ์คํด์ค ๋ณ์)๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ด ์์ ์ ์ค์ํ ์ ์ ํ์ํ ๊ฒฝ์ฐ๊ฐ ์๋๋ฉด ArrayList๋ LinkedList ๊ฐ์ ๊ตฌํ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ฐ๋ฅํ ํ List interface๋ฅผ ์ฌ์ฉํ๋ค๋ ์ ์ด๋ค.
์๋ฅผ ๋ค์ด, ์ธ์คํด์ค ๋ณ์๋ List interface๋ก ์ ์ธํ๊ณ getList ๋ฉ์๋๋ Listํ์ ๋ฐํํ์ง๋ง ๊ตฌ์ฒด์ ์ธ ํด๋์ค๋ ์ธ๊ธํ์ง ์๋๋ค.
๋ง์์ ๋ฐ๊ฟ ArrayList๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด ์์ฑ์๋ง ๋ฐ๊พธ๋ฉด ๋๊ณ ๋๋จธ์ง๋ ๊ทธ๋๋ก ๋๋ฉด ๋๋ค.
์ด๋ฌํ ์คํ์ผ์ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ํ๋ก๊ทธ๋๋ฐ(interface-based programming) ๋๋ ์ธํฐํ์ด์ค ํ๋ก๊ทธ๋๋ฐ(interface programming)์ด๋ผ๊ณ ํ๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋ ์ฝ๋๋ ์ค์ง List์ ๊ฐ์ interface๋ง ์์กดํ๊ณ ArrayList ํด๋์ค์ ๊ฐ์ ํน์ ๊ตฌํ์ ์์กดํด์๋ ์ ๋๋ค.
์ด๋ฌํ ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด ๋์ค์ ๊ตฌํ์ด ๋ณ๊ฒฝ๋์ด๋ interface๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๋ฉด์ interface๊ฐ ๋ณ๊ฒฝ๋๋ค๋ฉด interface๋ฅผ ์์กดํ๋ ์ฝ๋๋ ๋ณ๊ฒฝ๋์ด์ผ ํ๋ค.
์ด ๋๋ฌธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ๋ฐ์๋ ๊ผญ ํ์ํ ์ผ์ด ์๋๋ฉด interface๋ฅผ ์ ๋ณ๊ฒฝํ์ง ์๋๋ค.
01-4. ์ค์ต 1
์์1. ListClientExampleTest
package com.allendowney.thinkdast;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
/**
* @author downey
*
*/
public class ListClientExampleTest {
/**
* Test method for {@link ListClientExample}.
*/
@Test
public void testListClientExample() {
ListClientExample lce = new ListClientExample();
@SuppressWarnings("rawtypes")
List list = lce.getList();
assertThat(list, instanceOf(ArrayList.class) );
}
}
์ด ํด๋์ค๋ ListClientExample ํด๋์ค์ ๋ํ JUnit ์ฝ๋์ด๋ค.
์ด ํด๋์ค์์๋ ํ ๊ฐ์ง ํ ์คํธ๋ฅผ ์คํํ๋๋ฐ, ListClientExample ํด๋์ค์ getList() ๋ฉ์๋๋ฅผ ์คํํ์ฌ ์ด ๋ฉ์๋์ ๋ฐํํ์ด ArrayList ํด๋์ค์ธ์ง ํ์ธํ๋ค.
getList() ๋ฉ์๋๋ LinkedList ํด๋์ค๋ฅผ ๋ฐํํ๋ฏ๋ก ํ ์คํธ์ ์คํจํ๋ค.
์๋ฌ ๋ฉ์์ง
java.lang.AssertionError:
Expected: an instance of java.util.ArrayList
but: <[]> is a java.util.LinkedList
๋ค์ ListClientExample ํด๋์ค์์ ์์ฑ์์ LinkedList๋ฅผ ArrayList๋ก ๋์ฒดํ๊ณ ํ ์คํธ๋ฅผ ์คํํ๋ฉด ์ฑ๊ณตํ๋ค.
์ด ํ ์คํธ๋ฅผ ํต๊ณผํ๊ธฐ ์ํด ์์ฑ์์์ LinkedList ๊ฐ์ฒด ์์ฑ ๋ถ๋ถ๋ง ๋ณ๊ฒฝํ์๊ณ List ์ ์ธ ๋ถ๋ถ์ ๋ณ๊ฒฝํ์ง ์์๋ค.
์ ์ธ ๋ถ๋ถ์ ArrayList๋ก ๋ณ๊ฒฝํด๋ ํ๋ก๊ทธ๋จ์ ์ ์ ๋์ํ๋ค.
ํ์ง๋ง ์ด๊ฒ์ ๊ณผ๋ค ์ง์ (overspecified)์ด๋ค.
๋์ค์ ๋ค์ interface๋ก ๋์๊ฐ๋ ค๋ฉด ๋ ๋ง์ ์ฝ๋๋ฅผ ์์ ํด์ผ ํ ์๋ ์๋ค.
๊ทธ๋ ๋ค๋ฉด ListClientExample ํด๋์ค์ ์์ฑ์์์ ArrayList ๊ฐ์ฒด๋ฅผ List interface๋ก ๊ต์ฒดํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
์ List interface๋ก๋ ์ธ์คํด์ค๊ฐ ์์ฑ๋์ง ์์๊น?
์๋ฌ๋ฉ์์ง
'List' is abstract; cannot be instantiated
'๐ ์๋ฃ๊ตฌ์กฐ & ์๊ณ ๋ฆฌ์ฆ > ์๋ฐ๋ก ๋ฐฐ์ฐ๋ ํต์ฌ ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Think Data Structures] 02. ์๊ณ ๋ฆฌ์ฆ ๋ถ์ (0) | 2023.10.22 |
---|---|
[Think Data Structures] 00. ๊ณต๋ถ ์ค๋นํ๊ธฐ (0) | 2023.09.29 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Claire's Study Note
Hi.Claire