Design of different business messages for order receiving

1, Business scenario Messages are distributed...

1, Business scenario

Messages are distributed to the order system. Different messages represent different order business logic

2, Business design ideas

1. About message: it should be in a unified format. For example, if the message is xml or json, it should define a unified message structure to facilitate subsequent unified parsing and processing

2. The same processing logic: after receiving the message, it needs to be parsed and processed uniformly to parse the core business message body and pass it into the corresponding business processing logic

3. Different logic: different messages correspond to different business processing logic

4. How to distinguish different messages: when sending messages, define a parameter such as tags to distinguish different business messages

5. How different messages enter the corresponding business processing: get the implementation class according to the tags

3, Code design ideas

code:

1. Interface

public interface BmsService { boolean execute(EccMessageReqVO eccMessageReqVO) throws Exception; BmsServiceEnum getType(); }

2. Abstract class

@Component @Slf4j public abstract class AbstractBmsService implements BmsService { private static final String SHEET = "SHEET"; @Override @Transactional public boolean execute(EccMessageReqVO eccMessageReqVO) throws Exception { log.info("implement ecc-bms-mq Consumption news service:{},Input:{}", eccMessageReqVO.getEccServiceName(), JSON.toJSONString(eccMessageReqVO)); String message = eccMessageReqVO.getMessage(); // JSONObject obj = JSON.parseObject(message).getJSONObject(SHEET); // if (Objects.nonNull(obj)) { // String msg = JSON.toJSONString(obj); // eccMessageReqVO.setMessage(msg); // } this.process(eccMessageReqVO); return true; } public abstract void process(EccMessageReqVO eccMessageReqVO) throws Exception; }

  3. Implementation class

@Service @Slf4j public class ECCBMS152ServiceImpl extends AbstractBmsService { @Override public void process(EccMessageReqVO eccMessageReqVO) throws Exception { String msaage = eccMessageReqVO.getMessage(); log.info("ECCBMS152 message: {}", msaage); return; } @Override public BmsServiceEnum getType() { return BmsServiceEnum.ECC_BMS152; } }

Design pattern: strategy + factory + template

Classes and interfaces:

An interface: an abstract class implements the interface. The interface contains two methods. Template method: abstract class implementation; Methods to distinguish different business classes: subclass implementation

An abstract class: to solve the above 2.3 problems, the common parsing logic is implemented by public methods in the abstract class, and an abstract interface is defined and implemented by different business implementation classes. Methods: template method: dealing with common logic; Abstract method: subclass implementation

Multiple business implementation classes: solve the above 3 and 5 problems, inherit the abstract classes, and implement the abstract methods in the abstract classes: implement your own business; Methods in the implementation interface: mark the implementation class itself for subsequent access to specific implementation classes

The design mode reflects:

Template method pattern: abstract class: template method implements public logic, and abstract method implements concrete logic,

Detailed explanation of template method mode (template method design mode)

Policy pattern: define an interface, and different subclasses implement their own type logic

Detailed explanation of strategy mode (strategy design mode)

Abstract factory: it implements its own type logic model, which can also be understood as abstract factory. Similar to the strategy

Introduction to abstract factory:

Like the factory method mode, the abstract factory mode is also composed of four elements: abstract factory, concrete factory, abstract product and concrete product. However, the number of methods in the abstract factory is different, so is the number of abstract products. Now let's analyze its basic structure and implementation method.

1. Structure of the model

The main roles of the abstract factory pattern are as follows.

  1. Abstract Factory: it provides an interface for creating products. It contains multiple methods for creating products newProduct(), which can create multiple products of different levels.
  2. Concrete Factory: it mainly implements multiple abstract methods in the abstract factory to complete the creation of specific products.
  3. Abstract Product: it defines the Product specification and describes the main features and functions of the Product. The abstract factory pattern has multiple Abstract products.
  4. Concrete product: it implements the interface defined by the abstract product role and is created by the concrete factory. It has a many-to-one relationship with the concrete factory.

Abstract factory: corresponding interface

Specific factory: corresponding implementation class

Abstract product: the corresponding enumeration class, the enumeration class returned in the interface

public enum BmsServiceEnum implements GenericStringEnum { ECC_BMS110("ECC_BMS110", "Upload return application form-fresh "), ECC_BMS110A("ECC_BMS110A", "Upload return application form-Food supplies"),

Specific product: the specific product returned in the subclass interface, such as:

public BmsServiceEnum getType() { return BmsServiceEnum.ECC_BMS152; }

4, Thinking

Difference between template and policy

1. The template defines abstract classes and the policy defines interfaces

2. Relationship with subclasses:

Abstract class: the execution results of subclasses may be returned to affect the logic in the abstract class, that is, the results of subclass processing are further processed as parameters of other logic in the abstract class.

Strategy: without the above abstract classes, the logic of the reverse impact

Difference between strategy and factory

  Same point: both are implemented by defining interfaces

Differences: the factory involves Abstract products and specific products, which are reflected through polymorphism; Strategy: the above implementation is embodied by enumeration

14 October 2021, 18:45 | Views: 6687

Add new comment

For adding a comment, please log in
or create account

0 comments