TIL(Today I Learned)

TIL 54th day

keepgoing 2021. 12. 16. 15:24

비즈니스 로직 작성 Tip

Data를 가지고 있는 class에 business logic을 설계하는것이 응집도에 좋다.

Exception class 작성 Tip

에러 메시지와 원인을 파악하도록 Override Method를 작성한다.
ex)

//NotEnoughStockException

public NotEnoughStockException() {
        super();
    }

    public NotEnoughStockException(String message) {
        super(message);
    }

    public NotEnoughStockException(String message, Throwable cause) {
        super(message, cause);
    }

    public NotEnoughStockException(Throwable cause) {
        super(cause);
    }

데이터 변경 시 비즈니스 로직 작성

setter를 이용해서 변경하는것이 아닌, 핵심 비즈니스 로직을 작성해서 데이터를 변경하는것이 바람직하다.

ex)

//Item

    public void addStock(int quantity){
        this.stockQuantity += quantity;
    }

    public void removeStock(int quantity){
        int restStock = this.stockQuantity = quantity;
        if(restStock < 0){
            throw new NotEnoughStockException("need more stock");
        }
        this.stockQuantity = restStock;
    }