-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitorTest.java
More file actions
158 lines (134 loc) · 3.89 KB
/
VisitorTest.java
File metadata and controls
158 lines (134 loc) · 3.89 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
/*
A Behavioral Pattern.
The Visitor pattern uses an external class to act on data in other classes.
This is a useful approach to you when you have a polymorphic operation that
cannot reside in the class hierarchy. Visitor is also a useful way to extend
the behavior of a class hierarchy without the need to alter existing classes
or to implement the new behavior in every subclass that requires it.
*/
import java.util.ArrayList;
public class VisitorTest {
public static void main(String[] args){
System.out.println("-------------- VISITOR ---------------");
Auditor iso = new ISOAuditor("Aru Bag");
Auditor sei = new SEIAuditor("Nakul Bera");
ArrayList procs = new ArrayList();
String[] deliveryDocs = new String[]{"DeliveryRequest", "DeliverySchedule", "CustomerResponse"};
procs.add(new Delivery("SISL-Delivery-process", deliveryDocs));
String[] purchaseDocs = new String[]{"VendorRegister", "PurchaseRegister", "ChallanRegister"};
procs.add(new Purchase("SISL-Purchase-process", purchaseDocs));
Company sisl = new Company("SISL", procs);
sisl.invite(iso);
sisl.invite(sei);
}
}
// Abstract Visitor
abstract class Auditor {
String name;
public abstract void auditPurchase(CorporateProcess proc);
public abstract void auditDelivery(CorporateProcess proc);
};
// Concrete Visitor 1
class ISOAuditor extends Auditor {
ISOAuditor(String nm){
name = nm;
}
public void auditPurchase(CorporateProcess proc){
System.out.println(name+" is auditing "+proc.getName()+" on behalf of ISO.");
System.out.print("Checking purchase docs [");
String[] docs = proc.getDocs();
for (int i=0; i<docs.length; i++)
{
System.out.print(docs[i]+", ");
}
System.out.println("]\n");
}
public void auditDelivery(CorporateProcess proc){
System.out.println(name+" is auditing "+proc.getName()+" on behalf of ISO.");
System.out.print("Checking delivery docs [");
String[] docs = proc.getDocs();
for (int i=0; i<docs.length; i++)
{
System.out.print(docs[i]+", ");
}
System.out.println("]\n");
}
};
// Concrete Visitor 2
class SEIAuditor extends Auditor {
SEIAuditor(String nm){
name = nm;
}
public void auditPurchase(CorporateProcess proc){
System.out.println(name+" is auditing "+proc.getName()+" on behalf of SEI.");
System.out.print("Checking purchase docs [");
String[] docs = proc.getDocs();
for (int i=0; i<docs.length; i++)
{
System.out.print(docs[i]+", ");
}
System.out.println("]\n");
}
public void auditDelivery(CorporateProcess proc){
System.out.println(name+" is auditing "+proc.getName()+" on behalf of SEI.");
System.out.print("Checking delivery docs [");
String[] docs = proc.getDocs();
for (int i=0; i<docs.length; i++)
{
System.out.print(docs[i]+", ");
}
System.out.println("]\n");
}
};
// Abstract Element
abstract class CorporateProcess {
String name;
String[] docs;
public String getName(){
return name;
}
public String[] getDocs(){
return docs;
}
public abstract void accept(Auditor auditor);
};
// Concrete Element 1
class Delivery extends CorporateProcess {
Delivery(String nm, String[] arr){
name = nm;
docs = arr;
}
public void accept(Auditor auditor){
auditor.auditDelivery(this);
}
};
// Concrete Element 2
class Purchase extends CorporateProcess {
Purchase(String nm, String[] arr){
name = nm;
docs = arr;
}
public void accept(Auditor auditor){
auditor.auditPurchase(this);
}
};
// Object Structure
class Company {
String name;
ArrayList procs = new ArrayList();
Company(String nm, ArrayList p){
name = nm;
procs = p;
}
public String getName(){
return name;
}
public void invite(Auditor auditor){
CorporateProcess proc;
for (int i=0; i<procs.size(); i++)
{
proc = (CorporateProcess)procs.get(i);
proc.accept(auditor);
}
}
};