The strategy pattern is a good design pattern, and spring implements the policy pattern to simplify the code flow:
XML and pure annotations can be implemented, but the pure annotation is more convenient, no configuration, more convenient: First look at the annotation way:
The public interface is implemented
Package org.gyy.strategy.iface;
Public interface Icontextstrategy {
void Say (String name);
}
Implementation class:
Package Org.gyy.strategy.impl;
Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;
@Component public
class Alicontextstrategy implements Icontextstrategy {
@Override public
void Say (String Name) {
System.out.println ("Alibaba Group Welcomes You:" +name);
}
Package Org.gyy.strategy.impl;
Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;
@Component public
class Bducontextstrategy implements icontextstrategy{
@Override public
Void Say ( String name {
//TODO auto-generated method stub
System.out.println ("Welcome to Baidu Group:" +name);
}
Package Org.gyy.strategy.impl;
Import Org.gyy.strategy.iface.IContextStrategy;
Import org.springframework.stereotype.Component;
@Component public
class Tencontextstrategy implements Icontextstrategy {
@Override public
void Say (String Name) {
//TODO auto-generated method stub
System.out.println ("Welcome to Tencent Group:" +name);
}
Factory: Spring when the map is injected, the class masterpiece is 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;
@Component public
class Contextstrategyfactory {
@Autowired
private map<string, icontextstrategy> contextstrategy = new hashmap<string,icontextstrategy> ();
Public map<string, Icontextstrategy> Getcontextstrategy () {return
contextstrategy;
}
public void Setcontextstrategy (map<string, icontextstrategy> contextstrategy) {
this.contextstrategy = contextstrategy;
}
Public icontextstrategy dostrategy (String type) {return
this.contextStrategy.get (type);
}
}
Test:
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 ("John");
}
Here is the basic implementation, if there are complex business requirements, can be combined to implement specific methods,
The following is the configuration of XML, not adding an implementation class to the map plus a key value pair, the special trouble:
<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 "/>