Tec&Cult
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Tec&Cult

Technologie et Culture
 
AccueilAccueil  PortailPortail  GalerieGalerie  Dernières imagesDernières images  RechercherRechercher  S'enregistrerS'enregistrer  Connexion  
Sujets similaires
Rechercher
 
 

Résultats par :
 
Rechercher Recherche avancée
Derniers sujets
» Que faire à Paris le week-end du 15 août ?
How to create a Proxy in Java EmptyJeu 11 Aoû - 11:20 par Admin

» T-shirt Mec de Paname
How to create a Proxy in Java EmptyMer 3 Aoû - 17:04 par Admin

» dating web site uk dating free parent single
How to create a Proxy in Java EmptyMer 3 Aoû - 10:56 par Invité

» rsvp dating website seeking bisexual
How to create a Proxy in Java EmptyLun 1 Aoû - 2:08 par Invité

» dating french woman gay bottom seeking tops
How to create a Proxy in Java EmptyDim 31 Juil - 21:59 par Invité

» dating philippine woman man seeking wealthy woman
How to create a Proxy in Java EmptyVen 29 Juil - 12:51 par Invité

» dating lesbian n r cacee cobb dating lachey nick
How to create a Proxy in Java EmptyVen 29 Juil - 3:17 par Invité

» single dating chat room relationship dating advice
How to create a Proxy in Java EmptyJeu 28 Juil - 0:21 par Invité

» скачать порно тетя скачать порнофото семейное
How to create a Proxy in Java EmptyJeu 21 Juil - 14:34 par Invité

Navigation
 Portail
 Index
 Membres
 Profil
 FAQ
 Rechercher
Forum
Partenaires
Forum gratuit


Tchat Blablaland
Le Deal du moment : -45%
PC Portable LG Gram 17″ Intel Evo Core i7 32 Go ...
Voir le deal
1099.99 €

Partagez | 
 

 How to create a Proxy in Java

Voir le sujet précédent Voir le sujet suivant Aller en bas 
AuteurMessage
Admin
Admin


Messages : 156
Date d'inscription : 20/05/2010

How to create a Proxy in Java _
MessageSujet: How to create a Proxy in Java   How to create a Proxy in Java EmptyJeu 23 Juin - 16:03

http://toptech.geekaddict.net/
How to create a Proxy in Java
A proxy is an object that can wrap another object and expose the same interface. This proxy can be used the same way as the original object, but can contain extra functionality. It can be an important tool for Aspect-oriented programming. The following example shows how to make a proxy that logs the method calls of an object.
We define a logger proxy that wraps an object and log all method invocations on it.
class LoggerProxy implements InvocationHandler {

private Object proxiedObj;

private LoggerProxy(Object proxiedObj) {
this.proxiedObj = proxiedObj;
}

public Object invoke(Object proxy, Method method, Object[] args) {
try {
Object returnValue = method.invoke(proxiedObj, args);
String log = "LoggerProxy - method: " + method.getName() + " called on object: " + proxiedObj;
System.out.println(log);
return returnValue;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static Object wrapObject(Object proxiedObj) {
return Proxy.newProxyInstance(
proxiedObj.getClass().getClassLoader(),
proxiedObj.getClass().getInterfaces(),
new LoggerProxy(proxiedObj));
}
}
In this test application we define an interface for testing the proxy (Foo), and an implementing class (FooImpl). We create an instance of FooImpl, then wrap that object in a Logger proxy, then call some methods through the proxy.
interface Foo {
public void fooTest();
}

class FooImpl implements Foo {
public void fooTest() {
System.out.println("Foo: fooTest() called");
}
}

public class Test {
public static void main(String[] args) {
// create an instance of Foo
Foo f = new FooImpl();

// wrap the object f in a logger proxy. The type of the proxied object remains Foo.
Foo proxiedObject = (Foo) LoggerProxy.wrapObject(f);

// call a method through the proxy
proxiedObject.fooTest();
}
}
Revenir en haut Aller en bas
http://toptech.geekaddict.net
 

How to create a Proxy in Java

Voir le sujet précédent Voir le sujet suivant Revenir en haut 
Page 1 sur 1

Permission de ce forum:Vous ne pouvez pas répondre aux sujets dans ce forum
Tec&Cult :: Informatique :: Java :: Tutoriel-
Sauter vers: