Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement static proxy mode in multithreading in Java

Shulou Source: shulou.com Published: 2022-06-01 12:52:08 10月02日 Update

This article will explain in detail how to achieve the static proxy mode in multithreading in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Preface

Proxy pattern is a kind of design pattern, which provides additional access to the target object, that is, to access the target object through the proxy object, so that it can provide additional functional operations without modifying the original target object. extend the functionality of the target object.

In short, the proxy mode is to set up an intermediate agent to control access to the original target object, so as to enhance the function of the original object and simplify the way of access.

Static agents belong to the agent pattern in the design pattern. On the contrary, there are dynamic agents, which will not be discussed in this article, and those who are interested can do their own Google research.

In fact, inheriting Thread is also a kind of static agent, so learning static proxy here can help us learn multithreading.

Static agent

Advantage: the function of the target object can be extended without modifying the target object.

Disadvantages:

redundancy. Because the proxy object wants to implement an interface consistent with the target object, there will be too many proxy classes.

It's not easy to maintain. Once methods are added to the interface, both the target object and the proxy object are modified.

I. static agent

Actual case: buying a house

The buyer of the house

Buying a house as an agent

Common behavior to buy a house

Code implementation case:

Class MyI implements BuyHouse {/ / for me, I just need to take the money and sign @ Override public void Buy () {System.out.println ("1 million, sign the contract, the house is mine!") ;}} class Agent implements BuyHouse {private BuyHouse buyHouse; public Agent (BuyHouse buyHouse) {this.buyHouse = buyHouse;} / / help me prepare the contract and other materials public void work1 () {System.out.println ("prepare the contract and other materials ~");} / / take me to the Housing Administration to go through the formalities public void work2 () {System.out.println ("take the customer to the formalities ~") } / / the agent charged me money, he had to help me prepare the purchase materials, take me to the purchase process and so on @ Override public void Buy () {work1 (); work2 (); / / customer purchase buyHouse.Buy () }} this is the end of the article on "how to implement the static proxy mode in multithreading in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Agents objects goals patterns static functions threads contracts materials articles preparations intermediaries premises customers formalities interfaces articles ways more cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Shulou Technology vpn MariaDB