tonykang22 / study

0 stars 0 forks source link

[이펙티브 자바] 아이템 26. 로 타입은 사용하지 말라. #148

Open tonykang22 opened 1 year ago

tonykang22 commented 1 year ago

아이템 26. 로 타입은 사용하지 말라.

핵심 정리: 용어 정리



핵심 정리

매개변수화 타입을 사용해야 하는 이유



예시

컴파일 타임에 문제를 찾을 수 있다.

public class GenericBasic {

    public static void main(String[] args) {
//         Generic 사용하기 전
        List numbers = new ArrayList();
        numbers.add(10);
        numbers.add("tony");

//         로 타입 사용 시, Object / 형변환도 필요
        for (Object number: numbers) {
            System.out.println((Integer)number);
        }

//         Generic 등장 이후
        List<Integer> nuberms = new ArrayList<>();
        nuberms.add(10);
//         컴파일 에러
        nuberms.add("tony");

//         제네릭 사용 시, 지정한 타입 / 형변환 불필요
        for (Integer number: nuberms) {
            System.out.println(number);
        }
    }
}



제네릭 클래스

public class Box<E> {

    private E item;

    private void add(E e) {
        this.item = e;
    }

    private E get() {
        return this.item;
    }

    public static void main(String[] args) {
        Box<Integer> box = new Box<>();
        box.add(10);
        System.out.println(box.get() * 100);

        printBox(box);
    }

    private static void printBox(Box<?> box) {
        System.out.println(box.get());
    }

}



자바는 왜 로 타입을 지원하는가?

public class Box<E> {

    private E item;

    private void add(E e) {
        this.item = e;
    }

    private E get() {
        return this.item;
    }

    public static void main(String[] args) {
        Box<Integer> box = new Box<>();
        box.add(10);
        System.out.println(box.get() * 100);

        printBox(box);
    }

    private static void printBox(Box<?> box) {
        System.out.println(box.get());
    }

}
image



tonykang22 commented 1 year ago

완벽 공략



완벽 공략 40. GenericRepository

자바 Generic을 활용한 중복 코드 제거 예제

public interface Entity {

    Long getId();
}
public class Account implements Entity {

    private Long id;

    private String username;

    public Account(Long id, String username) {
        this.id = id;
        this.username = username;
    }

    @Override
    public Long getId() {
        return this.id;
    }

    public String getUsername() {
        return username;
    }
}
public class AccountRepository {

    private Set<Account> accounts;

    public AccountRepository() {
        this.accounts = new HashSet<>();
    }

    public Optional<Account> findById(Long id) {
        return accounts.stream().filter(a -> a.getId().equals(id)).findAny();
    }

    public void add(Account account) {
        this.accounts.add(account);
    }
}
public class Message implements Entity {

    private Long id;

    private String body;

    @Override
    public Long getId() {
        return id;
    }

    public String getBody() {
        return body;
    }
}
public class MessageRepository {

    private Set<Message> messages;

    public MessageRepository() {
        this.messages = new HashSet<>();
    }

    public Optional<Message> findById(Long id) {
        return messages.stream().filter(a -> a.getId().equals(id)).findAny();
    }

    public void add(Message message) {
        this.messages.add(message);
    }
}



public class GenericRepository<E extends Entity> {

    private Set<E> entities;

    public GenericRepository() {
        this.entities = new HashSet<>();
    }

    public Optional<E> findById(Long id) {
        return entities.stream().filter(a -> a.getId().equals(id)).findAny();
    }

    public void add(E entity) {
        this.entities.add(entity);
    }

}
public class AccountRepository extends GenericRepository<Account> {
}