策略模式

1.先写Test

Apple() a = new Apple(0.3,1);//一个0.3斤、1块钱的苹果

Market.discount(a);//超市打折

Market.sell();//超市卖出

package dp;

public class Market {

    public static void discount(Apple a) {
        a.setPrice(a.getPrice()*0.8);
    }

    public static void sell(Apple a) {
        System.out.println("卖出苹果"+a.getPrice());
    }

}
Market

 

2.那么问题出现了,超市有很多很多东西,每个东西都要打折,Market要给每个商品写一个打折方法,和一个卖出方法么?

显然不是,为了解决这个问题,怎么办呢

1)新建一个接口,可以打折的接口:Discountable

package dp;

public interface Discountable {

    public void discount(Object o) throws Exception ;
    
    public void sell(Object o)throws Exception;
}
discountable

2)所有的打折商品都实现这个类

package dp;

public class Apple implements Discountable{
    
    private double weight;
    private double price;
    
    public Apple(double weight,double price) {
        this.weight = weight;
        this.price = price;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public void discount(Object o) throws Exception {
        if(o instanceof Apple){
            Apple a = (Apple) o;
            a.setPrice(a.getPrice()*0.8);
        }else{
            throw new Exception();
        }
        
    }
    @Override
    public void sell(Object o) throws Exception {
        if(o instanceof Apple){
            Apple a = (Apple) o;
            System.out.println("卖出苹果"+a.getPrice());
        }else{
            throw new Exception();
        }
    }

}
Apple

3)超市方法变的通用

package dp;

public class Market {

    public static void discount(Object obj) {
        Discountable da = (Discountable) obj;
        try {
            da.discount(da);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void sell(Object obj) {
        Discountable da = (Discountable) obj;
        try {
            da.sell(da);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
Market

3.问题又来了,每一种商品不一定只有一种折扣,比如苹果,刚开业有8折,过几天变成买100斤以上,打8折

1)定义一个接口打折器:Discountor

package dp;

public interface Discountor {
    
    public void discountToObj(Object o);

}
Discountor

2)每种商品有几种打折方式,就写多少个实体类,实现Discountor

package dp;

public class AppleDiscountor1 implements Discountor{

    @Override
    public void discountToObj(Object o) {
        if(o instanceof Apple){
            Apple a = (Apple) o;
            a.setPrice(a.getPrice()*0.8);
        }
        
    }

}
AppleDiscountor1
package dp;

public class AppleDiscountor2 implements Discountor{

    @Override
    public void discountToObj(Object o) {
        if(o instanceof Apple){
            Apple a = (Apple) o;
            a.setPrice(a.getPrice()*0.9);
        }
        
    }

}
AppleDiscountor2

3)每种商品里加一个变量,用哪种打折器,就new 哪种打折器

package dp;

public class Apple implements Discountable{
    
    private double weight;
    private double price;
    
    Discountor dis = new AppleDiscountor2();
    
    public Apple(double weight,double price) {
        this.weight = weight;
        this.price = price;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public void discount(Object o) throws Exception {
        if(o instanceof Apple){
//            Apple a = (Apple) o;
//            a.setPrice(a.getPrice()*0.8);
            dis.discountToObj(o);
        }else{
            throw new Exception();
        }
        
    }
    @Override
    public void sell(Object o) throws Exception {
        if(o instanceof Apple){
            Apple a = (Apple) o;
            System.out.println("卖出苹果"+a.getPrice());
        }else{
            throw new Exception();
        }
    }

}
Apple

 

这就是一个完整的策略模式

 

posted @ 2016-03-10 21:07  刘尊礼  阅读(130)  评论(0)    收藏  举报