-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory_Pattern
More file actions
66 lines (60 loc) · 1.38 KB
/
Factory_Pattern
File metadata and controls
66 lines (60 loc) · 1.38 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
public class Order_Team {
bool validate(Address a) {
return Factory.getInstance().getValidator(a.country).validate(a);
}
}
public class Factory {
static Factory f;
static map<String,FactoryValidators> ctov;
private Factory() {
ctov.put("US",new USval());
ctov.put("IN",new INval());
}
public static Factory getInstance() {
if(f==NULL) {
f = new Factory();
}
return f;
}
public FactoryValidator getValidator(String C) {
if(ctov.containsKey(C)) {
return ctov.get(C);
}
else {
throw an exception("Country Code Not Found");
}
}
}
Interface FactoryValidators{
public static bool validate(Address a);
}
public class INval implements FactoryValidators{
// code to validate indian addresses
public static bool Validate(Address a) {
return ( ZipValid.validate(a.zipcode) && CountryValid.validate(a.country)&& CityValid.validate(a.city) );
}
}
public class USval implements FactoryValidators{
// code to validate us addresses
}
class ZipValid {
public static bool validate(String zip) {
Set<String> s=RI.RIinstance().zipcodes;
//check
//return 1/0
}
}
class RI {
static Set<String> zipcodes;
static RI r;
private RI() {
//create a connection with db
//db query -> close the connection
//initialize the list //this.zipcodes= fetch from db
}
public static RI RIinstance() {
if(r==NULL)
RI r=new RI();
return r;
}
}