Proxy Design Pattern


Proxy mode

In Proxy Pattern, a class represents the functionality of another class. This type of design pattern is a structured pattern.

In proxy mode, we create objects with existing objects to provide functional interfaces to the outside world.

Introduction

Intent: Provides a proxy for other objects to control access to this object.

Main solution: Problems that arise when accessing objects directly, such as: The object to be accessed is on a remote machine. In object-oriented systems, some objects cause a lot of trouble for users or system structures for some reasons (such as high object creation overhead, or some operations require security control, or need to access outside the process). We can add an access layer to this object when accessing this object.

When to use: want to do some control when accessing a class.

How to fix: Add the middle layer.

Key Code: The implementation is combined with the proxy class.

Application example: 1. Shortcuts in Windows. 2, pigs go to find Gao Cuilan results are Sun Wukong change, you can understand this: abstract the appearance of Gao Cuilan, Gao Cuilan and Sun Wukong have realized this interface, when the pigs go to Gao Cuilan, they can't see this is Sun Wukong, so that Sun Wukong is Gao Cuilan Proxy class. 3, buy a train ticket is not necessarily bought at the train station, you can also go to the point of sale. 4. A check or bank deposit slip is an agent for funds in the account. Cheques are used in place of cash in market transactions and provide control over funds on the issuer's account. 5, spring aop.

Advantages: 1. Clear responsibilities. 2, high scalability. 3. Intelligent.

Disadvantages: 1. Due to the addition of proxy objects between the client and the real theme, some types of proxy mode may cause the request to be processed slower. 2. Implementing the proxy mode requires extra work, and the implementation of some proxy modes is very complicated.

Usage scenarios: are divided by responsibilities, usually with the following usage scenarios: 1, remote agent. 2. Virtual agent. 3. Copy-on-Write proxy. 4, Protect (Protect or Access) agent. 5, Cache agent. 6, firewall (Firewall) agent. 7, Synchronization (Synchronization) agent. 8, Smart Reference (Smart Reference) agent.

Note: 1, the difference between the adapter mode: the adapter mode mainly changes the interface of the object under consideration, and the proxy mode cannot change the interface of the proxy class. 2, and the difference between the decorator mode: the decorator mode to enhance the function, and the proxy mode is to control.

Implementation

We will create an Image interface and an entity class that implements the Image interface. ProxyImage is a proxy class that reduces the memory footprint of RealImage objects.

ProxyPatternDemo, our demo class uses ProxyImage to get the Image object to load and display it as needed.

proxy_pattern_uml_diagram

Step 1

Create an interface.

Image.java

public interface Image { void display(); }

Step 2

Create an entity class that implements the interface.

RealImage.java

public class RealImage implements Image { private String fileName; public RealImage(String fileName){ this.fileName< Span class="hl-code"> = fileName; loadFromDisk(fileName< Span class="hl-brackets">); } @Override public void display() { System.out< Span class="hl-code">.println("Displaying " + fileName); } private void loadFromDisk(String fileName){ System.out< Span class="hl-code">.println("Loading " + fileName); } }

ProxyImage.java

public class ProxyImage implements Image{ private RealImage realImage; private String fileName; public ProxyImage(String fileName){ this.fileName = fileName; } @Override public void display() { if(realImage == null){ realImage = new RealImage(fileName); } realImage.display(); } }

Step 3

When requested, use ProxyImage to get the object of the RealImage class.

ProxyPatternDemo.java

public class ProxyPatternDemo { public static void main(String[] args) { Image image = new ProxyImage("test_10mb.jpg "); // Images will be loaded from disk< /span> image.display< Span class="hl-brackets">(); System.out< Span class="hl-code">.println(""); // Images do not need to be loaded from disk image.display< Span class="hl-brackets">(); } }

Step 4

Executing the program, outputting results:

  Loading   test_10mb  .   jpg
  display   test_10mb  .   jpg

  display   test_10mb  .   jpg  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition