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 realize the Agent pattern of Java Design pattern

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to achieve the proxy pattern of Java design pattern, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this article on how to realize the proxy pattern of Java design pattern. Let's take a look.

What is the agent mode?

Definition of proxy pattern: for some reason, you need to provide a proxy to an object to control access to that object. At this time, the access object is not suitable or cannot directly reference the target object, and the proxy object acts as an intermediary between the access object and the target object.

Advantages

1. The proxy mode acts as an intermediary between the client and the target object and protects the target object.

2. The proxy object can extend the function of the target object

3. The agent mode can separate the client from the target object, reduce the coupling degree of the system to a certain extent, and increase the expansibility of the program.

Shortcoming

1. The agent mode will increase the number of classes in the system design.

2. Adding a proxy object between the client and the target object will slow down the processing of requests.

3. Increase the complexity of the system.

Knowledge point

The structure of the agent pattern is relatively simple, mainly by defining an agent that inherits the abstract theme to include the real theme, so as to realize the access to the real topic, the following analyzes its basic structure and implementation method.

Implementation of agent mode

Case study: Dabendai Gan Niang sells alcohol

Abstract theme (Subject) class: selling alcohol

Real theme (Real Subject) category: Gan Niang's tavern

Agent (Proxy) category: Daben's wine stall

A booth is set up in front of the tavern of Gan Niang in Daben.

Sell wine

A wine-selling interface that declares an abstract method for selling wine

Public interface Jiu {void show ();} Gan Niang's tavern

Gan Niang's tavern class implements the interface of selling wine, and implements the show () wine selling method, declares the name of a wine, and an aa () method prompts Daben to set up a stall.

Public class GanNiang implements Jiu {private String name; GanNiang () {} GanNiang (String name) {this.name = name; aa ();} @ Override public void show () {System.out.printf ("sell a% s", name);} public void aa () {System.out.println ("Big Ben set up a stall");}} Daben's wine stand

The wine stalls of Daben realized the interface of selling wine, declared the name of wine and the attribute of Gan Niang tavern, and realized the method of selling wine.

Public class DaBen implements Jiu {private String name; private GanNiang ganNiang; DaBen () {} DaBen (String name) {this.name = name;} @ Override public void show () {if (ganNiang = = null) {ganNiang = new GanNiang (name);} ganNiang.show ();}} Test

New A big Ben wine stall, calling the show () method twice is equivalent to selling wine twice.

Public class Demo {public static void main (String [] args) {DaBen daBen = new DaBen ("daughter Red"); daBen.show (); System.out.println (); System.out.println (); daBen.show ();}}

Here we can see that the result of the first wine sale is different from that of the second wine sale, because every time you sell wine, you have to go to Gan Niang's pub to get the wine. When Daben came to get the wine for the first time, I informed Gan Niang, I came to set up the stall, and Gan Niang already knew that Daben was coming, so there was no need to notify if she took the wine again.

This is the end of the article on "how to implement the proxy pattern of the Java design pattern". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize the agent pattern of Java design pattern". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report