spring创建策略模式的两种方式(xml配置和纯注解)

策略模式是很好用的设计模式,spring实现策略模式更加简化了代码流程:

xml和纯注解都可以实现,但是纯注解更加方便,没有配置,更加方便:先看注解的方式:

公共接口被实现

 

package org.gyy.strategy.iface;

public interface IContextStrategy {

	void say(String name);
	
}
实现类:
package org.gyy.strategy.impl;

import org.gyy.strategy.iface.IContextStrategy;
import org.springframework.stereotype.Component;

@Component("aLIContextStrategy")
public class ALIContextStrategy implements IContextStrategy {

	@Override
	public void say(String name) {
		System.out.println("阿里巴巴集团欢迎你:"+name);
	}

}

 

package org.gyy.strategy.impl;

import org.gyy.strategy.iface.IContextStrategy;
import org.springframework.stereotype.Component;
@Component("bDUContextStrategy")
public class BDUContextStrategy implements IContextStrategy{

	@Override
	public void say(String name) {
		// TODO Auto-generated method stub
		System.out.println("百度集团欢迎你:"+name);
	}

	
}

 

package org.gyy.strategy.impl;

import org.gyy.strategy.iface.IContextStrategy;
import org.springframework.stereotype.Component;
@Component("tENContextStrategy")
public class TENContextStrategy implements IContextStrategy {

	@Override
	public void say(String name) {
		// TODO Auto-generated method stub
		System.out.println("腾讯集团欢迎你:"+name);
	}

}


工厂:spring在对map注入时,会将类名作为key

 

 

package org.gyy.strategy;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.gyy.strategy.iface.IContextStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Data
@Component
public class ContextStrategyFactory {

    /**
     *
     * 功能描述: 线程安全的map
     *
     * @param:
     * @return:
     * @auther: PC_gongyiyang
     * @date: 2018/9/28 17:08
     */
    @Autowired
    private Map<String, IContextStrategy> contextStrategy = new ConcurrentHashMap<>();

    public IContextStrategy doStrategy(String type) {
        return this.contextStrategy.get(type);
    }
	
	
}
 

测试:

 

 

package org.gyy.strategy;

import javax.annotation.Resource;

import org.gyy.baseTest.BaseJunit4Test;
import org.gyy.strategy.iface.IContextStrategy;
import org.junit.Test;

public class StrategyTest extends BaseJunit4Test {
	@Resource
	private ContextStrategyFactory strategyFactory;
	
	@Test
	public void testStrategy(){
		IContextStrategy doStrategy = strategyFactory.doStrategy("aLIContextStrategy");
		doStrategy.say("张三");
	}
}


到这里就是全部的基本实现,如果有复杂的业务需求,可以组合实现具体的方法,

下面是xml的配置,没增加一个实现类就要在map加一个键值对,特别的麻烦:

 

 

 <bean id="contextStrategyFactory" class="org.gyy.strategy.ContextStrategyFactory">
       <property name="contextStrategy">
       <map>
       	<entry key="1" value-ref="aLIContextStrategy"/>
       	<entry key="2" value-ref="bDUContextStrategy"/>
       	<entry key="3" value-ref="tENContextStrategy"/>
       </map>
       </property>
   </bean>
   <bean id="aLIContextStrategy" class="org.gyy.strategy.impl.ALIContextStrategy"/>
   <bean id="bDUContextStrategy" class="org.gyy.strategy.impl.BDUContextStrategy"/>
   <bean id="tENContextStrategy" class="org.gyy.strategy.impl.TENContextStrategy"/>

 

 

 

 

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值