码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之策略模式

时间:2016-05-28 15:53:53      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

策略模式

策略模式:是指某个指定对象有某个行为,但是在不同的场景中,该行为有不同的实现方式。通过将不同的实现方式独立实现,并使他们之间可互相替换,从而使的不同方式独立出来,供使用者去选择。

UML如图:

技术分享

下面使用策略模式实现一个简单的算术运算:

package strategy;

/**
 * 运算符的抽象描述
 * @author wxh
 * @vertion 1.0
 */
public interface Operator {

    int execute(int num1, int num2);
    
}
package strategy;

/**
 * 加法运算
 * @author wxh
 * @vertion 1.0
 */
public class Plus implements Operator{

    @Override
    public int execute(int num1, int num2) {
        return num1 + num2;
    }

}
package strategy;

/**
 * 减法运算
 * @author wxh
 * @vertion 1.0
 */
public class Minus implements Operator {

    @Override
    public int execute(int num1, int num2) {
        return num1 - num2;
    }

}
package strategy;

/**
 * 计算器
 * @author wxh
 * @vertion 1.0
 */
public class Calculator {
    
    //数字1
    private int number1;
    
    //数字2
    private int number2;
    
    //运算符
    private Operator operator;

    //返回操作结果
    public int getResult() {
        return operator.execute( number1, number2 );
    }

    public int getNumber1() {
        return number1;
    }

    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    public int getNumber2() {
        return number2;
    }

    public void setNumber2(int number2) {
        this.number2 = number2;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }
    
}
package strategy;

public class Test {
    
    public static void main(String[] args) {
        
        Calculator cal = new Calculator();
        cal.setNumber1( 20 ); 
        cal.setNumber2( 30 );
        //设置运算符
        cal.setOperator( new Plus() );
        System.out.println( cal.getResult() );
        
    }

}

执行结果:50 

策略模式 + 模板方法结合使用

/**
 * 计算器
 * @author wxh
 * @vertion 1.0
 */
public class Calculator {

    private int number1;
    
    private int number2;

    public int getNumber1() {
        return number1;
    }

    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    public int getNumber2() {
        return number2;
    }

    public void setNumber2(int number2) {
        this.number2 = number2;
    }
    
}
/**
 * 模板方法
 * @author wxh
 * @vertion 1.0
 */
public abstract class Operator {

    private Calculator cal;
    
    public Operator( Calculator cal ) {
        this.cal = cal;
    }
    
    protected abstract int execute( int num1, int num2 );
    
    public int doExecute() {
        return execute( cal.getNumber1( ), cal.getNumber2( ) );
    }
    
}
/**
 * 加法运算
 * @author wxh
 * @vertion 1.0
 */
public class Plus extends Operator{
    public Plus(Calculator cal) {
        super(cal);
    }

    @Override
    protected int execute(int num1, int num2) {
        return num1 + num2;
    }
 
}
public class Test01 {
    
    public static void main(String[] args) {
        Calculator cal = new Calculator();
        cal.setNumber1(30);
        cal.setNumber2(20);
        Plus p = new Plus(cal); //加法
        //Minus m = new Minus(cal); //减法
        System.out.println( p.doExecute() );
        
    }
    
}

 执行结果:50 

设计模式之策略模式

标签:

原文地址:http://www.cnblogs.com/dahao1020/p/5537492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!