Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sampath Kumar S Assistant Professor, SECE/IT

Similar presentations


Presentation on theme: "Sampath Kumar S Assistant Professor, SECE/IT"— Presentation transcript:

1 Sampath Kumar S Assistant Professor, SECE/IT
Proxies Sampath Kumar S Assistant Professor, SECE/IT

2 Proxies Sometime the interfaces need to be implemented during runtime.
1/3/2019 Proxies Sometime the interfaces need to be implemented during runtime. Some programmer place the required code in file and invoke the compiler when necessary and load the resulting class file. But this process is slow. Proxy technique is used as alternative for this, it create a new class at run-time that implements the desired interfaces. Sampath Kumar S, AP/IT

3 Methods in proxy class:
1/3/2019 Methods in proxy class: There are 2 methods present in the proxy class: The method specified by the interface. Methods defined in the object class. Sampath Kumar S, AP

4 newProxyInstance: The methods is called on the proxy object.
1/3/2019 newProxyInstance: The methods is called on the proxy object. For creating the proxy object newProxyInstance() method of Proxy class is used. newProxyInstance() has 3 parameters: Class loader. Array of class objects. Invocation handler. whenever particular method is called on proxy object, the invocation handler invokes the invoke() method with the object of the method being invoked. It is the invocation handler who directs how to handle the call for a method a run time. Proxy class are always public and final Sampath Kumar S, AP

5 1/3/2019 import java.lang.reflect.*; interface Gowtham { void MyFunction(); //Method is called at runtime } class InterfaceInstanse implements Gowtham public void MyFunction() //definition of the method Integer a = 10; Integer b = 20; Integer c = a + b; System.out.println("Sum of 2 nos is: "+c); Sampath Kumar S, AP/IT

6 1/3/2019 class MyProxy implements InvocationHandler { Object obj; public MyProxyClass(Object i) obj = i; } public Object invoke (Object proxy, Method m, Object[] args) throws Throwable Object result = null; System.out.println("Before invoking the Method"); result = m.invoke (obj, args); System.out.println("After invoking the method"); return result; Sampath Kumar S, AP

7 1/3/2019 Sampath Kumar S, AP

8 1/3/2019 Thank You  Sampath Kumar S, AP


Download ppt "Sampath Kumar S Assistant Professor, SECE/IT"

Similar presentations


Ads by Google