Domain Model Design Ideas in Architecture Design

Domain model design ideas in architecture design:

 

 

Finally, the message header + service header + specific service is sent

 

 

Abstracted base class (field attributes common to most classes): header part

 

1. Hierarchical nesting of entities

Large-level entity classes should also be designed with tostring bidirectional methods

There are also the attributes of the operation itself made by the reflection mechanism (the corresponding entity attributes are operated according to the message)

If the inheritance method is extended and the processing method needs to be public, in the abstract method of the subclass and the parent class, the parent class uses reflection to operate the child. Although it is convenient to write in the subclass without reflection, it cannot be shared.

When debugging the interface:

    1. The string sent by the other party has a fixed pattern such as json and is converted into an entity with a tool

    2. You can directly use the reflection mechanism to operate without a class conversion tool (this reflection mechanism is very useful in calling different service class methods according to the passed strings)

    3. According to the fixed position of the message, traverse in turn and assign values ​​to the corresponding object attributes

abstract method

 

2. Small entity class

Attribute + tostring() bidirectional method

 

 

 

Specific business class: message body part

1. Inherit abstract classes

2. Further personalization by overriding abstract methods

3. Own business attributes

 

When the small entity class needs to be subdivided, the intermediate entity does not need to integrate the abstract method (it is still an abstract class at this time), and it can be overwritten in the final class

 

You can pass in the parent class when it should be (similar to the pattern (applying service applications between service layers)) (using the entity entity to pass the value in the service layer (the value type in the interface))

 

Use of entity polymorphism:

public interface PacketProcesser<P extends PaPacket,T extends Answer> {

 

public  T execute(P packet);

 

}

 

 

@Service

