Design pattern ~ Proxy ~

1.First of all

Here is a summary of the ** Proxy pattern ** in the GoF design pattern.

2. What is the Proxy 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 **.

3. Sample class diagram

Proxy.PNG

4. Sample program

This is a "named printer" program that displays characters on the screen.

4-1. Printable interface

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);
}

4-2. PrinterProxy class

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);
		}
	}
}

4-3. Printer class

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.");
	}
}

4-4. Main class

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.");
	}
}

4-5. Execution result

The name is now Alice.
The name is now Bob.
Printer instance(Bob)Is being generated.....Done.
=== Bob ===
Hello, world.

5. Benefits

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.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Proxy ~
Proxy Pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"