This article is still supplemented to "I have learned a little experience (3)" in Java.
I used Java to develop more than a year, and I should be skilled. When I read "Design Mode" and "Effective Java", I re-learned the Java's basic class library, which has a new understanding of programming ideas. Java's basic class library is designed by experts, understanding basic class libraries, can increase their development efficiency, and on the other hand, they can design ideas for experts. In the basic class library of Java, a lot of design patterns are used, providing extension mechanisms in many ways to facilitate design mode. It can be said that the Java's basic class library will play an ultimate Open-Close PrinciPle (Software Entities Should Be Open for Extension, But Closed for Modification).
In the Java's underlying class library, some types of design are to provide tools for Java developers, directly let developers, and some are specifically designed for inheritance. For the first type of class, it is easy to use the integrated development tool, and for the second type of class, it is difficult to learn its use. I will give an example. Java 2 provides support for Proxy mode. In the following example, it demonstrates how to use proxy mode (from "Java and Mode"). Mainly understand the usage of java.lang.reflect.invocationhandler
Package com.javapatterns.proxy.reflect;
Import java.lang.reflect.invocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect.method; import java.util.vector; import java.util.list;
Public Class VectorProxy Implements InvocationHandler {Private Object Proxyobj;
/ ** @Link Dependency * / / / * # proxy lnkProxy; * /
Public VectorProxy (Object Obj) {proxyobj = Obj;}
Public Static Object Factory (Object Obj) {Class CLS = Obj.getClass ();
Return Proxy.newProxyInstance (CLS.GetClassLoader (), Cls.GetInterface (), New VectorProxy (Obj));}
Public Object Invoke (Object Proxy, Method Method, Object [] args "throws throwable {system.out.println (" before calling " method);
IF (args! = null) {for (int i = 0; i Object o = method.invoke (proxyobj, args); System.out.println ("After Calling" Method; Return O; Public static void main (string [] args) {list v = null; V = (list) Factory (New Vector (10)); v.add ("new"); v.add ("york");}}