0
点赞
收藏
分享

微信扫一扫

android | 声明式编程!(笔记)

灵魂跑者 2024-06-29 阅读 6
策略模式

1、策略模式、工厂模式解决if else

Cal

package com.example.dyc.cal;

import org.springframework.beans.factory.InitializingBean;

public interface Cal extends InitializingBean {
    public Integer cal(Integer a, Integer b);
}

Cal工厂

package com.example.dyc.cal;

import org.springframework.util.StringUtils;

import java.util.HashMap;

public class CalFactory {
    public static HashMap<String, Cal> calMap = new HashMap<>();

    public static Cal getCal(String name){
        return calMap.get(name);
    }

    public static void register(String name, Cal cal){
        if(StringUtils.isEmpty(name)||null == cal){
            return;
        }
        calMap.put(name,cal);
    }
}

Cal具体实现Add

package com.example.dyc.cal;

import org.springframework.stereotype.Component;

@Component
public class Add implements Cal {

    @Override
    public Integer cal(Integer a, Integer b) {
        return a + b;
    }

//    public Add(){
//        CalFactory.register("add", this);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
          CalFactory.register("add", this);
    }
}

Cal具体实现Sub

package com.example.dyc.cal;

import org.springframework.stereotype.Component;

@Component
public class Sub implements Cal{
    @Override
    public Integer cal(Integer a, Integer b) {
        return a - b;
    }

//    public Sub(){
//        CalFactory.register("sub", this);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
        CalFactory.register("sub", this);
    }
}

测试类

package com.example.dyc.cal;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CalFactoryTest {

    @Test
    void MyTest(){
        Cal add = CalFactory.getCal("add");
        Integer cal = add.cal(1, 8);
        System.out.println(cal);
    }
}

测试类具体使用参考以往操作

2、策略模式、工厂模式,模板模式实现代码复用

 Cal

package com.example.dyc.cal;

import org.springframework.beans.factory.InitializingBean;

public abstract class Cal implements InitializingBean {

    protected abstract Integer cal(Integer a, Integer b);

    public Integer secretCal(Integer a, Integer b){
        a = secretCode(a);
        b = secretCode(b);
        Integer cal = cal(a, b);
        return cal;
    }

    private Integer secretCode(Integer a){
        return a * a;
    }

//    Integer unsupport(){
//        throw new UnsupportedOperationException();
//    }
}

Cal工厂

package com.example.dyc.cal;

import org.springframework.util.StringUtils;

import java.util.HashMap;

public class CalFactory {
    public static HashMap<String, Cal> calMap = new HashMap<>();

    public static Cal getCal(String name){
        return calMap.get(name);
    }

    public static void register(String name, Cal cal){
        if(StringUtils.isEmpty(name)||null == cal){
            return;
        }
        calMap.put(name,cal);
    }
}

Cal具体实现Add

package com.example.dyc.cal;

import org.springframework.stereotype.Component;

@Component
public class Add extends Cal {

    @Override
    protected Integer cal(Integer a, Integer b) {
        return a + b;
    }

//    public Add(){
//        CalFactory.register("add", this);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
          CalFactory.register("add", this);
    }
}

Cal具体实现Sub

package com.example.dyc.cal;

import org.springframework.stereotype.Component;

@Component
public class Sub extends Cal{
    @Override
    protected Integer cal(Integer a, Integer b) {
        return a - b;
    }

//    public Sub(){
//        CalFactory.register("sub", this);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
        CalFactory.register("sub", this);
    }
}

测试类

package com.example.dyc.cal;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CalFactoryTest {

    @Test
    void MyTest(){
        Cal add = CalFactory.getCal("add");
        Integer cal = add.secretCal(1, 8);
        System.out.println(cal);
    }
}

测试类具体使用参考以往操作

举报

相关推荐

0 条评论