ex. 결제 대행업체) 결제 결과 파일 제공 -> 결제 내역 유효한지 확인
public class PaySync {
// 의존 객체 직접 생성
**private PayInfoDao payInfoDao = new PayInfoDao();**
public void sync() throws IOException {
// 하드 코딩된 경로
**Path path = Paths.get("D:\\\\data\\\\pay\\\\cp0001.csv");**
List<PayInfo> payInfos = Files.lines(path)
.map(line -> {
String[] data = line.split(",");
PayInfo payInfo = new PayInfo(
data[0], data[1], Integer.parseInt(data[2])
);
return payInfo;
})
.collect(Collectors.toList());
payInfos.forEach(pi -> payInfoDao.insert(pi));
}
}
ex. AuthUtil 클래스의 정적 메서드 사용
public class LoginService {
private String authKey = "someKey";
private CustomerRepository custmerRepo;
public LoginService(CustomerRepository customerRepo) {
this.customerRepo = customerRepo;
}
public LoginResult login(String id, String pw) {
int resp = 0;
boolean authorized = **AuthUtil.authorized**(authKey);
if (authorized) {
resp = **AuthUtil.authenticate**(id, pw);
} else {
resp = -1;
}
if (resp == -1) return LoginResult.badAuthKey();
if (resp == 1) {
Customer c = customerRepo.findOne(id);
return LoginResult.authenticated(c);
} else {
return LoginResult.fail(resp);
}
}
}
ex. 특정 사용자의 포인트 계산
public class UserPointCalculator {
// 역할이 섞여 있는 코드
// 포인트 계산 자체는 두 DAO 필요 없고 Product만 필요 But, 설정해야 계산 가능
**private SubscriptionDao subscriptionDao;
private ProductDao productDao;**
public UserPointCalculator(SubscriptionDao subscriptionDao,ProductDao productDao) {
this.subscriptionDao = subscriptionDao;
this.productDao = productDao;
}
public int calculatePoint(User u) {
Subscription s = subscriptionDao.selectByUser(u.getId());
if (s == null) throw new NoSubscriptionException();
Product p = productDao.selectById(s.getProductId());
// 실행 시점에 따라 달라지는 결과
**LocalDate now = LocalDate.now();**
int point = 0;
if (**s.isFinished(now)**) {
point += p.getDefaultPoint();
} else {
point += p.getDefaultPoint() + 10;
}
if (s.getGrade() == GOLD) {
point += 100;
}
return point;
}
}