Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Agent pattern of Java Design pattern

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand the agent pattern of Java design pattern. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I. definition of agent mode

   provides a proxy object for other objects, and access to this object is controlled by the proxy object.

Characteristics

1) directly, implement the same interface or inherit the same abstract class.

2) the proxy object controls access to the proxied object.

UML

This is a generic UML for proxy mode, and the roles involved are as follows:

  -Abstract theme role: defines a common interface or abstract class between the delegated role and the proxy role.

  -delegated role: implements or inherits abstract subject roles and defines an implementation that implements specific business logic.

  -proxy roles: implement or inherit abstract subject roles, hold references to delegated roles, control and restrict the implementation of delegated roles, and have their own processing methods (preprocessing and aftermath)

Second, the actual combat of the agency mode

The first is the abstract subject role:

Public interface Subject {public void movie ();}

Quite simply, the movie method is simply defined. Let's take a look at the implementation of the delegated role:

Public class Star implements Subject {

@ Override

Public void movie () {

System.out.println (getClass (). GetSimpleName () + ": the agent picked up a movie and I'm in charge of shooting it")

}

}

The delegated role implements the abstract subject role and focuses on realizing the business logic of the delegated role. Continue to look at the agent role:

Public class Agent implements Subject {

Private Subject star

Public Agent (Subject star) {

This.star = star

}

@ Override

Public void movie () {

System.out.println (getClass (). GetSimpleName () + ": the script is very good, this movie has taken over")

Star.movie ()

}

}

The agent role holds the reference of the delegated role. To access the delegated role, it must be responsible for the functions outside its own duty through the agent, and has the function of admittance and filtering. Finally, let's look at the implementation of the client:

Public class Client {public static void main (String [] args) {Subject star = new Star (); Subject proxy = new Agent (star); proxy.movie ();}}

On the surface, the method of the proxy is called, but the actual executor is actually the delegated role Star. You can see the result:

Agent: the script is good. The movie has been taken over.

Star: the manager picked up a movie. I'll take care of it.

The above is one of the implementations of the agent pattern, which mainly specifies the delegated role Star in the agent role Agent, which means that the broker shouts "Hey ~ Brother, you are mine!" .

Let's take a look at another implementation of the proxy pattern, starting with the abstract topic role code:

Public interface Subject {public void movie (); / / specify proxy public Subject getAgent ();}

Added the method getAgent to specify the proxy, and then look at the proxied role and the proxy role code:

/ / proxied role

Public class Star implements Subject {

@ Override

Public void movie () {

System.out.println (getClass (). GetSimpleName () + ": the agent picked up a movie and I'm in charge of shooting it")

}

@ Override

Public Subject getAgent () {return new Agent (this)

}

}

/ / Agent role

Public class Agent implements Subject {

Private Subject star

Public Agent (Subject star) {

This.star = star

}

@ Override

Public void movie () {

System.out.println (getClass (). GetSimpleName () + ": the theme of the script is very good, and the film takes over")

Star.movie ()

}

@ Override

Public Subject getAgent () {return this

}

}

Focus on the getAgent method of the delegated role, which specifies Agent as the proxy, while the getAgent of Agent does not specify a proxy. Let's take a look at the client code implementation:

Public class Client {public static void main (String [] args) {Subject star = new Star (); Subject proxy = star.getAgent (); proxy.movie ();}}

On the client side, the specified proxy role is obtained through getAgent, and the agent controls the star object.

The result of the operation is the same as the previous one. Note that in this way, the client accesses the delegated role directly, and the proxy is specified by the proxied role. The former method is that the client cannot access the proxied role directly, but can only access the proxy. However, either way, the implementation of the proxy pattern must go through the proxy in order to access the proxied mode. For example, when a star makes a movie, he will not skip the agent to find the star directly, but go through the agent to the star, otherwise the position of agent will not be wasted.

This is the end of the proxy pattern on how to understand the Java design pattern. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report