Here is a summary of the ** Proxy pattern ** in the GoF design pattern.
--The English word Proxy means ** proxy **. --In object-oriented programming, both "principal" and "agent" are objects. --The Proxy pattern is a method in which a proxy object does some work instead of a principal object that is too busy to work **. --The GoF design patterns are classified as ** structural design patterns **.
This is a "named printer" program that displays characters on the screen.
This interface is common to Printer and Printer Proxy.
Printable.java
public interface Printable {
//Name setting
public abstract void setPrinterName(String name);
//Get name
public abstract String getPrinterName();
//Character string display(print out)
public abstract void print(String string);
}
A class that represents a named printer. (Agent)
PrinterProxy.java
public class PrinterProxy implements Printable {
private String name;
private Printer real;
public PrinterProxy() {
}
public PrinterProxy(String name) {
this.name = name;
}
public synchronized void setPrinterName(String name) {
if (real != null) {
real.setPrinterName(name);
}
this.name = name;
}
public String getPrinterName() {
return name;
}
public void print(String string) {
realize();
real.print(string);
}
private synchronized void realize() {
if (real == null) {
real = new Printer(name);
}
}
}
A class that represents a named printer. (Person)
Printer.java
public class Printer implements Printable {
private String name;
public Printer() {
heavyJob("Creating an instance of Printer");
}
public Printer(String name) {
this.name = name;
heavyJob("Printer instance(" + name + ")Is being generated");
}
public void setPrinterName(String name) {
this.name = name;
}
public String getPrinterName() {
return name;
}
public void print(String string) {
System.out.println("=== " + name + " ===");
System.out.println(string);
}
private void heavyJob(String msg) {
System.out.print(msg);
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.print(".");
}
System.out.println("Done.");
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
Printable p = new PrinterProxy("Alice");
System.out.println("The name is now" + p.getPrinterName() + "is.");
p.setPrinterName("Bob");
System.out.println("The name is now" + p.getPrinterName() + "is.");
p.print("Hello, world.");
}
}
The name is now Alice.
The name is now Bob.
Printer instance(Bob)Is being generated.....Done.
=== Bob ===
Hello, world.
In the Proxy pattern, Proxy acts as a proxy and takes over as much as possible. In the sample program, by using the Proxy role, it was possible to delay heavy processing (instantiation) until the actual printing. For example, in a system where there are many functions that take a long time to initialize, if all the functions that are not used at the time of startup are initialized, it will take time to start the application. It is possible to put the delay function in the Printer class from the beginning instead of dividing it into the PrinterProxy class and the Printer class, but by separating the classes, the program becomes more componentized and the functions can be added individually. I can.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts