I have to tell the story with the proxy model

Source: Internet
Author: User

Summary: proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.

the proxy mode is classified as three types of roles :

abstract Role (Subject): A business method that declares the real role implementation through an interface or abstract class. Agent Role (Proxy):is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations. Also called the delegate class, the proxy class, it is responsible for the real role of the application, the definition of all the abstract topic class methods to delegate to the real theme role implementation, and before and after the real topic role processing completed pre-processing and aftercare work. Real characters (realsubject): Implements an abstract role that defines the business logic to be implemented by the real role for invocation by the proxy role. also called the delegated role, the role of the agent, it is generally very busy, is the business logic of the specific performer. recently nothing likes to play lol, but now elementary school holidays are always abused, even if super God Five Kill Sword Saint also can't save the world, cause I fell from Gold 2 to Silver 1, no way, I only go to find leveling help me on points.


The whole realsubject is a normal business implementation class, the core of proxy mode is on the proxy class, see
The above is the simplest kind of proxy mode, because my segment has been dropped, and then fall may be bronze 5, the fact that I have not been suitable for the operation of my account, so I went to find a leveling, then this time he is the real game operator, and I just his agent, the real work is leveling, I don't have to worry about how he's going to manipulate my character, I just need to focus on the results. But I have another condition, because I want to be in the holiday with my friend when black, can dazzle my Dan, so I here to leveling a time, that is, must be in the one-week class to complete. OK, then we put this time conditions in which place, if placed in the real role is my leveling class inside, then the next time he in the order, is not to modify the time, so this is unreasonable, we can put this constraint in the proxy class inside to achieve.. The other advantage of the proxy class here is thatin order to add some other operations to the real target class, and the details of these operations are given to the agent for processing, you only need to centralize the implementation of the target class functionality.
<span style= "color: #ff0000;" >p</span><span style= "color: #333333;" >ublic class Proxy implements subject{//to proxy which implementation class private Subject Subject = null;//Default proxy public proxy () {this.subject= New Realsubject ();} Pass the proxy public proxy (Subject Subject) {this.subject=subject;} through the constructor Implementation method public void combat () {begin (); This.subject.combat (); End ();} Start time public void Begin () {System.out.println ("2015-8-17");} End time public void End () {System.out.println ("2015-8-23");}} </span>


I now have a problem, that is, in the row, especially in the low-sectional area, there are a lot of people who like to grab positions, if you are not lucky in the 4, 5 floor, then it is difficult to get your own favorite position, such as you a proficient ADC leveling, to play on the single, then may be blown, Since it's leveling, you'll definitely think of it. So they certainly more than one person, but a studio, there may be several people, they are proficient in the location is not the same, we all know play a place to play for a long time, and then to play other places, it will certainly be a little rusty. So we have to find the corresponding leveling according to the position assigned to us by the system. So this leveling is dynamic, that is to say our real role is dynamic, then we have to use our dynamic agent.
/*/* Focus on playing wild leveling */public class Junglesubject implements Subject{public void Combat () {System.out.println ("My Sword Is Your Sword");}}

/*/* Focus on ADC leveling */public class Adcsubject implements Subject{public void Combat () {System.out.println ("Welcome to De Levin League");}
/*/* Focus on the order of leveling */public class Topsubject implements Subject{public void Combat () {System.out.println ("True will is not weakened");}}
Above we generated three leveling theme classes, the core of the dynamic proxy class is the proxy class, how can let him to the dynamic agent of these roles;
public class proxy  implements invocationhandler{//to proxy which implementation class private Object subject = Null;public Proxy (object subject ) {super (); this.subject = subject;} Public Proxy () {};p Ublic object Invoke (Object Proxy, Method method, object[] args) throws Throwable {System.out.println (" Before calling "+ method); Object Result=null;result=method.invoke (subject, args); System.out.println ("After calling" + method); return result;} public static object factory (object obj)     {         Class cls = Obj.getclass ();         The following proxy is the JDK's own, the core class of the dynamic Agent         return Java.lang.reflect.Proxy.newProxyInstance (Cls.getclassloader (), Cls.getinterfaces (), New Proxy (obj));}    }
public class Test {public static void main (string[] args) {Subject Subject = new Topsubject ();//Specify the order leveling real role Subject ds = ( Subject) proxy.factory (Subject); Initialize the proxy class Ds.combat ();}}


Dynamic Agent Description:The Java Dynamic proxy class is located under the Java.lang.reflect package, which generally involves the following two classes:
(1). Interface Invocationhandler: Only one method Object:invoke (Object Obj,method method, object[] args) is defined in this interface. When actually used, the first parameter, obj, generally refers to the proxy class, which method is the proxy, as in the previous example, the request (), args is the parameter array for the method. This abstract method is implemented dynamically in the proxy class.
(2). Proxy: This class is a dynamic proxy class that acts like the proxysubject in the previous example, which mainly includes the following:
Protected Proxy (Invocationhandler H): constructor, estimated to be used to assign values to internal H.
Static class Getproxyclass (ClassLoader loader, class[] interfaces): Gets a proxy class, where loader is a class loader, and interfaces is an array of all the interfaces owned by the real class.
Static Object newproxyinstance (ClassLoader loader, class[] interfaces, Invocationhandler h): Returns an instance of the proxy class, The returned proxy class can be used as a proxy class (you can use a method declared in the subject interface of the proxy class).
in the test class, New Topsubject (), creates a leveling "real role", passes the factory method Proxy.factory () to the broker role, and initializes the "call processor"-the class that implements the Invocationhandler. and returns a dynamically created proxy class instance, as "proxy role" also inevitably implements the "abstract role" provided by the business logic method, it can be transformed upward to subject (), and assigned to the subject type of reference DS.
newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) method is used by programmers to specify parameters to dynamically return the required proxy class, while in The Voke (Object Proxy, Method method, object[] args) method is called dynamically by the JVM at run time. When executing "ds.combat ();" method, the JVM dynamically assigns a "call processor", passes the arguments to the outer invoke, and calls Method.invoke (Obj,args) to actually execute!

proxy.factory static methods are used to dynamically generate proxy classes ("proxy roles") that dynamically generate proxy roles at run time. The "Abstract role", "Proxy role", and the call processor (the class that implements the Invocationhandler interface) can all be changed, so the dynamic agent of Java is very powerful.

Application form of proxy mode

(1) Remote proxy-You can hide the fact that an object exists in a different address space. Also allows the client to access objects on the remote machine, 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. It is only created when we really need this object.

(3) Copy-on-write agent (Copy-on-write proxy) – Used to control the replication of objects by delaying the copying of objects until the customer really needs them. is a variant of the virtual agent.

(4) Protection agent (Protection (access) proxy) – provides different levels of access to target objects for different customers

(5) Cache proxy – provides temporary storage for the result of a costly operation that allows multiple customers to share results to reduce compute or network latency.

(6) Firewall proxy (Firewall proxy) – Control access to network resources and protect topics from malicious customers.

(7) Synchronous agent (Synchronizationproxy) – Provides secure access to topics in multi-threaded situations.

(8) Smart Reference Agent (Smart Referenceproxy)-When an object is referenced, provides some extra action, such as recording the number of times this object is called.

(9) Complex hidden proxy (complexity Hidingproxy) – The complexity of hiding a complex set of classes and access control. Sometimes called the appearance agent (Fa?ade proxy), it is not difficult to understand. Complex hidden proxies and appearance patterns are not the same because the proxy controls access, and the appearance pattern is not the same because the proxy controls access, while the façade mode only provides another set of interfaces.

Finally, I am in the ruling Land telecommunications nine district play, interested in a holiday to play two friends can add my id Saint Immortal Hero, is now a silver one of the Dan, since the strength of gold.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

I have to tell the story with the proxy model

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.