当前位置:网站首页>In depth understanding of seven specific ways to enhance code scalability
In depth understanding of seven specific ways to enhance code scalability
2022-07-25 21:10:00 【InfoQ】
1 Six principles
Principle of single responsibility : Do one thing at a time
Li substitution principle : Subclasses extend the parent
The principle of Dependence Inversion : Interface oriented programming
Interface isolation principle : High cohesion and low coupling
Dimitar's law : Least known principle
Opening and closing principle : Close changes , Open new 2 Database dimension
2.1 Design type field
2.2 Design extended fields
2.3 Design business binary fields
2.3.1 Demand background

2.3.2 Find the problem
Retrieving data needs to synchronize a copy to ES
Business parties use this table through Flink Calculate business indicators
The business party subscribes to this table Binlog Message processing 2.3.3 Solution








2.3.4 Code instance
// 1 -> 00000001
NORMAL(1, " Ordinary users "),
// 2 -> 00000010
MANAGER(1 << 1, " Administrators "),
// 4 -> 00000100
SUPER(1 << 2, " Super administrator ")
;
private int code;
private String description;
private UserRoleEnum(Integer code, String description) {
this.code = code;
this.description = description;
}
public String getDescription() {
return description;
}
public int getCode() {
return this.code;
}// 1 -> 00000001
NORMAL(1, " Ordinary users "),
// 2 -> 00000010
MANAGER(1 << 1, " Administrators "),
// 4 -> 00000100
SUPER(1 << 2, " Super administrator ")
;
// New role -> Bit or operation
// oldRole -> 00000001 -> Ordinary users
// addRole -> 00000010 -> New administrator
// newRole -> 00000011 -> Ordinary users and administrators
public static Integer addRole(Integer oldRole, Integer addRole) {
return oldRole | addRole;
}
// Delete the role -> Bit exclusive or operation
// oldRole -> 00000011 -> Ordinary users and administrators
// delRole -> 00000010 -> Delete Administrator
// newRole -> 00000001 -> Ordinary users
public static Integer removeRole(Integer oldRole, Integer delRole) {
return oldRole ^ delRole;
}
// Is there a role -> Bit and operation
// allRole -> 00000011 -> Ordinary users and administrators
// qryRole -> 00000001 -> Whether there is an administrator role
// resRole -> 00000001 -> There are normal user roles
public static boolean hasRole(Integer allRole, Integer qryRole) {
return qryRole == (role & qryRole);
}
private int code;
private String description;
private UserRoleEnum(Integer code, String description) {
this.code = code;
this.description = description;
}
public String getDescription() {
return description;
}
public int getCode() {
return this.code;
}
public static void main(String[] args) {
System.out.println(addRole(1, 2));
System.out.println(removeRole(3, 1));
System.out.println(hasRole(3, 1));
}select * from user_role where (user_flag & 1) = user_flag;
select * from user_role where (user_flag & b'0001') = user_flag;<select id="selectByUserRole" resultMap="BaseResultMap" parameterType="java.util.Map">
select * from user_role
where user_flag & #{userFlag} = #{userFlag}
</select>
<select id="selectByUserIdAndRole" resultMap="BaseResultMap" parameterType="java.util.Map">
select * from user_role
where id = #{userId} and user_flag & #{userFlag} = #{userFlag}
</select>3 Interface dimension
3.1 Design type input
public class OrderDTO {
private Integer bizType;
private Integer bizSubType;
private Long amount;
private String goodsId;
}
public Response<String> createOrder(OrderDTO order);3.2 The design is loosely incorporated
public class OrderDTO {
private Integer bizType;
private Integer bizSubType;
private Long amount;
private String goodsId;
private Map<String, String> params;
}
public Response<String> createOrder(OrderDTO order);3.3 Design interface version number
/order/1.0/createOrder
/order/1.1/createOrder3.4 Design vertically and horizontally
@Resource
private OrderMapper orderMapper;
@Override
public void createOrder(OrderBO orderBO) {
if (null == orderBO) {
throw new RuntimeException(" Parameter exception ");
}
if (OrderTypeEnum.isNotValid(orderBO.getType())) {
throw new RuntimeException(" Parameter exception ");
}
// A Type order
if (OrderTypeEnum.A_TYPE.getCode().equals(orderBO.getType())) {
orderBO.setPrice(orderBO.getPrice() * 0.9);
if (orderBO.getWeight() > 9) {
throw new RuntimeException(" Exceeding the maximum weight of logistics ");
}
orderBO.setRefundSupport(Boolean.FALSE);
}
// B Type order
else if (OrderTypeEnum.B_TYPE.getCode().equals(orderBO.getType())) {
orderBO.setPrice(orderBO.getPrice() * 0.8);
if (orderBO.getWeight() > 8) {
throw new RuntimeException(" Exceeding the maximum weight of logistics ");
}
orderBO.setRefundSupport(Boolean.TRUE);
}
// C Type order
else if (OrderTypeEnum.C_TYPE.getCode().equals(orderBO.getType())) {
orderBO.setPrice(orderBO.getPrice() * 0.7);
if (orderBO.getWeight() > 7) {
throw new RuntimeException(" Exceeding the maximum weight of logistics ");
}
orderBO.setRefundSupport(Boolean.TRUE);
}
// Save the data
OrderDO orderDO = new OrderDO();
BeanUtils.copyProperties(orderBO, orderDO);
orderMapper.insert(orderDO);
}
3.4.1 Isolate vertically
@Override
public void discount(OrderBO orderBO) {
orderBO.setPrice(orderBO.getPrice() * 0.9);
}@Override
public void discount(OrderBO orderBO) {
orderBO.setPrice(orderBO.getPrice() * 0.8);
}@Override
public void discount(OrderBO orderBO) {
orderBO.setPrice(orderBO.getPrice() * 0.7);
}@Resource
private TypeADiscountStrategy typeADiscountStrategy;
@Resource
private TypeBDiscountStrategy typeBDiscountStrategy;
@Resource
private TypeCDiscountStrategy typeCDiscountStrategy;
public DiscountStrategy getStrategy(String type) {
return strategyMap.get(type);
}
@Override
public void afterPropertiesSet() throws Exception {
strategyMap.put(OrderTypeEnum.A_TYPE.getCode(), typeADiscountStrategy);
strategyMap.put(OrderTypeEnum.B_TYPE.getCode(), typeBDiscountStrategy);
strategyMap.put(OrderTypeEnum.C_TYPE.getCode(), typeCDiscountStrategy);
}public void discount(OrderBO orderBO) {
DiscountStrategy discountStrategy = discountStrategyFactory.getStrategy(orderBO.getType());
if (null == discountStrategy) {
throw new RuntimeException(" No preferential strategy ");
}
discountStrategy.discount(orderBO);
}3.4.2 Arrange horizontally
@Resource
private OrderMapper orderMapper;
public void createOrder(OrderBO orderBO) {
// Parameter checking
if (null == orderBO) {
throw new RuntimeException(" Parameter exception ");
}
if (OrderTypeEnum.isNotValid(orderBO.getType())) {
throw new RuntimeException(" Parameter exception ");
}
// Calculate the discount
discount(orderBO);
// Calculated weight
weighing(orderBO);
// Refund support
supportRefund(orderBO);
// Save the data
OrderDO orderDO = new OrderDO();
BeanUtils.copyProperties(orderBO, orderDO);
orderMapper.insert(orderDO);
}
public abstract void discount(OrderBO orderBO);
public abstract void weighing(OrderBO orderBO);
public abstract void supportRefund(OrderBO orderBO);@Resource
private DiscountStrategyExecutor discountStrategyExecutor;
@Resource
private ExpressStrategyExecutor expressStrategyExecutor;
@Resource
private RefundStrategyExecutor refundStrategyExecutor;
@Override
public void discount(OrderBO orderBO) {
discountStrategyExecutor.discount(orderBO);
}
@Override
public void weighing(OrderBO orderBO) {
expressStrategyExecutor.weighing(orderBO);
}
@Override
public void supportRefund(OrderBO orderBO) {
refundStrategyExecutor.supportRefund(orderBO);
}4 Article summary
边栏推荐
- Record the transfer of domain names from Alibaba cloud service providers to Huawei cloud
- wokerman 自定义写入日志文件
- leetcode-6130:设计数字容器系统
- Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
- Qixin Jushi cloud spectrum new chapter | Haitai Fangyuan and Sichuan Unicom reach ecological strategic cooperation
- MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
- Yolov7 training error indexerror: list index out of range
- Using the OAP aspect causes the controller to be called repeatedly
- Leetcode-6130: designing digital container systems
- Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
猜你喜欢

