当前位置: 首页> 房产> 建材 > 策略模式、工厂模式和模板模式的应用

策略模式、工厂模式和模板模式的应用

时间:2025/7/27 23:00:34来源:https://blog.csdn.net/m0_74295055/article/details/140046430 浏览次数:0次

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 {@Overridepublic Integer cal(Integer a, Integer b) {return a + b;}//    public Add(){
//        CalFactory.register("add", this);
//    }@Overridepublic 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{@Overridepublic Integer cal(Integer a, Integer b) {return a - b;}//    public Sub(){
//        CalFactory.register("sub", this);
//    }@Overridepublic 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 {@Testvoid 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 {@Overrideprotected Integer cal(Integer a, Integer b) {return a + b;}//    public Add(){
//        CalFactory.register("add", this);
//    }@Overridepublic 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{@Overrideprotected Integer cal(Integer a, Integer b) {return a - b;}//    public Sub(){
//        CalFactory.register("sub", this);
//    }@Overridepublic 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 {@Testvoid MyTest(){Cal add = CalFactory.getCal("add");Integer cal = add.secretCal(1, 8);System.out.println(cal);}
}

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

关键字:策略模式、工厂模式和模板模式的应用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: