Skip to content

Business Overlay Product

Ryu Xin edited this page Oct 17, 2022 · 7 revisions

What you'll build

With this sample, you can learn:

  1. How a scenario-oriented horizontal business (product) is defined
  2. Conflict decision mechanism when extension point has multiple implementations
  3. Complete the Quickstart Guide reading

What you’ll need

  1. An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
  2. A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.

Maven dependency

<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-model</artifactId>
      <version>1.0.12</version>
</dependency>
<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-runtime</artifactId>
      <version>1.0.12</version>
</dependency>

Step 1: Define the group buying scenario "GroupBuyProduct" product

@Product(code = GroupBuyProduct.GROUP_BUY_PRODUCT_CODE, name = "Group Buy Trade Product")
public class GroupBuyProduct extends ProductTemplate {

    public static final String GROUP_BUY_PRODUCT_CODE = "lattice.productGroupBuyProduct";

    @Override
    public boolean isEffect(ScenarioRequest request) {
        if (request instanceof BuyScenarioRequest) {
            boolean effect = StringUtils.equals("groupBuy", ((BuyScenarioRequest) request).getSource());
            System.out.println("GroupBuyProduct effect status:" + effect);
            return effect;
        }
        return false;
    }
}

Product definitions need to be marked with the @Product annotation, and at the same time need to inherit from the ProductTemplate abstract class and implement the isEffect method. Effective conditions for GroupBuyProduct:

  1. The current scenario is the "BuyScenario", that is, the ScenarioRequest is a BuyScenarioRequest
  2. If the channel source is "groupBy" , the current product will take effect

Step 2: Implement the "custom item unit price" extension point for GroupBuyProduct

In the Quickstart Guide, the platform defines an extension point for "custom item unit price". Here, we let GroupBuyProduct realize this extension point, and assume that on the group-buying platform, the unit price of the product is 30% off. as follows:

@Realization(codes = GroupBuyProduct.GROUP_BUY_PRODUCT_CODE)
public class GroupBuyProductExt extends BlankOrderLinePriceExt {
    @Override
    public Long getCustomUnitPrice(OrderLine orderLine) {
        return orderLine.getUnitPrice() * 700 / 1000; //only for sample.
    }
}

Note: The price calculation here is only used for DEMO, the actual price calculation cannot use type coercion

Step 3: Construct a request for "BuyScenario"

public class BuyScenarioRequest implements ScenarioRequest {

    @Getter
    private final OrderLine orderLine;

    @Getter
    @Setter
    private String source;

    public BuyScenarioRequest(OrderLine orderLine) {
        this.orderLine = orderLine;
    }

    @Override
    public OrderLine getBizObject() {
        return orderLine;
    }
}

Step 4: Construct a business invocation process for "BuyScenario"

    public static void doBusiness(String source) {
        OrderLine orderLine = new OrderLine();
        orderLine.setUnitPrice(1000L);
        orderLine.setBizCode("business.b");
        try {
            Long unitPrice = new BizSessionScope<Long, OrderLine>(orderLine) {
                @Override
                protected Long execute() throws LatticeRuntimeException {
                    //bla.bla.bla
                    OrderLinePriceAbility ability = new OrderLinePriceAbility(orderLine);
                    return ability.getCustomUnitPrice(orderLine);
                }

                @Override
                public BuyScenarioRequest buildScenarioRequest(OrderLine bizObject) {
                    BuyScenarioRequest request = new BuyScenarioRequest(bizObject);
                    request.setSource(source);
                    //add some other info.
                    return request;
                }
            }.invoke();
            System.out.println("[Business B] overlay product unit price: " + unitPrice);
        } catch (LatticeRuntimeException ex) {
            System.out.println(ex.getErrorMessage().getText());
        }catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

For a business invocation process, we wrap it with BizSessionScope. the reason is:

  1. A business can overlay a lot of products; you can imagine that there is an AppStore in the e-commerce ecosystem, and each business can select and install products from the AppStore;
  2. For a business call, not all products installed by the business will take effect. For example, the "GroupBuyProduct" in this example will only take effect when the order channel is "groupBy"; 3.In terms of performance, in a business call, we need to filter out the products that actually take effect in this session, and after superimposing the active products with the business, call the extension point and reduce the implementation of multiple extension points.

Therefore, BizSessionScope will be constructed again for the first time to perform business configuration processing, effective product filtering. And this session scope cache initialization and business context initialization.

Step 5: Sample Demonstration

We demonstrate two business invocation processes:

  1. The channel source of the first business is null, and the item unit price should be 1000 (cent)
  2. The channel source of the second business is 'groupBy'. The group buying platform requires that the unit price of the item must be discounted by 30%, and the customization logic of the product takes priority (this is generally agreed when the business and the platform product are signed)
public class LatticeOverlayProductSample {

    public static void main(String[] args) {
        Lattice.getInstance().setSimpleMode(true);
        Lattice.getInstance().start();

        System.out.println("---------------------------------------");
        doBusiness(null);
        System.out.println("---------------------------------------");
        doBusiness("groupBuy");
        System.out.println("---------------------------------------");
    }
    ......
}

The running result is as follows:

---------------------------------------
GroupBuyProduct effect status:false
[Business B] overlay product unit price: 1000
---------------------------------------
GroupBuyProduct effect status:true
[Business B] overlay product unit price: 700
---------------------------------------

We can see that when the "GroupBuyProduct" takes effect, the extension point of the custom product unit price returns the customized realization of the "GroupBuyProduct", that is, the product is discounted by 30%.

The sample code can be obtained by visiting: https://github.com/hiforce/lattice-sample/tree/main/lattice-overlay-product


中文版:https://www.ryu.xin/2022/09/24/lattice-overlay-product/