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 use Java static Agent

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

Share

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

This article mainly introduces "how to use Java static proxy". In daily operation, I believe many people have doubts about how to use Java static proxy. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to answer your doubts about "how to use Java static proxy"! Next, please follow the small series to learn together!

What is proxy mode?

Java proxy patterns are simply enhancements to target methods.

We all buy train tickets when we go home during the Spring Festival. We need to go to the ticket office to buy train tickets in previous years. There are train ticket sales points in some places. These sales points are very close to you. You can easily buy train tickets. These points of sale are equivalent to agents, and buying tickets is our goal. We can buy tickets directly at these points of sale, simplifying the intermediate process and saving our time. This is the idea of Java agent pattern.

Common transactions and logs in Java are an application of agent mode, only need to add corresponding annotations to realize the opening and submission of transactions, we can spend more time on the implementation of specific services, the specific implementation of transactions to the agent to do.

What is a static proxy?

A static proxy is a proxy class whose.class file already exists before the program runs.

A static proxy needs to define an interface first, let the target class and the proxy class implement this interface at the same time, and then let the proxy class call the target class to enhance the functionality of the target class.

Ticket buying interface

package com.teasir.spring.proxystatic;public interface BuyTicket { void buy(Double price);}

Buy tickets at the ticket office

package com.teasir.spring.proxystatic;/** * * */public class BuyTicketImpl implements BuyTicket { @Override public void buy(Double price) { System.out.println("Price is: " + price); }}

Buy tickets at the outlet

package com.teasir.spring.proxystatic;/** * The point of sale helps customers buy tickets at the ticket hall * The point of sale helps customers buy tickets conveniently, charging part of the handling fee, and the customer is also very recognized * */public class ProxyBuyTicketImpl implements BuyTicket { private BuyTicket buyTicket; public ProxyBuyTicketImpl(BuyTicket buyTicket) { this.buyTicket = buyTicket; } @Override public void buy(Double price) { System.out.println("Customer conveniently bought the ticket at the sales outlet downstairs, paying: "+price+"yuan, where the ticket price: "+(price-10)+"yuan, handling fee: 10 yuan. "); buyTicket.buy(price - 10); }}

test class

package com.teasir.spring.proxystatic; public class StaticProxy { public static void main(String[] args) { //Goal Method: Buy tickets at the ticket office BuyTicktImpl buyTickt=new BuyTicktImpl(); //agent method, agent point help buy tickets at the ticket hall ProxyBuyTicketImpl proxyBuyTicket=new ProxyBuyTicketImpl(buyTickt); proxyBuyTicket.buy(110.00); } }

Customers are very convenient to buy tickets at the downstairs sales point, pay: 110.0 yuan, of which ticket price: 100.0 yuan, handling fee: 10 yuan.

Price: 100.0

static proxy disadvantage

Although static proxies can enhance the target class, a corresponding proxy class must be established from each enhanced target class, so there will still be a lot of duplicate code, and the reusability is poor.

At this point, the study of "how to use Java static proxy" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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