[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item26 ์™„๋ฒฝ๊ณต๋žต. GenericRepository

2023. 3. 15. 13:52ใ†JAVA/Effective JAVA

728x90

 

item26. ๋กœ ํƒ€์ž…์€ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ.

Generic Dao ๋งŒ๋“ค๊ธฐ 

 

Generic์„ ํ™œ์šฉํ•˜๋ฉด ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๊ฐ•์˜์‹œ๊ฐ„์— ๊ฐ„๋‹จํ•œ Repository๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. 

 

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 Message implements Entity{
    private Long id;
    private String body;

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

    public String getBody() {
        return body;
    }
}

๊ฐ๊ฐ Entity๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์žˆ๊ณ , 

 

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 MessageRepository {
    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);
    }
}

๊ฐ์ฒด๋งˆ๋‹ค ๊ฐ๊ฐ Repository๊ฐ€ ์žˆ๋‹ค๊ณ  ๋ณด๋ฉด ์ง€๊ธˆ Repository์˜ ์ฝ”๋“œ๊ฐ€ ๊ฐ์ฒด๊ฐ€ ๋‹ฌ๋ผ์ง„๊ฒƒ ๋นผ๊ณ ๋Š” ์™„์ „ ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋Ÿฐ ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ Generic์„ ํ™œ์šฉํ•˜๋ฉด ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

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 e) {
        entities.add(e);
    }


}

E์˜ ํƒ€์ž…์„ Entity๋ฅผ ์ƒ์†๋ฐ›์€ ๊ฐ์ฒด๋กœ ํ•œ์ •ํ•˜๊ณ , ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

public class AccountRepository  extends GenericRepository<Account>{
//    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);
//    }
}

๊ทธ๋Ÿฌ๋ฉด AccountRepository์— ์žˆ๋˜ ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๊ณ  GenericRepository๋ฅผ ์ƒ์†๋ฐ›๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค! 

 

@Test
void findById() {
    AccountRepository accountRepository = new AccountRepository();
    Account account = new Account(1L, "whiteShip");
    accountRepository.add(account);

    Optional<Account> byId = accountRepository.findById(1L);
    Assertions.assertTrue(byId.isPresent());
}

์ด ํ…Œ์ŠคํŠธ๋ฅผ ์ œ๋„ค๋ฆญ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋„ ๋Œ๋ ค๋ณด๊ณ , ์ œ๋„ค๋ฆญ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์†๋ฐ›์€ ํ›„์—๋„ ๋Œ๋ ค๋ณด๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

โœ๏ธ ์ •๋ฆฌ 

Generic ํƒ€์ž…์„ ๊ณต๋ถ€ํ•˜๊ณ  ํ•ญ์ƒ ๋‚ด๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด๋ณธ ์ ์€ ๊ฑฐ์˜ ๋“œ๋ฌธ?๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋ฒˆ ๊ฐ•์˜ ์‹œ๊ฐ„์„ ํ†ตํ•ด์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•˜๋ฉด ๋˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋‹ค. 

์ด๋ ‡๊ฒŒ ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ๋‚ด๊ฐ€ ํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—๋„ ๋งŽ์€๋ฐ ์ด๊ฑด ๋‹น์žฅ ์ ์šฉํ•ด๋ณด๊ณ  ์‹ถ๋‹ค!!

์ œ๋„ค๋ฆญ์„ ํ™œ์šฉํ•˜๋ฉด ์•ž์œผ๋กœ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€๋œ๋‹ค๊ณ  ํ•ด์„œ ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋˜ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ  ๊ทธ๋ƒฅ extends GenericRepository<>๋ฅผ ํ•ด์ฃผ๋ฉด ๋˜๋‹ˆ๊นŒ ํ›จ์”ฌ ํŽธ๋ฆฌํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค ใ…Žใ…Ž

 

 

 

 

 

 

 

 

 

 

 

728x90