"Rookie learning Java" 12: Agent mode-static agent how to play? __java

Source: Internet
Author: User

What is a static proxy.

As is known in the week, there is a proxy pattern in the 23 commonly used design patterns. It is defined as follows:

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.

The idea of Agent mode is to insert a proxy object between the actual object and the caller in order to provide additional processing or different operations. These additional operations typically require communication with the actual object.

Then what is the ghost of static agent? Static means that the corresponding relationship between the proxy class and the delegate class has been determined before the program is run, or the bytecode file of the proxy class before the run is available.

Why use Agent mode.

The agent model has three main components:

Abstract role: A business method that declares a true role implementation through an interface or abstract class.

Agent role: Implement abstract role, is the proxy of real role, implement abstract method through the business logic method of real role, and can attach own operation.

Real role: Implementing abstract roles, defining the business logic to be implemented by the real role for proxy role invocation.

Specific relationship I found a picture on the Internet to help you understand:

The use of Agent mode to develop the program, our business class only need to pay attention to their own business logic on the line, what other log, access control and other auxiliary functions, no longer have to pipe, to the proxy class to complete it. This ensures the high reusability of the business class.

How to use static proxies.

Here we use Java to write a simple demo to illustrate how to use static proxies. The general requirement is that a user-managed business class needs to add log printing capabilities and how to do it. Scheme one, directly write the log printing code into the user-managed business logic implementation class inside the method; scenario two, using a static proxy, adds the log-printed code to the proxy class's method. In the example code, both approaches are reflected.

User Management interface

Public interface Usermanager {public

	void AddUser (String userId, String userName);
	
	public void Deluser (String userId);
	
	public void ModifyUser (string userId, String userName);
	
	public string Finduser (string userId);

User Management Implementation Class

public class Usermanagerimpl implements Usermanager {public void AddUser (string userId, String Userna
		Me) {//////directly add the print log code//system.out.println ("Start-->>adduser () userid-->>" + userId) to the business implementation class method;
			
			try {System.out.println ("Usermanagerimpl.adduser () userid-->>" + userId);
		System.out.println ("Success-->>adduser ()");
			}catch (Exception e) {e.printstacktrace ();
			System.out.println ("Error-->>adduser ()");
		throw new RuntimeException ();
	} public void Deluser (String userId) {System.out.println ("Usermanagerimpl.deluser () userid-->>" + userId); 
		public string Finduser (string userId) {System.out.println ("Usermanagerimpl.finduser () userid-->>" + userId);
	Return "Query success, find user Zhang three"; public void ModifyUser (string userId, String userName) {System.out.println ("Usermanagerimpl.modifyuser () USERID--&G
	T;> "+ userId); }

}

User Management proxy class

public class Usermanagerimplproxy implements Usermanager {

	//Add True object references
	private Usermanager Usermanager;
	The proxy object is passed through the construction method to the proxy class object public
	usermanagerimplproxy (Usermanager usermanager) {
		This.usermanager = Usermanager;
	}

	public void AddUser (string userId, String userName) {
		try {
			//) Add print log functionality to the core business method
			System.out.println ("Start adding users , call Method: AddUser () User id:userid-->> "+ userId);
			" Usermanager.adduser (UserId, userName);
			System.out.println ("Add user succeeded.") ");
		} catch (Exception e) {
			e.printstacktrace ();
			System.out.println ("Add user failed.") Please contact the administrator ... ");
		}

	public void Deluser (String userId) {
		usermanager.deluser (userId);
	}

	public string Finduser (string userId) {return
		usermanager.finduser (userId);
	}

	public void ModifyUser (string userId, String userName) {
		usermanager.modifyuser (userId, userName);
	}

Client

public class Client {public

	static void Main (string[] args) {
		//usermanager Usermanager = new Usermanagerimpl (); C2/>usermanager Usermanager = new Usermanagerimplproxy (new Usermanagerimpl ());
		Usermanager.adduser ("0001", "John");
		System.out.println (Usermanager.finduser ("0001"));
	}

Summary:

There are benefits to static proxies, but their drawbacks are obvious. The proxy class and the delegate class in the static proxy implement the same interface, and the proxy class implements the same method through the delegate class. This shows a lot of code duplication. If the interface adds a new method, except that all implementation classes need to implement this method, all proxy classes need to implement this method, which adds a lot of trouble to the maintenance of the code.

Second, static proxy mode only serves one type of object, if we want to add a business class, then we must add a proxy class, so if our program is very large, the use of static proxy is a very uneconomical choice. So how to solve this problem. And listen to let's.

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.