当前位置:网站首页>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
边栏推荐
- Character function and string function (2)
- How to automatically generate short chains? How to generate links with UTM parameters online in batches?
- NPM module removal_ [solved] after NPM uninstalls the module, the module is not removed from package.json [easy to understand]
- leetcode-146:LRU 缓存
- Interface testing tool restlet client
- The role of the resize function is "suggestions collection"
- Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
- Advanced technology management - how can the team be broken?
- leetcode-79:单词搜索
- Use of C log4net: add file name and line number to the output log content; Repackaged class output file name and line number
猜你喜欢

How to choose a microservice registration center?

Basic method of black box (function) test

Leetcode skimming -- guess the size of numbers II 375 medium

Product principles of non-financial decentralized application

Unity VS—— VS中默认调试为启动而不是附加到Unity调试

leetcode-155:最小栈

Miscellaneous notes -- a hodgepodge

Struct, enum type and union

Opencv learning Fourier transform experience and line direction Fourier transform code

Test cases and defect report templates
随机推荐
Airtest解决“自动装包”过程中需要输入密码的问题(同适用于随机弹框处理)
LeetCode刷题——猜数字大小II#375#Medium
MPI learning notes (II): two implementation methods of matrix multiplication
Kali modify the update source (it is not safe to update with this source)
Leetcode-6125: equal row and column pairs
mysql导入数据时已改成csv utf8文件且文件名为英文,为什么还是导入失败
Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
CTS test steps (Casio cts200 test)
Struct, enum type and union
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
Basic knowledge of Marine Geology
NPM module removal_ [solved] after NPM uninstalls the module, the module is not removed from package.json [easy to understand]
有哪些优化mysql索引的方式请举例(sqlserver索引优化)
Niuke-top101-bm37
resize函数的作用「建议收藏」
Vivo official website app full model UI adaptation scheme
In June 2021, the interview suffered a Waterloo. Is it so convoluted now
Introduction to MySQL engine and InnoDB logical storage structure
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)