Android:gravity= "Center_horizontal|center_vertical" __android

Source: Internet
Author: User
Tags aop object object throwable

The

can say that the proxy is a very important mechanism of Java, and the other of course belongs to reflection, The Reflection API (Java.lang.reflect) is mentioned separately in the JDK. Some people think that the reflection on the resource consumption is more severe, and indeed, reflection is definitely to consume resources, but also not everything should be used to reflect, so the best test should be the level of resource consumption and the use of reflection to find a balance between, this article does not fight The reflection, the reflection of the experience later posted again, oneself recently also in torture torture

Agents can be divided into: Staticproxy and dynamicproxy
such as: Java code package xyz;      Import  java.util.logging.*     public   Class  talkToSomebody{     Private  logger logger= Logger.getlogger ( this . GetClass (). GetName ());     public   void  talk (string name) {     logger.log (level.info, "Talking start ... ");     System.out.println (" Hi!ni hao, "+name);     logger.log (Level.info," Talking ends ... ");    }    }   

Package xyz; 
Import java.util.logging.* public 
class talktosomebody{ 
private Logger logger= Logger.getlogger (This.getclass (). GetName ()); 
public void Talk (String name) { 
Logger.log (Level.info, "talking start ..."); 
System.out.println ("Hi!ni hao," +name); 
Logger.log (Level.info, "talking ends ..."); 
} 


It shows that you need to talk other people, in fact, only one sentence is critical, "Hi!ni Hao XXX", this is the relationship, or the core business (this time may be a bit far-fetched), but if you want to record who you talk to, when the start, when the end of the log function is to achieve this , this is the business logic, put the business logic and core business together, if the day does not need to record, how to do? You have to change the source code, even if the customer only provide you compiled class or interface, you will be very depressed.

Workaround:
Using proxy mechanism, in fact, acting like an intermediary agency, I suddenly have something (or unwilling), to find intermediary agencies to do, of course, you get money to intermediary agencies.     Java code public interface italk{ public void talk (String name); }

Public interface italk{public 
void Talk (String name); 
 



You can take this as your request, intermediary agencies must follow your request to do, you will pay the intermediary agencies;
Java code public class Talktosomebody implements italk{ public void talk (Str     ing name) {System.out.println ("Hi,ni hao," +name); }     }

public class Talktosomebody implements italk{public 
void Talk (String name) { 
System.out.println ("Hi,ni Hao," + name); 
 

Yes, the intermediary is based on my requirements and the result is true.

Java Code public   class  StaticProxyTalk Implements  italk{     Private  logger logger=logger.getlogger ( this . GetClass (). GetName ());     Private  ITalk somebody;     Public  staticproxytalk (italk somebody) {     This .somebody=somebody;    }     public   void  talk (string name) {     log ("Talking start ...");     Somebody.talk (name);     log ("Talking ending ...");    }     private   void  log (string message) {     Logger.log (level.info,message)     }   

public class Staticproxytalk Implements italk{ 
private Logger Logger=logger.getlogger (This.getclass (). GetName ()) ; 
Private Italk somebody; 
Public Staticproxytalk (Italk somebody) { 
this.somebody=somebody; 
} 
public void Talk (String name) { 
log ("Talking start ..."); 
Somebody.talk (name); 
Log ("Talking ending ..."); 
} 
private void log (String message) { 
Logger.log (level.info,message) 
} 



Feeling much better, I do not need intermediary services, not to find him on the line, now see how the intermediary to do, to meet my requirements.

Java code public class testproxy{ public static void Main (String [     ]args) {italk proxy=new staticproxytalk (new talktosomebody ());     Proxy.talk ("Huyong"); }     }

public class testproxy{public 
static void main (String []args) { 
italk proxy=new staticproxytalk (new Talktosomebody ()); 
Proxy.talk ("Huyong"); 
} 


Yes, it did, and I can pay for it.

But the problem is still there, if I have n do not want to do it myself (lazy), I have to find every one of the intermediary agencies. It's enough to look for one at a time.

Look at one of the Logtalk:java code below.ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;ImportJava.util.logging.Level;ImportJava.util.logging.Logger; /** * @author Huyong email:yate7571@hotmail.com * * PublicclassLogtalkImplementsInvocationhandler {PrivateLogger Logger = Logger.getlogger ( This. GetClass (). GetName ());PrivateObject object; PublicObject bind (Object object) { This. Object = object; returnProxy.newproxyinstance (Object.getclass (). getClassLoader (), Object.getclass (). Getinterfaces (), This); } PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object result =NULL;Try{log ("method starts ..." + method);     result = Method.invoke (object, args);     Log ("method ends ..." + method); }Catch(Exception e)     {log (e.tostring ()); } returnResult }PrivatevoidLog (String message) {Logger.log (level.info, message); }        }

import Java.lang.reflect.InvocationHandler; 
Import Java.lang.reflect.Method; 
Import Java.lang.reflect.Proxy; 
Import Java.util.logging.Level; 

Import Java.util.logging.Logger; /** * @author Huyong email:yate7571@hotmail.com * * Public class Logtalk implements Invocationhandler {private Logger 

Logger = Logger.getlogger (This.getclass (). GetName ()); 

Private Object object; 
public object bind (Object object) {This.object = object; 

Return Proxy.newproxyinstance (Object.getclass (). getClassLoader (), Object.getclass (). Getinterfaces (), this); 
public object invoke (object proxy, Method method, object[] args) throws Throwable {object result = NULL; 
try {log ("method starts ..." + method); 
result = Method.invoke (object, args); 
Log ("method ends ..." + method); 
catch (Exception e) {log (e.tostring ()); 
return result; 
} private void log (String message) {Logger.log (level.info, message); } 

} 


As long as what I need to do is a kind of thing (to understand a class of things), I can first and intermediary agencies sign a good activity, I later all you help me to do is, I only need the structure OK, intermediary agencies also promise, as long as you give us all conform to this agreement (are object), I answered.

Also to test: Java code public class testdynamicproxy{public static void Main (stri     ng []args) {logtalk dynamicproxy=new logtalk ();     Italk proxy= (Italk) dynamicproxy.bind (new talktosomebody ());     Proxy.talk ("Yangyi"); }     }

public class testdynamicproxy{public 
static void main (String []args) { 
logtalk dynamicproxy=new logtalk (); 
Italk proxy= (Italk) dynamicproxy.bind (New Talktosomebody ()); 
Proxy.talk ("Yangyi"); 
} 


You can help me with this, and I'll make a list of what's going on with the agency, these things you have done for me, if there is no need to do something, I call you cancel that can be, do not affect other things continue to do, do not need to change the relevant agreement.


About Logtalk: Java code public static Object newproxyinstance (ClassLoader loader, class<?>[) inte Rfaces, Invocationhandler H) throws IllegalArgumentException

public static Object newproxyinstance (ClassLoader loader, 
class<?>[] interfaces, 
Invocationhandler h) 

Returns a proxy class instance of the specified interface that can assign a method call to the specified call handler

Java code public Object Bind (Object object) { this. Object = object;  return Proxy.newproxyinstance (Object.getclass (). getClassLoader (), Object.getclass (). Getinterfaces (), this);

public object bind (Object object) { 
This.object = object; 
Return Proxy.newproxyinstance (Object.getclass (). getClassLoader (), 


binding, as long as subclasses of object can be bound (hehe, all are subclasses of object.)

Summary: This is actually the most low-level implementation of AOP, the benefit of AOP is that the broker is used to separate the business logic, whether it's the core or the business logic of accessibility (or testing), such as logging as a slice to test whether each method is executed, With AOP there is no need to change any of the core business, and if you don't, you don't have to specify Pointcut (the various terms on AOP can refer to Spring Reference), which should be considered a change of mindset.

Add: Maybe the "core business" I'm using, and the other "business logic", is a little different, and it's a personal understanding: The core business is what I need to care about, and that's the core. Other business logic (a lot of books that are unrelated to the business logic of the system services logic) such as log, security, etc., just as a core shell, no shell core can still survive, but not so beautiful.

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.