Spring Boot How to get rid of if else?

demand

Here a virtual business needs, so that we easily understood. Suppose there is a line system, which is made a function of different types of processing in accordance with the order.

Orders entity:

 

 

 service interfaces:

 

 

 

Traditional achieve

Write a bunch of orders according to type if else:

 

 

 

Strategy pattern implementation

Use strategy mode, you only need two lines to implement business logic:

 

 

 

It can be seen that the above process into a HandlerContext, this is a context processor, a processor used to store different business, particularly in the following will explain. We derive an abstract processor AbstractHandler, call its methods to implement business logic.

We can now understand that our main business logic is implemented in a processor, so the number of order types, it corresponds to the number of processors. After changes in demand, increased order type, only need to add the appropriate processor can be the OrderServiceV2Impl completely without changes.

Let's take a look at the wording of traffic processors:

 

 

 

 

 

 

 

 

 

First, each processor must be added to the spring container, and therefore need to add @Component notes, followed by the need to add a custom annotation @HandlerType, which is used to identify the processor corresponding to the order type, the last is inherited AbstractHandler, to realize their business logic.

Custom annotation @HandlerType:

 

 

 Abstract processor AbstractHandler:

 

Custom annotation processor are very simple and abstract, how to register a container processor to the spring it?

Specific ideas are:

1, scans the specified package marked @HandlerType class;

2, the annotation type value as key, as a value corresponding to category, stored in a Map;

3, as in the above map constructor parameter initialization HandlerContext, which is registered to the spring container;

We will core functions are encapsulated in HandlerProcessor class, completion of the above functions.

Handler Processor:

 

 

ClassScaner: source code scanning tools

 

HandlerProcessor need to achieve BeanFactoryPostProcessor, spring before treatment bean, bean register the customized to the vessel.

The core has been completed, and now look HandlerContext how to obtain the corresponding processor:

HandlerContext:

 

 

BeanTool: get bean Tools

#getInstance method of acquiring the corresponding class according to the type, then the spring to obtain registration class according to the type of bean.

Finally, please note that, HandlerProcessor and BeanTool must be scanned, or by way of @Bean explicitly registered in order to play a role in the start of the project.

 

to sum up

Utilization Strategy mode simplifies complex if else the code, easy maintenance, and use custom annotations and self-registration mode, you can easily cope with the demand for change. This article only provides a general idea, there are many details can be flexible and change, for example, enumerated types, static or constant, as the type of order, I believe you can think of more and better ways.

 

Guess you like

Origin www.cnblogs.com/zhaoyan001/p/11447666.html