-
Notifications
You must be signed in to change notification settings - Fork 59
Business Overlay Product
With this sample, you can learn:
- How a scenario-oriented horizontal business (product) is defined
- Conflict decision mechanism when extension point has multiple implementations
- Complete the Quickstart Guide reading
- An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
- A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.
<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>
@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:
- The current scenario is the "BuyScenario", that is, the ScenarioRequest is a BuyScenarioRequest
- If the channel source is "groupBy" , the current product will take effect
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
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;
}
}
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:
- 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;
- 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.
We demonstrate two business invocation processes:
- The channel source of the first business is null, and the item unit price should be 1000 (cent)
- 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
Thanks..
Getting Started
Key Features
- Business Overlay Product
- Register Business Configuration
- Load Local Configuration
- Reduce Strategy
- UseCase Precipitation and Reuse
- RPC Invoke Extension
- Dynamic Loading
Key Concepts
Stories