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

Agent pattern of design pattern

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

First of all, let's learn what is the agent mode.

The so-called proxy mode is that objects control access to real objects through proxies.

For example, our daily shopping does not directly contact the manufacturer, but indirectly through online shopping malls, offline supermarkets and other forms. Then these malls or supermarkets are our agents. Our agent goes to contact the manufacturer to buy goods for us.

The design pattern consists of three core modules:

Subject (Abstract theme role): it is an abstract interface for both real and proxy themes. In this way, a proxy theme can be used wherever the real theme is called.

Proxy (proxy theme): our protagonist, which contains references to real themes, so that you can manipulate real theme objects at will, create and delete real objects when needed. And the proxy theme provides the same interface as the real theme to replace the real theme. Proxy topics often perform more operations, such as security checking before invoking real topic operations, log processing after real topic calls, and so on.

RealProject (really theme): this is an arrogant little princess who doesn't get in touch with the outside world easily and leaves everything to the Proxy.

Advantages and disadvantages of the agent model:

1. The ability to coordinate the caller and the callee reduces the coupling of the system to a certain extent.

two。 The client can program for abstract topic roles, adding and changing agent classes do not need to modify the source code, in line with the open and closed principle, the system has good flexibility and expansibility.

Disadvantage: it is to realize the agent function, which increases the extra workload.

The following is a simple case to understand the specific implementation of the agent mode:

Interfaces to be implemented for both the real theme and the proxy theme

Public interface IBuy {void buy ();}

Define real theme classes

Public class RealBuyer implements IBuy {@ Override public void buy () {System.out.println ("I'm starving, I want to eat egg tarts");}}

Define a proxy theme

Public class ProxyBuyer implements IBuy {private RealBuy realBuy = null; @ Override public void buy () {if (this.isEggTart ()) {/ / is egg tart realBuy = new RealBuy (); realBuy.buy (); / / Buy this.recordHistory () / / collect it}} / / check whether it is eaten public boolean isEggTart () {System.out.println ("I'll eat it if it's an egg tart, but I won't buy it if it's not!"); return true } / / record it in my purchase record so that I can have a public void recordHistory () {System.out.println ("Honey, this is your favorite egg tart, would you like another one?");}}

The client calls:

Public class Main {public static void main (String [] args) {IBuy buyer = new ProxyBuyer (); buyer.buy ();}}

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