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
package com.t4b.test.java.dp.sp.pp;
interface Printer {
void print();
}
Source code of ConsolePrinter.java
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);
}
}
Source code of ProxyPrinter.java
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();
}
}
Source code of TestMain.java
package com.t4b.test.java.dp.sp.pp;
public class TestMain {
public static void main(String[] args) {
Printer printer = new ProxyPrinter("test");
printer.print();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.