Java

์Šคํ„ฐ๋”” ํ• ๋ž˜ 14์ฃผ์ฐจ ๊ณผ์ œ: ์ œ๋„ค๋ฆญ(feedback, ํ”ผ๋“œ๋ฐฑ)

ํ–ฅ์ฐก 2021. 2. 28. 19:37

๐Ÿ’ก ํƒ€์ž… ๋ณ€์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ T์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค.

-> ์•„๋ฌด๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

<? extends > : ์ƒ์œ„ ์ œํ•œ(Upper Bounded)

<? super > : ํ•˜์œ„ ์ œํ•œ(Lower Bounded) 

๐Ÿ”Ž ํด๋ž˜์Šค์— ์ƒ์œ„ ์ œํ•œ์€ ์ธํ„ฐํŽ˜์ด์Šค๋„ ์ง€์›์ด ๋˜๋‚˜?

public interface RankGame {
}
public class LoL extends Game implements RankGame{
}
public class Game {
}

ํด๋ž˜์Šค์— ํ•˜์œ„ ์ œํ•œ์€ ์ธํ„ฐํŽ˜์ด์Šค ์ง€์›์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

Bridge Method : ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ  ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋‹คํ˜•์„ฑ์„ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์„ฑ

 

๐Ÿ’ก ์ œ๋„ค๋ฆญ์„ ์ด์šฉํ•˜์—ฌ ๊ณตํ†ต๋˜๋Š” Dao์ฝ”๋“œ ์ค„์ด๊ธฐ

AppleDao์™€ BananaDao๋Š” ๊ฐ™์€ ์ฝ”๋“œ์ด๋‹ค.

public class AppleDao {

    private Map<Integer, Apple> datasource = new HashMap<>();

    public void save(Apple apple) {
        datasource.put(apple.getId(), apple);
    }

    public void delete(Apple apple) {
        datasource.remove(apple.getId());
    }

    public void delete(Integer integer) {
        datasource.remove(integer);
    }
    public List<Apple> findAll() {
        return new ArrayList<>(datasource.values());
    }
    public Apple findById(Integer id) {
        return datasource.get(id);
    }
}
public class BananaDao {

    private Map<Integer, Banana> datasource = new HashMap<>();

    public void save(Banana banana) {
        datasource.put(banana.getId(), banana);
    }

    public void delete(Banana banana) {
        datasource.remove(banana.getId());
    }

    public void delete(Integer integer) {
        datasource.remove(integer);
    }
    public List<Banana> findAll() {
        return new ArrayList<>(datasource.values());
    }
    public Banana findById(Integer id) {
        return datasource.get(id);
    }
}

 

1. Entity ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ

public interface Entity<K> {

    K getId();
}

2. Apple ํด๋ž˜์Šค์—์„œ Entity์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„

public class Apple implements Entity<Integer>{

    private Integer id;

    public Integer getId() {
        return id;
    }

    public static Apple of(Integer id) {
        Apple apple = new Apple();
        apple.id = id;
        return apple;
    }
}

3. GenericDao ์ƒ์„ฑ

public class GenericDao<E extends Entity<K>, K> {

    private Map<K, E> datasource = new HashMap<>();

    public void save(E entity) {
        datasource.put(entity.getId(), entity);
    }

    public void delete(E entity) {
        datasource.remove(entity.getId());
    }

    public void delete(K id) {
        datasource.remove(id);
    }
    public List<E> findAll() {
        return new ArrayList<>(datasource.values());
    }
    public E findById(K id) {
        return datasource.get(id);
    }
}

 

4. GenericDao๋ฅผ ์ƒ์†ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

SpringDataJPA๋ฅผ ์“ฐ๊ธฐ ์ „์— ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

AppleDaoExtendsGenericํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ”๋กœ GenericDao์— ํƒ€์ž…์„ ์ง€์ •ํ•œ ๋’ค ๊ฐ์ฒด ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

๊ตณ์ด AppleDaoExtendsGeneric๋ฅผ ์ƒ์„ฑํ•ด์•ผ ๋  ๋•Œ๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ๋•Œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

 

๐Ÿ” ๋Ÿฐํƒ€์ž„ ์ค‘์— ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์„๊นŒ?

Erasure๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด ์ปดํŒŒ์ผ๋œ ํŒŒ์ผ์—๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž… ์ •๋ณด๊ฐ€ ์—†๋‹ค๊ณ  ๋ฐฐ์› ๋‹ค.

-> ๋ฆฌํ”Œ๋ ‰์…˜์œผ๋กœ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

public GenericDao() {
        this.entityClass = (Class<E>) ((ParameterizedType)this.getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    }