Strategies to reduce a large area if the project code

Reference Design Patterns - Strategy pattern we can optimize the if-else code segments, and in the Spring (Boot) , with the aid ApplicationContext scanning, can make the code more clean.

Man of few words, said Liang Code:

First, according to the wording of the strategy pattern, to create a Handle interface for distinguishing processing strategy.

public interface ITypeHandle {
    /**
     * 不同的业务操作
     *
     * @return 对应的操作结果
     */
    String working();

    /**
     * 声明用于处理的业务(方便查看而已)
     *
     * @return 业务名称
     */
    String handleType();
}

Then create a corresponding wake-processor and processor sleep :

// 起床操作
public class WakeTypeHandle implements ITypeHandle {
    @Override
    public String working() {
        try {
            // 模拟业务操作
            Thread.sleep(100);
        } catch (Exception ex) {
            return "Wake Interrupt";
        }
        return "Wake up Wake up Wake up";
    }

    @Override
    public String handleType() {
        return "wake";
    }
}
// 睡觉操作
public class SleepTypeHandle implements ITypeHandle {
    @Override
    public String working() {
        try {
            // 模拟业务操作
            Thread.sleep(500);
        } catch (Exception ex) {
            return "Sleep Interrupt";
        }
        return "Sleep Sleep Sleep Zzzzzzzzzzzzz";
    }

    @Override
    public String handleType() {
        return "sleep";
    }
}

Briefly, the operation mode of the policy context is processed by different services switching different processors. In Spring, you can make use of ApplicationContext and ComponentScan to complete.

Establish strategies factory :

public class TypeHandleFactory {
    private Map<String , ITypeHandle> map;

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    private void init(){
        Map<String ,ITypeHandle> beans=applicationContext.getBeansOfType(ITypeHandle.class);
        map= new HashMap<>(beans.size());
        for (ITypeHandle handle : beans.values()) {
            map.put(handle.handleType(),handle);
        }
    }
    public ITypeHandle getInstance(String type){
        return map.get(type);
    }
}

Here initialization should be carried out after the completion of the injection ApplicationContext, so add @PostConstructcomments. In the Spring, the primary sequence of class annotation is performed 构造方法=>@Autowired=>@PostConstruct=>@PreDestroy=>销毁. Also you need to add in terms of strategy factories and two handlers @Componentnotes or by a @ComponentScanscan.

Adding a Service class to simulate the business layer:

@Service
public class MainServiceImpl {
    @Autowired
    private TypeHandleFactory factory ;
    @Override
    public String work(String type){
        ITypeHandle handler = factory.getInstance(type);
        return handler.working();
    }
}

So far, we have completed the strategy mode to build in Spring . You can write the test class test:

@SpringBootTest
class MainServiceImplTest {
    @Autowired
    private MainServiceImpl service;
    @Test
    void work(){
        String typeA="wake";
        String typeB="sleep";
        System.out.println(service.work(typeA));
        System.out.println(service.work(typeB));
    }
}

Log printed as:

Wake up Wake up Wake up
Sleep Sleep Sleep Zzzzzzzzzzzzz

Strategy pattern completed in Spring optimization.

Guess you like

Origin www.cnblogs.com/ZoraZora59/p/12422967.html