策略模式在spring中的应用

目的:

1)减少程序中大量if else 导致的代码可读性差。

2)好多网上示例太过原始

3)让开发人员了解设计模式在真实开发中的应用而不是胡乱模仿.

 

不扯淡直接上代码

 以结算为案例,结算实现两种规则,1、金卡结算。2、银卡结算。

一、定义接口

public interface SettleAccount {
    /**
     * 结算
     * @param account
     */
    void settle(String account);
}

二、实现金卡结算

/**
 * 金卡结算
 * @author hfkj
 *
 */
@Slf4j
@Service("settleRuleGold")
public class SettleRuleGold implements SettleAccount {

    @Override
    public void settle(String account) {
        // TODO Auto-generated method stub
        log.info("金卡结算");
    }

}

 

三、实现银卡结算

/**
 * 银卡结算
 * @author hfkj
 *
 */
@Slf4j
@Service("settleRuleSilver")
public class SettleRuleSilver implements SettleAccount {

    @Override
    public void settle(String account) {
        // TODO Auto-generated method stub
        log.info("银卡结算");
    }

}

 

四、通过spring获取bean工俱类

package com.ni.ksxtjk.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext = null;

    public void setApplicationContext(ApplicationContext arg0) throws BeansException {

        if (applicationContext == null) {

            applicationContext = arg0;

        }

 

    }

 

    public static ApplicationContext getApplicationContext() {

        return applicationContext;

    }

 

    public static void setAppCtx(ApplicationContext webAppCtx) {

        if (webAppCtx != null) {

            applicationContext = webAppCtx;

        }

    }

 

    /**

     * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象

     */

    public static <T> T getBean(Class<T> clazz) {

        return getApplicationContext().getBean(clazz);

    }

 

    public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {

        return getApplicationContext().getBean(name, clazz);

    }

 

    public static final Object getBean(String beanName) {

        return getApplicationContext().getBean(beanName);

    }

 

    public static final Object getBean(String beanName, String className) throws ClassNotFoundException {

        Class<?> clz = Class.forName(className);

        return getApplicationContext().getBean(beanName, clz.getClass());

    }

 

    public static boolean containsBean(String name) {

        return getApplicationContext().containsBean(name);

    }

 

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {

        return getApplicationContext().isSingleton(name);

    }

 

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {

        return getApplicationContext().getType(name);

    }

 

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {

        return getApplicationContext().getAliases(name);

    }

}
 

五、结算工厂

@Slf4j
public class SettleAccountFactory {

    public static SettleAccount match(String name) {
        try {

          //通过spring获取实例Bean
            return SpringContextUtil.getBean(name, SettleAccount.class);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            log.error("exception reason is class not found,class name is SettleAccount",e);
        }
        return null;
    }
    

//测试方法
    public static void main(String[] args) {
        SettleAccount settleAccount=SettleAccountFactory.match("settleRuleGold");
        settleAccount.settle("account");
    }

    
}

 

完毕,以上是案例,通过测试是可行的。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值