Latest web development tutorials

Proxy mode

In proxy mode (Proxy Pattern), a class of functions on behalf of another class. This type of design patterns belong to structural model.

In proxy mode, we create an object with an existing object in order to provide a functional interface to the outside world.

Introduction

Intent: to provide an agent to control access to this object to other objects.

Main Resolution: direct access to the object caused problems, such as: the object you want to access on the remote machine.In object-oriented systems, some objects for some reason (such as object creation is expensive, or certain actions require security controls, or access outside the process), it will give users direct access to system configuration or a lot of trouble, we can add a layer to access this object at the time of access to this object.

When to use: when you want to do something to control access to a class.

How to fix: add intermediate layer.

The key code: link with the proxy class combinations.

Application examples: 1, Windows shortcuts inside.2, the pig went to high Cuilan result Monkey change, can be understood: the high Cuilan appearance abstracted, high Turquoise Blue I and Monkey implements this interface, Pig access high Cuilan when not see the Monkey King, so that the Monkey King is a high Cuilan proxy class. 3, not necessarily to buy train tickets at the station to buy, you can also go to the outlets. 4, a check or a bank certificate of deposit is a proxy account funds. Check transactions in the market is used instead of cash, and provides the issuer's capital account control. 5, spring aop.

Advantages: 1, clear responsibilities.2, high scalability. 3, intelligent.

Disadvantages: 1, as between the client and the real theme that the proxy object, so some type of proxy mode may result in slower processing speed request.2, to achieve proxy mode requires additional work to achieve some proxy mode is very complex.

Usage scenarios: according to divide responsibilities, usually have the following usage scenarios: 1, Remote Agent.2. Alerts. 3, Copy-on-Write agents. 4, the protection (Protect or Access) agent. 5, Cache Agent. 6, firewall (Firewall) agent. 7, synchronization (Synchronization) agent. 8, intelligent references (Smart Reference) agent.

Note: The difference between 1 and adapter modes: Adapter pattern major changes contemplated object's interface, and proxy mode can not be changed by agents of the class interface.The difference between 2 and decorator patterns: Decorator To enhance functionality, and proxy mode is to be controlled.

achieve

We will create anImageinterface and implementation of the entity classesImageinterface.ProxyImageis a proxy class, reducing memory footprintRealImageloaded objects.

ProxyPatternDemo,we demonstrate the use ofProxyImageclass to getan Imageobject to be loaded and displayed on demand.

Proxy pattern UML diagram

step 1

Create an interface.

Image.java

public interface Image {
   void display ();
}

Step 2

Create entity class that implements the interface.

RealImage.java

public class RealImage implements Image {

   private String fileName;

   public RealImage (String fileName) {
      this.fileName = fileName;
      loadFromDisk (fileName);
   }

   @Override
   public void display () {
      System.out.println ( "Displaying" + fileName);
   }

   private void loadFromDisk (String fileName) {
      System.out.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, useProxyImageto getRealImageclass object.

ProxyPatternDemo.java

public class ProxyPatternDemo {
	
   public static void main (String [] args) {
      Image image = new ProxyImage ( "test_10mb.jpg");

      // Load the image from disk image.display (); 
      System.out.println ( "");
      // Images can not be loaded from disk image.display (); 	
   }
}

Step 4

Verify output.

Loading test_10mb.jpg
Displaying test_10mb.jpg

Displaying test_10mb.jpg