The business logic and versatile technology logical difference phase separation

background

Many business code, business logic versatile technology logical and Differences mixed together, this approach results in:

  • Business intention not identified easily and quickly, or to think laborious business semantics;
  • When you want to reuse the same processing logic, then had to copy.

Developers often spend more effort to understand the business code, invisible in a lot more understanding and maintenance costs. Unfortunately, many developers still are not fully aware of this, even thought it harmless.

In fact, this minor problem, but it is reflected in a general lack of design thinking developers. If a developer emphasis on design thinking, he will find that this program is not just the details, but you can use design to control things.

So, how versatile technology logic and business logic of difference separated from it? First, we must understand what technology is logical, what is business logic.

For example: walks through a list of items, remove a list of titles of all commodities.

  • Logic Technology: usually some general processing, such as traversing a list of objects, properties of an object taken. I do not care what the list of objects, or objects of any property. I only care about go through the list and how to remove an attribute object. Logic technology typically require shielding the underlying details of the client.

  • Business logic: usually deal with differences, such as the list of goods and commodities title (possibly JSON in a field). I do not care about the technical details of how to traverse the list or remove the object properties, I only care about the commodity and commodity title. Business logic usually need to focus on the field of domain knowledge, to clearly stand out.

Hereinafter, an example will be described, how the business logic of the general logic and differences in the art of phase separation.

Examples

Consider the following code fragment for this is withdrawn from the main flow in. Listing One:

List<Integer> packIds = orderDeliveryResult.getData().stream()
                .filter(x->x.getDeliveryState() == 1 || x.getDeliveryState() == 2 )
                .map(x->x.getId()).collect(Collectors.toList());

You can see at a glance the meaning of this code do? Oh, it looks to get a package ID list, but x.getDeliveryState() == 1 || x.getDeliveryState() == 2what does it mean? You have to go to communicate about the author.

This code is a typical example of the technical and business logic mixed together. For business semantics, it does not actually care stream, filter, map this technical details. Let's look at what would later be rewritten:


    List<Integer> packIds = getDeliveredPackIds(orderDeliveryResult.getData());

    private List<Integer> getDeliveredPackIds(List<OrderDelivery> orderDelivery) {
        return StreamUtil.filterAndMap(orderExpresses, oe -> isDelivered(oe), OrderDelivery::getId);
    }

    private boolean isDelivered(OrderDelivery oe) {
        return oe.getDeliveryState() == 1 || oe.getDeliveryState() == 2;
    }
public class StreamUtil {

  private StreamUtil() {}

  public static <T,R> List<R> map(List<T> dataList, Function<T,R> getData) {
    if (CollectionUtils.isEmpty(dataList)) { return new ArrayList(); }
    return dataList.stream().map(getData).collect(Collectors.toList());
  }

  public static<T,R> List<R> filterAndMap(List<T> dataList, Predicate<? super T> predicate , Function<T,R> getData) {
    if (CollectionUtils.isEmpty(dataList)) { return new ArrayList(); }
    return dataList.stream().filter(predicate).map(getData).collect(Collectors.toList());
  }
}

After the rewrite:

  • The original main flow line in the code becomes: getDeliveredPackIds (orderDeliveryList); highlighted the business semantics: Oh, is to get a list of the parcel has been shipped. At a glance, in understanding the main processes you do not need to switch to understand this level of detail of things.

  • The original stream, filter, map StreamUtil tools are separated into classes, may be reused later, and when the business logic, no longer need to care about how to traverse the list, the filter conditions, get the details of this technique returns a list of values.

  • Isolated areas of knowledge: isDelivered. This should actually be written orderDelivery class. Such oe -> isDelivered(oe)written in a more concise form: OrderDelivery::isDelivered.

The new implementation to achieve the kill three birds:

  • Highlighting the business semantics;
  • Code reuse;
  • Precipitation domain knowledge.

This is a common technique logic and business logic of the three most important benefits!


Reflection

Java8 provides a stream, so developers in the application system left a stream of footprints everywhere; but you understand their mind it?

The aim is to be able to stream in a declarative manner clearly highlights the intention, however, developers will soon intentions aside, seek only the pleasure of writing, it did not reflect the original intention of this language feature. Perhaps Java8 providers, should provide more than a StreamUtil class, will allow developers to realize its fundamental purpose lies.

Different wording, different expression on the surface of programming, in fact, reflects a sensitivity to design thinking and domain knowledge. The tiny difference is hard to detect out, but over time, the micro-known product, once the face of large-scale business systems, will demonstrate its valuable point.

Design Thinking

Why design reflects the thinking of it?

Scalable design change is to separate essentially unchanged. Technology represents a logical part of the universal constant, and business logic represented part of the complex and volatile. The business logic versatile technology logical difference of phase separation reflects the scalability of design thinking nature. Although small programming problems is not sufficient to demonstrate its advantages, but it can indeed be a good exercise scalability design thinking. This kind of thinking can be strengthened by every "business logic logic and versatility of technology differences of phase separation" thinking and practice.

The distance, that is, in a single step.


Field thinking

Field thinking, is actually an additional effect. When the versatility of the technique derives from logic, we can more clearly see what you want to express, namely: domain knowledge and domain rules. This allows developers to more easily recognize the real concern lies, rather than indulging in technology and programming details.

Meanwhile, when clearly see the domain knowledge and domain rules and they will think it is placed in a suitable location, such as physical ability or service areas, rather than drown in the process code.


summary

The general technology logical difference business logic, to achieve a kill three birds: highlighting the business semantics; code reuse; precipitate domain knowledge. Small differences in the expression of programming, just reflect the design thinking, but also reflects whether a developer has a keen perception of domain knowledge.


Guess you like

Origin www.cnblogs.com/lovesqcc/p/10962283.html