public class XxProcesser implements PacketProcesser<BP1303,RspBP1303> {

 

@Override

public RspBP1303 execute(BP1303 packet) {

 

Use of service polymorphism:

Reference Design Patterns

 

 

 

The reflection mechanism is also a way to convert the returned information into entities, which is more advantageous when the corresponding method needs to be called according to the return field

 

 

======================================================================================

 

 

Example of assigning object properties in sequence according to the message position:

 

 

this is the current object that calls this method, and reflection is the bytecode pairing used, this.getClass()

 

PacketHeader: The business header is also similar to BusinessHeader

 

Assert.isTrue(src.length >= PacketHeader.HEADERLEN, "The header length must be greater than" + PacketHeader.HEADERLEN);

Assert.notNull(this.packetHeader);

 

Parse out the long message piece by piece (set the value to the entity attribute)

int srcPos = 0;

// 1 message class

char[] dest = new char[4];

System.arraycopy (src, srcPos, dest, 0, dest.length);

this.setPacketType(new String(dest).trim());

srcPos += 4;

 

 

A long message is spliced ​​segment by segment (spelled out from entity attributes)

int dstBegin = 0;

char[] dst = new char[222];

 

for (int i = 0; i < dst.length; i++) {

dst[i] = ' ';

}

 

// 1 message class

packetType.getChars(0, 4, dst, dstBegin);

 

Finally, the message header + service header + specific service is sent

 

These two are equivalent to the positive and negative of PacketHeader's tostring (in PacketHeader)

 

 

 

Example of calling the corresponding method according to the message using the reflection mechanism:

 

ProcesserInfo info = processer.get(tranFunc);

 

Assert.notNull(info);

 

 

Object processer = this.beanFactory.getBean(info.getBeanName());

 

 

Class<PacketProcesser<PaPacket, Answer>> cls = (Class<PacketProcesser<PaPacket, Answer>>) processer.getClass();

 

PacketProcesser<PaPacket, Answer> proc = cls.cast(processer);

 

Type type = info.getBeanClass().getGenericSuperclass();

 

 

ParameterizedType pt = (ParameterizedType) type;

Type[] args = pt.getActualTypeArguments();

 

Class<PaPacket> clazz = (Class<PaPacket>) Class.forName(args[0].getTypeName());

 

PaPacket packet = clazz.newInstance();

packet.analyze(packetStr);

Answer answer = proc.execute(packet);

 

// Verify that the result returned by the method is correct

ValidatorResult<Answer> result = packetValidator.validator(answer);

 

 

 

==============================================================================

Example of entity level settings:

Often involves the parent class method to operate the subclass

1. The parent class provides abstract methods for the subclass to pass the properties that need to be manipulated

2. The parent class uses the reflection mechanism to operate (the child class can operate the parent class with super)

 If the inheritance method is extended and the processing method needs to be public, in the abstract method of the subclass and the parent class, the parent class uses reflection to operate the child. Although it is convenient to write in the subclass without reflection, it cannot be shared.

PaPacket:  

 

  PacketHeader//It has its own string method, which is converted into an entity method

  BusinessHeader// has its own string method

  protected abstract String[] order();

  marshal//Total string method

  analyze//The method of total conversion to entity

 

The abstract method order() inside is the field that needs to be added to the conversion in the business entity

 

marshal converts all relevant entities to strings:

 

getstring converts business entities to strings, getPacketHeader converts packet header entities to strings,

 

 

 

 

 

 

public String getString() {

StringBuilder sb = new StringBuilder();

try {

for (String fName : this.order()) {

Object obj = getValue(fName);

if (obj == null) {

sb.append("");

}

if (obj instanceof String) {

sb.append((String) obj);

}

if (obj instanceof Integer) {

sb.append(((Integer) obj).intValue());

}

if (obj instanceof Long) {

sb.append(((Long) obj).intValue());

}

if (obj instanceof Double) {

sb.append(df.format((Double) obj));

}

sb.append("&");

}

 

} catch (NoSuchFieldException | SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {

 

e.printStackTrace ();

}

sb.deleteCharAt(sb.length() - 1);

return sb.toString();

}

private Object getValue(String fName) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

 

fName = fName.substring(0, 1).toUpperCase() + fName.substring(1);

Method method = this.getClass().getMethod("get" + fName);

Assert.notNull(method);

 

return method.invoke(this);

 

}

 

That is to say, the parent class needs to operate the subclass by using the principle of reflection, and between different classes (not the parent-child relationship can be obtained by obtaining the instance (new)), it is called dynamically according to the incoming value (including methods and properties) with reflection

 

analyze converts the entire message into an object

 

 

 

Business class inherits PaPacket

 

public abstract class InMoney extends PaPacket {

 

public class InMoneyBP1310 extends InMoney{

protected String[] order() {

String[]  args={"supAcctId","custAcctId","tranAmount","tnAcctId","inAcctIdName","ccyCode","acctDate","reserve"};

 return args;

}

 

 

 

}

 

Override the order() method to add fields that need to be converted

 

When the small entity class needs to be subdivided, the intermediate entity does not need to integrate the abstract method (it is still an abstract class at this time), and it can be overwritten in the final class

 

 

 

 

Example of using a hierarchy class:

Pass in specific, use generics when using, and use the parent class as the receiving value. For useful interfaces, these classes are used, and generics or parent classes are also used when they need to be public.

 

 

 

Combined with abstract examples, method abstract thinking:

abstract class for public methods,

1, the processing logic is the same (the parent class or generic type is used to receive parameters )

2. Similar functional methods (use parent class or generic type for interface parameters )

3, mode

 

 

 

1. Conventional method:

 

RspBP1020 rsp = (RspBP1020) paTemplate.execute(bp1020, RspBP1020.class);

 

 

 

 

 

 

 

 

public class PaTemplate {

 

@Autowired

private RestTemplate restTemplate;

 

@Autowired

private PacketValidator packetValidator;

 

@Value("${pa.url}")

private String paUrl ;

 

 

public <T> T execute(PaPacket packet,Class<T> responseType){

 

 

//Verify whether the business report problem meets the specification

ValidatorResult<PaPacket> result = packetValidator.validator(packet);

Assert.isTrue(result.isValid(),result.getMessage());

 

 

HttpHeaders tempHeaders = new HttpHeaders();

tempHeaders.add("Content-type", "application/json");

HttpEntity<PaPacket> httpEntity = new HttpEntity<PaPacket>(packet,tempHeaders);

 

T response = restTemplate.postForObject(paUrl, httpEntity, responseType);

 

 

return response;

}

 

 

}

 

 

 

 

 

 

2. Common interface:

 

 

public interface PacketProcesser<P extends PaPacket,T extends Answer> {

 

public  T execute(P packet);

 

}

 

 

@Service

public class XxProcesser implements PacketProcesser<BP1303,RspBP1303> {

 

@Override

public RspBP1303 execute(BP1303 packet) {

 

 

 

 

3. Mode of use

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326288017&siteId=291194637