Java design Pattern (vii): Proxy mode proxy (static agent mode + dynamic proxy mode) __java

Source: Internet
Author: User
Static proxy

Package com.iter.devbox.staticProxy;

Public interface Star {public
	
	void Sing ();

}

Package com.iter.devbox.staticProxy;

public class Realstar implements Star {

	@Override public
	void Sing () {
		System.out.println this.getclass ( ). GetName () + ". Sing ()");
	}

	


Package com.iter.devbox.staticProxy;

Public class Starhandler implements star {

	private star Realstar;
	
	Public Starhandler (Star Realstar) {
		super ();
		This.realstar = Realstar;
	}

	@Override public
	void Sing () {
		System.out.println (This.getclass (). GetName () + ". Sing () before execution");
		Realstar.sing ();
		System.out.println (This.getclass (). GetName () + ". Sing () after execution");



Package com.iter.devbox.staticProxy;

public class Client {public

	static void Main (string[] args) {
		new Starhandler (New Realstar ()). sing ();




Dynamic Proxy
Package com.iter.devbox.dynamicProxy;

Public interface Star {public
	
	void Sing ();

}
Package com.iter.devbox.staticProxy;

public class Realstar implements Star {

	@Override public
	void Sing () {
		System.out.println (this.getclass (). GetName () + ". Sing ()");
	}

	


Package com.iter.devbox.dynamicProxy;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;

public class Starhandler implements Invocationhandler {
	
	Object star;
	
	Public Starhandler (Object star) {
		super ();
		This.star = Star;
	}

	@Override Public
	object Invoke (Object proxy, Method method, object[] args) throws Throwable {
		System.out.println ("Before the agent method executes");
		Object obj = Method.invoke (star, args);
		System.out.println ("After the agent method executes");
		return obj;
	}

}

Package com.iter.devbox.dynamicProxy;

Import Java.lang.reflect.Proxy;

public class Client {public

	static void Main (string[] args) {
		Star Realstar = new Realstar ();
		Call Processor
		Starhandler handler = new Starhandler (Realstar);
		Generate proxy class interface using JDK Dynamic proxy
		star star = (Star) proxy.newproxyinstance (Classloader.getsystemclassloader (), 
				new Class[]{star.class}, handler);
		Star.sing ();
	}





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.