-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathproblem.txt
77 lines (76 loc) · 1.58 KB
/
problem.txt
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
// *******. s1 ********************************
public static void ShowUI(Account account) {
Dialog dlg=null;
if(account instanceof SA) {
dlg = new SADialog();
}
if(account instanceof CA) {
dlg = new CADialog();
}
if(dlg!=null) {
dlg.Display();
}
} (reflection,cyclomatic complexity)
// *******. s2 ********************************
interface Account{
...
Dialog getDialog();
}
public static void ShowUI(Account account) {
Dialog dlg= account.getDialog();
dlg.Display();
} (SRP, Coupling)
// *******. s3 ********************************
class Factory{
Dialog getDialog(CA ca){
return new CADialog();
}
Dialog getDialog(CB cb){
return new CBDialog();
}
}
public static void ShowUI(Account account) {
Factory f = new Factory();
Dialog dlg= f.getDialog(account);
dlg.Display();
}
// *******. s4 ********************************
interface Plugin{
void do(SA sa);
void do(CA ca);
}
interface Account{
void withdraw();
void deposit();
void invoke(Plugin plugin);
}
class SA implements Account{
void withdraw(){}
void deposit(){}
void invoke(Plugin plugin){
plugin.do(this);
}
}
class CA implements Account{
void withdraw(){}
void deposit(){}
void invoke(Plugin plugin){
plugin.do(this);
}
}
-----------------------------------------
class Factory implements Plugin{
Dialog res;
void do(CA ca){
res= new CADialog();
}
void do(SA sa){
res= new SADialog();
}
}
public static void ShowUI(Account account) {
Factory f = new Factory();
//f.do(account); <-- will not work "base pointer"
account.invoke(f);
f.res.Display();
}