Structural Design Pattern: Proxy Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Structural Design Pattern: Proxy Pattern

Share This
In essence, a proxy is an entity that serves as a representation of another entity. The Proxy Pattern, as defined by the Gang of Four (GoF), is a design pattern that enables control over the access to the source object.

Various procedures can be executed, such as concealing the information of the original item and loading it when needed.

The Proxy pattern is alternatively referred to as the Surrogate or Placeholder pattern.

The RMI API employs the proxy design pattern. Stub and Skeleton are a pair of proxy objects utilized in RMI (Remote Method Invocation).

It offers safeguarding to the original entity from external influences.

Source code of Printer.java
1
2
3
4
5
6
7
package com.t4b.test.java.dp.sp.pp;
interface Printer {
void print();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of ConsolePrinter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.t4b.test.java.dp.sp.pp;
class ConsolePrinter implements Printer {
private String fileName;
public ConsolePrinter(String fileName) {
this.fileName = fileName;
}
@Override
public void print() {
System.out.println("Displaying " + fileName);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of ProxyPrinter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.t4b.test.java.dp.sp.pp;
class ProxyPrinter implements Printer {
private ConsolePrinter consolePrinter;
private String fileName;
public ProxyPrinter(String fileName) {
this.fileName = fileName;
}
@Override
public void print() {
if (consolePrinter == null) {
consolePrinter = new ConsolePrinter(fileName);
}
consolePrinter.print();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.t4b.test.java.dp.sp.pp;
public class TestMain {
public static void main(String[] args) {
Printer printer = new ProxyPrinter("test");
printer.print();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.