-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateMethodTest.java
More file actions
159 lines (139 loc) · 3.73 KB
/
TemplateMethodTest.java
File metadata and controls
159 lines (139 loc) · 3.73 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
A Behavioral Pattern.
Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. Template Method lets subclasses redefine certain
steps of an algorithm without changing the algorithm's structure.
*/
public class TemplateMethodTest {
public static void main(String[] args){
System.out.println("-------------- TEMPLATE_METHOD ---------------");
DataAccessObject customer = new CustomerDAO();
DataAccessObject order = new OrderDAO();
// customer.save(DataAccessObject.INSERT);
System.out.println("\nLoading customer...");
customer.load();
System.out.println("\nSaving customer...");
customer.save(DataAccessObject.UPDATE);
System.out.println("\nSaving customer...");
customer.save(DataAccessObject.DELETE);
// order.save(DataAccessObject.INSERT);
System.out.println("\nLoading order...");
order.load();
System.out.println("\nSaving order...");
order.save(DataAccessObject.UPDATE);
System.out.println("\nSaving order...");
order.save(DataAccessObject.DELETE);
}
}
// Abstract Class
abstract class DataAccessObject {
public static final int INSERT = 1;
public static final int UPDATE = 2;
public static final int DELETE = 3;
public abstract boolean connect();
public abstract boolean exists();
public abstract String[] select();
protected abstract void assignValues();
public abstract boolean insert();
public abstract boolean update();
public abstract boolean delete();
public abstract void disconnect();
public boolean load(){
boolean success = false;
if(connect()){
if(select() != null){
assignValues();
success = true;
}
disconnect();
}
return success;
}
public boolean save(int type){
boolean success = false;
if(connect()){
switch(type){
case INSERT:
if(!exists()){
if(insert()){
success = true;
}
}
break;
case UPDATE:
if(exists()){
if(update()){
success = true;
}
}
break;
case DELETE:
if(exists()){
if(delete()){
success = true;
}
}
break;
default:
disconnect();
}
}
return success;
}
};
// Concrete Class 1
class CustomerDAO extends DataAccessObject {
public boolean connect(){
System.out.println("Conneted to OrderSupply database for customer.");
return true;
}
public boolean exists(){return true;}
public void assignValues(){}
public String[] select(){
System.out.println("Selected customer from database.");
return new String[]{"cust0021", "Partha"};
}
public boolean insert(){
System.out.println("Inserted customer.");
return true;
}
public boolean update(){
System.out.println("Updated customer.");
return true;
}
public boolean delete(){
System.out.println("Deleted customer.");
return true;
}
public void disconnect(){
System.out.println("Disconnected from OrderSupply database.");
}
};
// Concrete Class 2
class OrderDAO extends DataAccessObject {
public boolean connect(){
System.out.println("Conneted to OrderSupply database for order.");
return true;
}
public boolean exists(){return true;}
public void assignValues(){}
public String[] select(){
System.out.println("Selected order from database.");
return new String[]{"ord0001", "cust0021", "1229.00"};
}
public boolean insert(){
System.out.println("Inserted order.");
return true;
}
public boolean update(){
System.out.println("Updated order.");
return true;
}
public boolean delete(){
System.out.println("Deleted order.");
return true;
}
public void disconnect(){
System.out.println("Disconnected from OrderSupply database.");
}
};