一天一种设计模式之一-----策略模式

一.策略模式定义

  1. 策略模式属于三种类型设计模式中的行为模式(另外两种是建立型模式和结构型模式)
    java

  2. 策略模式定义了算法家族,分别封装起来,让他们之间能够互相替换,此模式让算法的变化不会影响到使用算法的客户。算法

  3. 该模式通常须要定义一个父类(算法类),定义该类要实现业务的抽象方法(计算返回结果),而后定义N个子类去实现该方法,经过上下文类(context)声明一个该父类的成员,建立上下文的时候根据传入的string用生成子类赋到父类成员上,再在上下文类中声明一个方法,方法中调用业务方法便可.设计模式

  4. 应用场景:1.以不一样格式保存文件;2.以不一样算法压缩文件。3.以不一样算法截获图像。4.以不一样格式输出一样数据的图形,如曲线或框图等ide

  5. 缺点:下述代码暴露给客户端的有Context,celve,celve的子类,耦合性很高;每增长一个策略就要多写一个子类。测试

  6. 优势:完美支持开放-封闭原则,里氏替换原则和单一职责原则。
    this

二.测试代码

package com.me.celvemoshi.test;

/**
 * 工场模式是经过工场new出来一个Celve 而策略模式是经过上下文传递,直接在上下文中建立声明一个Celve,并经过构造方法把Celve穿进去,
 * 最后调用相应的方法
 * 
 * @author liujun E-mail:
 * @version 建立时间:2016年2月21日 下午5:22:23
 */
public class CelveTest {
    public static void main(String[] args) {
        // 客户端调用
        Context context;
        context = new Context(new DazheCelve(0.8));
        System.out.println(context.getResult(110d));
        context = new Context(new FanliCelve(300d, 100d));
        System.out.println(context.getResult(400d));
        context = new Context(new FanliCelve(300d, 100d));

    }
}

class Context {
    Celve celve;

    public Context(Celve celve) {
        this.celve = celve;
    }

    public double getResult(Double money) {
        return celve.getResult(money);
    }
}

abstract class Celve {
    public abstract double getResult(Double money);
}

/**
 * 打折
 * 
 * @author 58
 *
 */
class DazheCelve extends Celve {
    private double rate;

    public DazheCelve(double rate) {
        this.rate = rate;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    @Override
    public double getResult(Double money) {
        return money * rate;
    }

}

/**
 * 打折并满减
 * 
 * @author
 */
class DazheAndFanli extends Celve {
    private double rate;
    private double moneyLimit;
    private double moneyReturn;

    public DazheAndFanli(double rate, double moneyLimit, double moneyReturn) {
        this.rate = rate;
        this.moneyLimit = moneyLimit;
        this.moneyReturn = moneyReturn;
    }

    @Override
    public double getResult(Double money) {
        double tempResult = money * rate;
        if (tempResult >= moneyLimit) {
            tempResult -= moneyReturn;
        }
        return tempResult;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public double getMoneyLimit() {
        return moneyLimit;
    }

    public void setMoneyLimit(double moneyLimit) {
        this.moneyLimit = moneyLimit;
    }

    public double getMoneyReturn() {
        return moneyReturn;
    }

    public void setMoneyReturn(double moneyReturn) {
        this.moneyReturn = moneyReturn;
    }

}

/**
 * 满减
 * 
 * @author 58
 *
 */
class FanliCelve extends Celve {
    private double moneyLimit;
    private double moneyReturn;

    public FanliCelve(double moneyLimit, double moneyReturn) {
        this.moneyLimit = moneyLimit;
        this.moneyReturn = moneyReturn;
    }

    public double getMoneyLimit() {
        return moneyLimit;
    }

    public void setMoneyLimit(double moneyLimit) {
        this.moneyLimit = moneyLimit;
    }

    public double getMoneyReturn() {
        return moneyReturn;
    }

    public void setMoneyReturn(double moneyReturn) {
        this.moneyReturn = moneyReturn;
    }

    @Override
    public double getResult(Double money) {
        if (money > moneyLimit) {
            money = money - moneyReturn;
        }
        return money;
    }

}
相关文章
相关标签/搜索