Skip to content

Commit 09c6197

Browse files
committed
Bonus example of Proxy doctor for Proxy pattern
1 parent 6deb6b9 commit 09c6197

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.premaseem.proxy.proxyDoctor;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientForProxyDoctor {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("Welcome to Proxy Doctor Hospital ");
10+
11+
Doctor doctor = new ProxyDoctor();
12+
Scanner scan = new Scanner(System.in);
13+
int repeatRunFlag = 1;
14+
while (repeatRunFlag == 1) {
15+
System.out.println("What do you want from doctor ");
16+
System.out.println(" Press 1 for general checkup");
17+
System.out.println(" Press 2 for Surgery and operation ");
18+
System.out.println(" Press 3 for testing report and surgery ");
19+
20+
int type = scan.nextInt();
21+
22+
switch (type) {
23+
case 1:
24+
doctor.doGeneralCheckup();
25+
break;
26+
case 2:
27+
doctor.doOperationAndSurgery();
28+
break;
29+
case 3:
30+
doctor.provideSummaryandReport();
31+
break;
32+
}
33+
34+
System.out.println("=============================");
35+
36+
System.out.println("Press 1 for further treatment and 0 for EXIT .... ");
37+
try {
38+
repeatRunFlag = scan.nextInt();
39+
} catch (Exception e) {
40+
repeatRunFlag = 0;
41+
}
42+
43+
}
44+
45+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
46+
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.premaseem.proxy.proxyDoctor;
2+
3+
public abstract class Doctor {
4+
5+
void doGeneralCheckup() {
6+
new HospitalService().callNurse();
7+
}
8+
9+
abstract void doOperationAndSurgery();
10+
11+
void provideSummaryandReport() {
12+
new HospitalService().generateSummaryRepor();
13+
}
14+
}
15+
16+
class RealDoctor extends Doctor {
17+
18+
@Override
19+
void doOperationAndSurgery() {
20+
System.out.println("Real doctor doing operation");
21+
22+
}
23+
24+
}
25+
26+
class ProxyDoctor extends Doctor {
27+
Doctor realDoctor = null;
28+
29+
void doGeneralCheckup() {
30+
if (realDoctor != null) {
31+
realDoctor.doGeneralCheckup();
32+
} else {
33+
new HospitalService().callNurse();
34+
}
35+
}
36+
37+
@Override
38+
void doOperationAndSurgery() {
39+
System.out.println("Since proxy doctor cannot handle this operation he is invoking real doctor ");
40+
realDoctor = new RealDoctor();
41+
realDoctor.doOperationAndSurgery();
42+
}
43+
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.premaseem.proxy.proxyDoctor;
2+
3+
public class HospitalService {
4+
5+
public void callNurse() {
6+
System.out.println("Doing the basic check up and like BP, breathing etc");
7+
}
8+
9+
public void generateSummaryRepor(){
10+
System.out.println("Call pathology and generate reports ");
11+
}
12+
13+
}

0 commit comments

Comments
 (0)