Home > 类库下载 > java类库 > body text

Spring and Strategy Pattern

阿神
Release: 2016-11-07 17:45:48
Original
1979 people have browsed it

1: Definition of Strategy Pattern

The strategy pattern is a packaging of the algorithm, which separates the responsibility of using the algorithm from the algorithm itself and delegates it to different object management. The strategy pattern usually packages a series of algorithms into a series of strategy classes, as a subclass of an abstract strategy class.

The class diagram is as follows:

Spring and Strategy Pattern

If the strategy pattern is to be implemented using JAVA classes, the source code is as follows:

Java code

/** 
 * 
 * 策略执行 
 * @author weique.lqf 
 * @version $Id: Context.java, v 0.1 2014-2-9 下午2:32:56 weique.lqf Exp $ 
 */  
public class Context {  
    private Strategy stg;  
   
    public Context(Strategy theStg) {  
        this.stg = theStg;  
    }  
   
    public void doAction() {  
        this.stg.testStrategy();  
    }  
}
Copy after login

Strategy interface:

Java代码  
/** 
 * 
 * 
 * @author weique.lqf 
 * @version $Id: Strategy.java, v 0.1 2014-2-9 下午2:32:17 weique.lqf Exp $ 
 */  
public interface Strategy {  
   
    void testStrategy();  
}
Copy after login

Implementation class one :

Java code

package com.proxy.strategy.impl;  
   
import com.proxy.strategy.Strategy;  
   
public class PrintStrategy implements Strategy {  
   
    public void testStrategy() {  
        System.out.print("我要打印!!");  
    }  
   
}
Copy after login

Implementation category two:

Java code

package com.proxy.strategy.impl;  
   
import com.proxy.strategy.Strategy;  
   
public class WriteStrategy implements Strategy {  
   
    public void testStrategy() {  
        System.out.println("我要写字!!!");  
    }  
   
}
Copy after login

Execution code:

Java code

package com.proxy.strategy;  
   
import com.proxy.strategy.impl.PrintStrategy;  
   
public class StrategyClient {  
   
    public static void main(String[] args) {  
        Strategy stgA = new PrintStrategy();  
        Context ct = new Context(stgA);  
        ct.doAction();  
    }  
}
Copy after login

Two: spring implementation strategy pattern

Now The system using spring can There are so many, so how to implement strategies in spring mode?

In fact, it only needs a little modification, because one of the cores of spring is IOC.

First modify the Contex class:

Java code

package com.proxy.strategy;  
public class ContextSpring {  
    private Strategy stg;  
   
    /** 
     * Setter method for property <tt>stg</tt>. 
     * 
     * @param stg value to be assigned to property stg 
     */  
    public void setStg(Strategy stg) {  
        this.stg = stg;  
    }  
   
    public void doAction() {  
        this.stg.testStrategy();  
    }  
}
Copy after login

Then configure it in the spring configuration file,

Xml code

<bean id="ct" class = "com.proxy.strategy.ContextSpring">  
      <property name="stg" ref="writeStg"/>  
   </bean>  
   <bean id="writeStg" class = "com.proxy.strategy.impl.WriteStrategy"/>  
   <bean id="printStg" class = "com.proxy.strategy.impl.PrintStrategy"/>
Copy after login

Select the implementation class you want to inject, and then write this in the executed code :

Java code

package com.proxy.strategy;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
   
public class StrategySpringClient {  
   
    public static void main(String[] args) {  
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  
        ContextSpring ct = (ContextSpring) context.getBean("ct");  
        ct.doAction();  
    }  
}
Copy after login

Look, spring is introduced in this way.

But this has advantages and disadvantages. If I want to apply according to different types, for example: contracts need to be printed, while love letters need to be handwritten. Assuming that the contract is type 2 and the love letter is type 1, how do we automatically adapt?

Three: Advanced version of spring strategy mode

First modify the Context class:

Java code

package com.proxy.strategy;  
import java.util.HashMap;  
import java.util.Map;    
/** 
 * 
 * 
 * @author weique.lqf 
 * @version $Id: ContextSpringFactory.java, v 0.1 2014-2-9 下午3:46:09 weique.lqf Exp $ 
 */  
public class ContextSpringFactory {  
    private Map<String, Strategy> stgMap = new HashMap<String, Strategy>();  
    /** 
     * Getter method for property <tt>stgMap</tt>. 
     * 
     * @return property value of stgMap 
     */  
    public Map<String, Strategy> getStgMap() {  
        return stgMap;  
    }  
    /** 
     * Setter method for property <tt>stgMap</tt>. 
     * 
     * @param stgMap value to be assigned to property stgMap 
     */  
    public void setStgMap(Map<String, Strategy> stgMap) {  
        this.stgMap = stgMap;  
    }  
    public void doAction(String strType) {  
        this.stgMap.get(strType).testStrategy();  
    }  
}
Copy after login

Then modify the spring configuration file:

Xml code

<bean id="ctf" class = "com.proxy.strategy.ContextSpringFactory">  
      <property name="stgMap">   
         <map>   
              <entry key="1" value-ref="writeStg"/>   
              <entry key="2" value-ref="printStg"/>    
         </map>   
      </property>   
   </bean>
Copy after login

The entry class modification performed For:

Java code

package com.proxy.strategy;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class StrategySpringClientFactory {  
    public static void main(String[] args) {  
        //外部参数  
        String type = "1";  
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  
        ContextSpringFactory ctf = (ContextSpringFactory) context.getBean("ctf");  
        ctf.doAction(type);  
        //type 2  
        type = "2";  
        ctf.doAction(type);  
    }  
}
Copy after login

Then run it and see what the results will be?

Spring and Strategy Pattern

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!