The example explains how to use proxy mode in Java design pattern programming _java

Source: Internet
Author: User

Definition:

Provide a surrogate or placeholder for another object to control access to it.

A proxy for other objects to control access to this object.

General Description:

Typically contains three roles: abstract topics, specific topics, and agent topics.

    1. Abstract topic: An abstract class or interface that is a common business type definition.
    2. Specific topic: The specific performer of the business logic
    3. Agent role: Responsible for the application of the real role, to limit all the methods defined by the abstract subject class to the true theme role implementation.

General class Diagram:

Common code:

Package Proxy;
Abstract Topic Class: Public
interface Subject {public
  void dosomething ();
}

Package Proxy;
Specific topic classes public class
Realsubject implements subject{
  @Override public
  void DoSomething () {
    SYSTEM.OUT.PRINTLN ("Business logic ...");
  }

Package Proxy;
Agent subject Class Public classes
proxy implements subject{
   
  Private Subject sub = null; 
   
  @Override public
  void DoSomething () {
    this.sub.doSomething ();   
  }
   
  Public Proxy (Subject sub) {
    this.sub = sub;
  }
   
   
}

Package Proxy;
Client public
class Client {public
  static void Main (string[] args) {
     
    Subject realsub = new Realsubject (); 
   subject proxy = new Proxy (realsub);
     
    Proxy.dosomething ();
  }


Advantages :

1. Clear Responsibilities

2. High expansion

Extension of Agent mode:

General Agent:

Specific topic class transparent to the high-level, in the agent subject class constructs the specific topic class

Code implementation:

Package generalproxy;

Public interface Subject {public void dosomething ();}
 
Package generalproxy;
   
  public class Realsubject implements subject{private String name = NULL;
  @Override public void DoSomething () {System.out.println (THIS.name + "is being represented, executing business logic ..."); Public Realsubject (Subject proxy,string name) throws exception{if (proxy = = null) {throw new Exception ("
    Cannot create a proxy object ");
    }else{this.name = name;
 
}} package Generalproxy;
     
  public class Proxy implements subject{private Subject realsub = null;
    Public Proxy (String name) {try {realsub = new Realsubject (this, name);
    catch (Exception e) {e.printstacktrace ();
  } public void DoSomething () {realsub.dosomething ();
 
}} package Generalproxy;        
    public class Client {public static void main (string[] args) {//General agent Subject proxy = new Proxy ("John");
  Proxy.dosomethaing ();

 }
   
}


Force Agent:

You must obtain an object for the proxy subject class by accessing the specific subject class, and then control access using the Proxy topic class

Code implementation:

Package mustproxy;
   
  Public interface Subject {public void dosomething ();
Public Subject getproxy ();
 
} package Mustproxy;
  public class Realsubject implements subject{private String name = NULL;
   
  Private Subject proxy = null;
    @Override public void dosomething () {if (IsProxy ()) {System.out.println (this.name + "is represented, executing business logic ...");
    }else{System.out.println ("Please visit the agent first ...");
  } public Realsubject (String name) {this.name = name;
    Public Subject GetProxy () {proxy = new proxy (this);
  return this.proxy;
    Private Boolean IsProxy () {if (This.proxy = = null) {return false;
    }else{return true;
 
}} package Mustproxy;
   
  public class Proxy implements subject{private Subject realsub = null;
  Public Proxy (Subject realsub) {this.realsub = realsub;
  public void DoSomething () {this.realSub.doSomething ();
 Public Subject GetProxy () {
    return this;
 
}} package Mustproxy;
    public class Client {public static void main (string[] args) {Subject realsub = new Realsubject ("John");
     
    Realsub.dosomething ();
    Subject proxy = Realsub.getproxy ();
  Proxy.dosomething ();

 }
}

Application Scenario
in the real world, the Secretary is equivalent to an agent, the boss meeting, then inform staff meeting time, decorate the venue, after the meeting and so on meetings and so on the relevant work can be entrusted to the Secretary to do, the boss just need to meet on the line, do not need to do Similarly, in our programming we can use proxy mode to decouple code that is composed of a series of unrelated logic, such as the log code in the business code can be done in the broker. Spring's AOP is a typical dynamic proxy application.


proxy mode application Form
(1) Remote proxy-You can hide the fact that an object exists in a different address space. Also enables clients to access objects on remote machines, the remote machine may have better computational performance and processing speed, and can quickly respond to and process client requests.
(2) Virtual proxy-allows objects with large memory overhead to be created when needed. We only create when we really need this object. The
(3) write-time replication agent (Copy-On-Write proxy) – Used to control the replication of objects by delaying the replication of the object until the customer really needs it. is a variant of the virtual agent.
(4) Protection agent-provides different levels of target object access for different customers
(5) Cache proxy-provides temporary storage for costly operations results, allowing multiple customers to share results, Protection To reduce computation or network latency.
(6) firewall agent (Firewall proxy)-Controls access to network resources and protects topics from malicious customers.
(7) Synchronization agent (Synchronizationproxy) – Provides secure access to topics in multi-threaded situations.
(8) Smart Referenceproxy-When an object is referenced, provide some extra action, such as logging the number of calls to this object.
(9) Complex hidden agents (complexity hidingproxy)-the complexity of hiding a complex set of classes, and access control. Sometimes called the appearance agent (Façade proxy), this is not difficult to understand. Complex hidden agents and skin patterns are not the same because the proxy controls access and the appearance pattern is different because the proxy controls access and the skin mode provides only another set of interfaces.
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.