-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProxyDemo1.java
74 lines (61 loc) · 1.61 KB
/
ProxyDemo1.java
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
package Basics;
// https://www.youtube.com/watch?v=hNHutM_k7EM&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=354
interface Network {
// attr
// method
void browse();
}
/**
* Design pattern : Proxy
*
* <p>1) use proxy class instead of the actual "true" class 2) using case: - safety -> prevent
* actual class to be visited directly - remote proxy -> RMI - delay loading -> load the proxy class
* first, if needed, load the actual class
*
* <p>3) Types - static proxy (this example) - dynamic proxy -> "java reflection", we will cover
* this later
*/
public class ProxyDemo1 {
public static void main(String[] args) {
// run
Server server = new Server();
// polymorphism (put server into public ProxyServer(Network work))
ProxyServer proxyServer = new ProxyServer(server);
// or, can use below
// ProxyServer proxyServer = new ProxyServer(new Server());
/**
* NOTE : we're not explicitly using Server class below, but we run browse method via "proxy"
* (e.g. proxyServer) class
*/
proxyServer.browse();
}
}
/** proxied class */
class Server implements Network {
// attr
// constructor
// method
@Override
public void browse() {
System.out.println("Server browse");
}
}
/** proxy class */
class ProxyServer implements Network {
// attr
private final Network work;
// constructor
public ProxyServer(Network work) {
this.work = work;
}
// method
public void check() {
System.out.println("Prepare before connect to internet ...");
}
@Override
public void browse() {
check();
// System.out.println();
work.browse();
}
}