策略模式

 

 

 

策略模式主要有两个类:

1.角色:作为使用各种策略的对象

2.策略:各种策略类统一的上层接口,定义统一的算法接口方法

角色通过 组合 的方式使用各种策略

实例1: 角色类:国王,王后 (角色) 策略类:各种武器

Character

package Strategy;

/**
 * 角色抽象类
 * 拥有武器,可以进行攻击
 * @author TisaKong
 * 2015-6-4
 */
public abstract class Character {

    WeaponBehavior weapon;

    abstract void fight();

    public void setWeapon(WeaponBehavior weapon) {
        this.weapon = weapon;
    }


}

King

package Strategy;

public class King extends Character{

    public King() {
        weapon = new KnifeBehavior();
    }

    @Override
    void fight() {
        weapon.useWeapon();
    }

}

Queen

package Strategy;

public class Queen extends Character {


    public Queen() {
        weapon = new BowAndArrowBehavior();
    }

    @Override
    void fight() {
        weapon.useWeapon();
    }

}

weapon

package Strategy;

/**
 * 武器接口
 * @author TisaKong
 * 2015-6-4
 */
public interface WeaponBehavior {
    public void useWeapon();
}

Knife

package Strategy;

public class KnifeBehavior implements WeaponBehavior {

    @Override
    public void useWeapon() {
        System.out.println("use knife to kill enemies!");
    }

}

BowAndArrow

package Strategy;

public class BowAndArrowBehavior implements WeaponBehavior {

    @Override
    public void useWeapon() {
        System.out.println("use BowAndArrow to kill enemies!");
    }

}

客户端测试

package Strategy;


public class Client {

    public static void main(String[] args) {

        Character king = new King();
        king.fight();

        Character queen = new Queen();
        queen.fight();

    }
}


实例2:

顾客到商场购物

商品抽象类Things

package Strategy;

public abstract class Things {
    protected String name;
    protected double price;

    public Things() {
    }

    public Things(String name , double price) {
        this.name = name;
        this.price = price;
    }

    abstract double discount(double originalPrice);

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


}

衣服Clothes

package Strategy;

public class Clothes extends Things{

    public Clothes(String name, double price) {
        super(name, price);

    }

    @Override
    public double discount(double originalPrice) {
        double paid = Discount.CLOTHES_DISCOUNT*originalPrice;
        return paid;
    }

}

裤子Pants

package Strategy;

public class Pants extends Things{

    public Pants(String name, double price) {
        super(name, price);

    }

    @Override
    double discount(double originalPrice) {
        return Discount.PANTS_DISCOUNT*originalPrice;
    }

}

电子产品Electronics

package Strategy;

public class Electronics extends Things {

    public Electronics(String name, double price) {
        super(name, price);

    }

    @Override
    double discount(double originalPrice) {
        return Discount.ELECTRONICS_DISCOUNT*originalPrice;
    }

}

商城Market

package Strategy;

public class Market {
    Storage myStorage = new Storage();

    /**
     * 初始化时将货物装载入库
     */
    public Market(){
        myStorage.install();
    }

    /**
     * 从仓库中提取货物
     * @return
     */
    public Things getClothesFormStorage(){
        Clothes clothes = myStorage.clothesStorage.get(0);
        myStorage.clothesStorage.remove(0);
        return clothes ;
    }
    public Things getPantsFormStorage(){
        Pants pants = myStorage.pantsStorage.get(0);
        myStorage.pantsStorage.remove(0);
        return pants ;
    }
    public Things getElectronicsFormStorage(){
        Electronics electronics = myStorage.electronicsStorage.get(0);
        myStorage.electronicsStorage.remove(0);
        return electronics ;
    }


    public Things buy(String name) {
        //购买产品的类型
        Things product = null;
        if(name.equalsIgnoreCase("clothes")){
            product = this.getClothesFormStorage();
            product.setPrice(product.discount(product.getPrice()));
        }
        else if(name.equalsIgnoreCase("pants")){
            product = this.getPantsFormStorage();
            product.setPrice(product.discount(product.getPrice()));
        }
        else if(name.equalsIgnoreCase("electronics")){
            product = this.getElectronicsFormStorage();
            product.setPrice(product.discount(product.getPrice()));
        }
        return product;

    }

}

商城的商品储藏室

package Strategy;

import java.util.ArrayList;
import java.util.List;


public class Storage {
    /*
     * 仓库内衣服的数量
     */
    private static int NUM_CLOTHES = 100;
    /*
     * 仓库内裤子的数量
     */
    private static int NUM_PANTS = 200;
    /*
     * 仓库内电子产品的数量
     */
    private static int NUM_ELECTRONICS = 50;

    /**
     * 存储产品的集合类
     */



    public List<Clothes> clothesStorage = new ArrayList<Clothes>();
    public List<Pants> pantsStorage = new ArrayList<Pants>();
    public List<Electronics> electronicsStorage = new ArrayList<Electronics>();;

    /**
     * 装产品
     */
    public void install(){
        for(int i=0 ; i<NUM_CLOTHES ; i++){
            clothesStorage.add(new Clothes("Clothes"+(i+1), new Random().random()));
        }
        for(int i=0 ; i<NUM_PANTS ; i++){
            pantsStorage.add(new Pants("Pants"+(i+1),  new Random().random()));
        }
        for(int i=0 ; i<NUM_ELECTRONICS ; i++){
            electronicsStorage.add(new Electronics("Electronic"+(i+1) ,  new Random().random()));
        }
    }

/*  public static void main(String[] args) {
        for(int i=0;i<10;i++){
            System.out.println(new Random().random());
        }
    }*/

}

/**
 * 价格随机数类
 * @author TisaKong
 * 2015-2-2
 */
class Random{
    public  double random(){
        java.util.Random r = new java.util.Random();
        r.setSeed(System.nanoTime());
        return r.nextDouble()*100;
    }
}

顾客Customer

package Strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Customer {
    /**
     * 去某个商场 买几样东西
     * @param market
     * @return
     */
    public List<Things> goShopping(Market market , Map<String , Integer > ... products){
        //获取购买商品的数量
        int numClothes=0 , numPants=0 , numElectronics=0 , sum=0;
        for(Map<String ,Integer > product : products){
            if(product.get("clothes")!=null){
                numClothes = product.get("clothes");
            }else if(product.get("pants") != null){
                numPants = product.get("pants");
            }else if(product.get("electronics") != null){
                numElectronics = product.get("electronics");    
            }
        }
        List<Things> haveBuy = new ArrayList<Things>();
        for(int i=0;i<numClothes;i++){
            haveBuy.add(market.buy("clothes"));
        }
        for(int i=0;i<numPants;i++){
            haveBuy.add(market.buy("pants"));
        }
        for(int i=0;i<numElectronics;i++){
            haveBuy.add(market.buy("electronics"));
        }

        return haveBuy;
    }
}

商品打折信息

package Strategy;

public class Discount {
    public static final double CLOTHES_DISCOUNT = 0.8;
    public static final double PANTS_DISCOUNT = 0.65;
    public static final double ELECTRONICS_DISCOUNT = 0.9;
}

 

posted @ 2015-06-05 14:27  克什米尔公子  阅读(123)  评论(0)    收藏  举报