[置顶] Proxy---reference coreJava

A feature that became available with java SE1.3

 

You use a proxy  to create at runtime new classes that implements a give set of interface.Proxies are only necessary when you don't yet know at compile time which interfacees you need to implement. This is not common situation for application programmers, and you should feel free to skip this section if you are not interested in advanced wizardry .However ,for certain system programming application ,the flexibilty that proxies offer can be very important.

 

Suppose you want to construct an object of a class that implements one or more interfaces whose exact nature you may not know at compile at runtime .This is a difficult problem .To construct an actual class ,you can simply use newIntance method or use reflection to find a constructor. But you can't instantiate an interface,you need to define a new class in a running program.

 

To overcome this problem ,some proragms generate code,place it into a file, invoke the compile and then load the resulting class file.Naturally. this is slow, and it also reuqires deployment of the compiler together with the program. The proxy mechanism is better solution. The proxy class can create brand-new class at runtime.Such a proxy implements the inferace the you specify.In particular. the proxy class has the following methods:

 

…… All methods required by the specify interfaces;and

…… All methods defined in the Object class(toSting,equals,and so on)

你可能感兴趣的:([置顶] Proxy---reference coreJava)