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

What are the combined use methods of Java

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

Share

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

This article mainly introduces "what are the combined use methods of Java". In the daily operation, I believe that many people have doubts about the combined use of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the combined use of Java?" Next, please follow the editor to study!

Top1: ResultExecutor + ResultDTO combination

First of all, let's talk about why this combination appeared. I learned this combination only when I was in my second job. I don't know if you still have any impression, but many times when we write some interfaces, we may return a very chilling 500 error to the caller. By using this combination, 500 errors can be solved fundamentally. This combination is very good for the encapsulation of errors and exceptions, and the writing method is standardized, which is worth learning. I hope everyone will be in their own project as a whole and bid farewell to 500 mistakes. Anyway, I have never seen 500 again.

This combination defines an abstract run method of ResultExecutor, which is used to solve the problem of how to handle and what parameters to return. When using it, just use anonymous classes to implement the business logic you want.

Second, standardize the returned results with a ResultDTO, which also ensures that no matter what our program logic is, the business side will definitely get a result. As to whether the result is success or fail, it needs to be confirmed by the business side through the field.

Public abstract class ResultExecutor {

Abstract T run ()

Public ResultDTO execute () {

Try {

T runResult = run ()

ResultDTO result = new ResultDTO ()

Result.setSuccess (true)

Result.setData (runResult)

Return result

} catch (Throwable t) {

ResultDTO result = new ResultDTO ()

Result.setSuccess (false)

Return result

}

}

}

Public class ResultDTO {

Private T data

Private Boolean success

}

Public class BananaService {

Public ResultDTO runSomeThing () {

Return new ResultExecutor () {

@ Override

Boolean run () {

/ * *

* write logic

, /

Return true

}

}. Execute ()

}

}

Top2: Center+Processor combination

The advantage of this combination is that it can split a lot of data from different stages of logic into different classes, which can play a very good role in the decoupling of code logic, which is very friendly to the processing of state machine classes, because the state machine may have dozens of states that need to be dealt with one by one, and if they are all written in the same class, if else may immediately make you dizzy.

Well, in some scenes with more categories, it is also recommended that you try it. The expansibility is very good. And this routine can be set all the way down, you can set down three or four layers and so on.

Public class ExecuteCenter {

@ Resource

Set processorSet

Public void execute (String type,String params) {

For (BaseExecuteProcessor executeProcessor: getProcessors (type)) {

ExecuteProcessor.execute (params)

}

}

Private List getProcessors (String type) {

Return processorSet.stream () .filter (processor-> type.equals (processor.getSupportType () .conversation (Collectors.toList ())

}

}

Public interface BaseExecuteProcessor {

String getSupportType ()

Void execute (String params)

}

Top3: Enum + swith combination

I really like this. In many places, as long as you need to do some extra processing according to the state of an enumeration, to a certain extent, you don't want to write too many classes to deal with these problems, I use Enum + Switch to solve them.

The advantage is that the entry is uniform no matter how deep the functionality is, there is almost no cost to add an enumerated type, and changing one does not affect the other branch implementations.

Public enum SomeTypeEnum {

T1,T2,T3,T4

Public void main (String [] args) {

String type = "T1"

SomeTypeEnum someTypeEnum = SomeTypeEnum.valueOf (type)

Switch (someTypeEnum) {

Case T1: {

ExecuteT1 (); break

}

Case T2: {

ExecuteT2 (); break

}

Default: {

}

}

}

Private void executeT1 () {}

Private void executeT2 () {}

At this point, the study on "what is the combined use of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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