-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAppConfig.java
More file actions
119 lines (100 loc) · 4.2 KB
/
AppConfig.java
File metadata and controls
119 lines (100 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package store;
import java.util.function.Consumer;
import store.controller.StoreController;
import store.model.repository.ProductRepository;
import store.model.repository.PromotionRepository;
import store.model.repository.StockRepository;
import store.model.repository.UserRepository;
import store.service.PurchaseService;
import store.service.RepositoryService;
import store.service.StoreInitializeService;
import store.service.UserService;
import store.utility.ErrorMessage;
import store.view.InputView;
import store.view.OutputView;
public class AppConfig {
private ProductRepository productRepository;
private PromotionRepository promotionRepository;
private StockRepository stockRepository;
private UserRepository userRepository;
private RepositoryService repositoryService;
private UserService userService;
private StoreInitializeService storeInitializeService;
private PurchaseService purchaseService;
private InputView inputView;
private OutputView outputView;
private StoreController storeController;
public MockSetting mock;
public AppConfig(){
setRepository();
setService();
setView();
setContoller();
mock = new MockSetting(this);
}
public StoreController getStoreController(){
return storeController;
}
public static class MockSetting {
public SingleVariable<InputView> InputView;
public SingleVariable<ProductRepository> ProductRepository;
public SingleVariable<PromotionRepository> PromotionRepository;
public SingleVariable<StockRepository> StockRepository;
public MockSetting(AppConfig parent){
this.InputView = new SingleVariable<>(v -> parent.inputView = v, parent::setContoller);
this.ProductRepository = new SingleVariable<>(v -> parent.productRepository = v, ()->{parent.setService(); parent.setContoller();});
this.StockRepository = new SingleVariable<>(v -> parent.stockRepository = v, ()->{parent.setService(); parent.setContoller();});
this.PromotionRepository = new SingleVariable<>(v -> parent.promotionRepository = v, ()->{parent.setService(); parent.setContoller();});
}
static class SingleVariable<T> implements Injectable {
private final Consumer<T> setter;
private final Refresh func;
public SingleVariable(Consumer<T> setter, Refresh func){
this.setter = setter;
this.func = func;
}
@Override
public void inject(Object mockedInstance){
try{
setter.accept((T) mockedInstance);
func.refresh();
} catch (Exception e){
throw new IllegalStateException(ErrorMessage.errorHeader+
String.format(ErrorMessage.PARAMETER_NOT_ACCEPTABLE.getMessage(),
"입력된 타입의 mockInstance",setter.getClass().getName()));
}
}
}
}
@FunctionalInterface
interface Injectable {
void inject(Object mockedInstance);
}
@FunctionalInterface
interface Refresh{
void refresh();
}
private void setRepository(){
productRepository = new ProductRepository();
promotionRepository = new PromotionRepository();
stockRepository = new StockRepository();
userRepository = new UserRepository();
}
private void setService(){
repositoryService = new RepositoryService(promotionRepository, productRepository, stockRepository);
userService = new UserService(userRepository);
storeInitializeService = new StoreInitializeService(repositoryService);
purchaseService = new PurchaseService(userService, repositoryService);
}
private void setView(){
inputView = new InputView();
outputView = new OutputView();
}
private void setContoller(){
storeController = new StoreController(inputView, outputView, storeInitializeService, purchaseService, repositoryService, userService);
}
//for debugging
// public void myInputView(){
// System.out.println("Injected InputView: " + inputView.myIdentity());
// }
}