Don't Repeat Yourself, 중복을 최소화하여 소프트웨어의 유지보수성을 향상시키는 핵심 원칙. 코드의 중복을 줄이고 유지보수성을 향상시킴
- 매장 존재하는지 확인
- 제품 존재하는지 확인
- 매장의 제품 존재하는지 확인
CommonService
로 추출하는 리팩토링을 진행하였다Base Class: CommonService
- extends: ProductService
- extends: StoreService
- extends: StoreProductService
- extends: SalesHistoryService
:
@Service
public class CommonService {
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private StoreRepository storeRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private StoreProductRepository storeProductRepository;
protected Store findStoreByCode(String storeCode) {
Store store = storeRepository.findByStoreCode(storeCode);
if (store == null) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
"STORE NOT FOUND"
);
}
return store;
}
protected Product findProductByCode(String productCode) {
Product product = productRepository.findByProductCode(productCode);
if (product == null) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
"PRODUCT NOT FOUND"
);
}
return product;
}
// 추가 메서드
@Service
public class SalesHistoryService extends CommonService {
@Autowired
private StoreProductRepository storeProductRepository;
@Autowired
private SalesHistoryRepository salesHistoryRepository;
public void registerSalesHistory(
String storeCode,
String productCode,
SalesHistoryInput salesHistoryInput
) {
Store store = findStoreByCode(storeCode);
Product product = findProductByCode(productCode);
StoreProduct previousStoreProduct = findStoreProduct(store, product);
// 추가 서비스로직
멋진 리팩토링이에요!