Skip to content

Commit

Permalink
code optimise for strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzaiplus committed Nov 9, 2020
1 parent 73cc707 commit bca4079
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public class StrategyController {
@Autowired
private CalculateServiceFactory calculateServiceFactory;

@Autowired
private CalculateStrategy calculateStrategy;

@PostMapping("strategy1")
public ServerResponse strategy1(@RequestBody CalculateDto dto) {
Integer type = dto.getType();
Expand Down Expand Up @@ -96,7 +93,7 @@ public ServerResponse strategy3(@RequestBody CalculateDto dto) {

@PostMapping("strategy4")
public ServerResponse strategy4(@RequestBody CalculateDto dto) {
CalculateService calculateService = calculateStrategy.getCalculateService(dto.getType());
CalculateService calculateService = CalculateStrategy.getCalculateService(dto.getType());
int result = calculateService.calculate(dto.getA(), dto.getB());
return ServerResponse.success(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import com.wangzaiplus.test.common.Constant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("addCalculateServiceImpl")
public class AddCalculateServiceImpl implements CalculateService, InitializingBean {

@Autowired
private CalculateStrategy calculateStrategy;

@Override
public int calculate(int a, int b) {
return a + b;
}

@Override
public void afterPropertiesSet() throws Exception {
calculateStrategy.register(Constant.CalculateTypeEnum.ADD.getType(), this);
CalculateStrategy.register(Constant.CalculateTypeEnum.ADD.getType(), this);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.wangzaiplus.test.service.strategy;

import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CalculateStrategy {

private static Map<Integer, CalculateService> map = Maps.newConcurrentMap();

public void register(Integer type, CalculateService calculateService) {
public static void register(Integer type, CalculateService calculateService) {
map.put(type, calculateService);
}

public CalculateService getCalculateService(Integer type) {
public static CalculateService getCalculateService(Integer type) {
return map.get(type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import com.wangzaiplus.test.common.Constant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("divideCalculateServiceImpl")
public class DivideCalculateServiceImpl implements CalculateService, InitializingBean {

@Autowired
private CalculateStrategy calculateStrategy;

@Override
public int calculate(int a, int b) {
return a / b;
}

@Override
public void afterPropertiesSet() throws Exception {
calculateStrategy.register(Constant.CalculateTypeEnum.DIVIDE.getType(), this);
CalculateStrategy.register(Constant.CalculateTypeEnum.DIVIDE.getType(), this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import com.wangzaiplus.test.common.Constant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("multiplyCalculateServiceImpl")
public class MultiplyCalculateServiceImpl implements CalculateService, InitializingBean {

@Autowired
private CalculateStrategy calculateStrategy;

@Override
public int calculate(int a, int b) {
return a * b;
}

@Override
public void afterPropertiesSet() throws Exception {
calculateStrategy.register(Constant.CalculateTypeEnum.MULTIPLY.getType(), this);
CalculateStrategy.register(Constant.CalculateTypeEnum.MULTIPLY.getType(), this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import com.wangzaiplus.test.common.Constant;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("subtractCalculateServiceImpl")
public class SubtractCalculateServiceImpl implements CalculateService, InitializingBean {

@Autowired
private CalculateStrategy calculateStrategy;

@Override
public int calculate(int a, int b) {
return a - b;
}

@Override
public void afterPropertiesSet() throws Exception {
calculateStrategy.register(Constant.CalculateTypeEnum.SUBTRACT.getType(), this);
CalculateStrategy.register(Constant.CalculateTypeEnum.SUBTRACT.getType(), this);
}

}

0 comments on commit bca4079

Please sign in to comment.