Struct, enum type and union

Jmeter分布式压测

Character function and string function (2)

Opencv learning Fourier transform experience and line direction Fourier transform code
![[online tutorial] iptables official tutorial -- learning notes 2](/img/7d/5f8328d1b4c8878f17c95d2658d2d6.jpg)
[online tutorial] iptables official tutorial -- learning notes 2

Basic method of black box (function) test

Qixin Jushi cloud spectrum new chapter | Haitai Fangyuan and Sichuan Unicom reach ecological strategic cooperation

Leetcode-6127: number of high-quality pairs

Cesium 多边形渐变色纹理(Canvas)

Vivo official website app full model UI adaptation scheme
随机推荐
两数,三数之和
When facing complex problems, systematic thinking helps you understand the essence of the problem
Leetcode skimming -- guess the size of numbers II 375 medium
What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
How to store pictures in the database "suggested collection"
Brush questions with binary tree (4)
Sqlx library usage
Jmeter分布式压测
How to obtain the subordinate / annotation information of KEGG channel
Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)
yuv422转rgb(422sp转420p)
MPI学习笔记(二):矩阵相乘的两种实现方法
[advanced drawing of single cell] 07. Display of KEGG enrichment results
如何自动生成短链?如何在线批量生成带UTM参数的链接?
An interview question about recover in golang
【C语言入门】ZZULIOJ 1016-1020
Leetcode-6126: designing a food scoring system
Niuke-top101-bm37
I live far away. Is there a good way to open an account? Is it safe to open a stock account by mobile phone?