Java design Mode _ (structured) _ Agent mode __java

Source: Internet
Author: User

Quote Encyclopedia

That is, proxy pattern,23 one of the common object-oriented software design patterns. Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object may not fit or directly refer to another object, and the proxy object can act as a mediator between the client and the target object. composition: abstract Role: A business method that declares a true role implementation through an interface or abstract class. Agent role: To implement abstract role, is the agent of real role, through the real role of the business logic method to implement abstract methods, and can attach their own operations. Real role: Implementing abstract roles, defining the business logic to be implemented by the real role for proxy role invocation. The proxy object provides an interface that is the same as the target object, so that the target object can be substituted at any time. Proxy objects typically perform an operation before or after a client call is passed to the target object, rather than simply passing the call to the target object.

Specific implementation:



Code implementation:

1. Abstract role

Public abstract class Abstractobject {
	//operation public
	abstract void Method ();
}

2. Target object role

public class Realobject extends Abstractobject {
	@Override public
	Void Method () {
		System.out.println () Specific action ");
	}

3, Agent object role

public class Proxyobject extends Abstractobject {
	private realobject real = new Realobject ();

	@Override public
	Void Method () {
		//TODO auto-generated method stub
		before ();
		Real.method ();
		After ();
	}
	Custom handling
	private void before () {
		System.out.println ("before ...") before
	and after the method is executed;

	private void After () {
		System.out.println ("After ...");
	}


4. Client-side test

public class Client {public

	static void Main (string[] args) {
		Proxyobject object = new Proxyobject ();
		Object.Method ();
	}


Through the simple proxy mode above, after running

Before .....
Specific operations
After .....

Through the proxy mode above, that is, the proxy object implementation delegates the client's call to the target object, and can perform a specific operation before and after the method of invoking the target object.



Related Article

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.