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

策略模式

时间:2017-07-23 00:57:51      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:factor   选择算法   com   实现   play   roo   使用   ring   pat   

  1.定义:

    在系统设计时,要实现一个功能有多种方式,每一种方式称为一个策略,我们可以根据不同的环境和条件

  来选择不同的策略来完成该功能。例如去商场购物,平常是没有优惠的,采取的是普通的收费策略,但是当要

  要进行促销活动时,就要采取打折的策略。策略模式是一种行为型模式。

  2.模式的结构

  技术分享

  (1)Client:使用策略的角色,它在解决问题时可以采取多种策略,在客户端类中维护一个抽象策略的引用的实例

  (2)PayStrategy:抽象的支付策略接口

  (3)CommonPayStrategy,DiscountPayStrategy:具体的策略实现类

  3.代码

  (1)PayStragegy.java

技术分享
/**
 * 抽象支付策略
 * @author Administrator
 *
 */
public interface PayStrategy {
    public void pay(Account account, double cost);
}
View Code

  (2)CommonPayStrategy.java  

技术分享
/**
 * 普通的策略类
 * @author Administrator
 *
 */
public class CommonPayStrategy implements PayStrategy{
    
    @Override
    public void pay(Account account, double cost) {
        // TODO Auto-generated method stub
        account.setBalance(account.getBalance() - cost);
    }

}
View Code

  (3)DiscountPayStraygy.java  

技术分享
/**
 * 打折的策略类
 * @author Administrator
 *
 */
public class DiscountPayStrategy implements PayStrategy{
    
    @Override
    public void pay(Account account, double cost) {
        // TODO Auto-generated method stub
        double discount = 0.8;//八折
        cost = discount * cost;
        account.setBalance(account.getBalance() - cost);
    }

}
View Code

  (4)Client.java

技术分享
public class Client {
    private PayStrategy payStrategy;

    public PayStrategy getPayStrategy() {
        return payStrategy;
    }

    public void setPayStrategy(PayStrategy payStrategy) {
        this.payStrategy = payStrategy;
    }
    
    public static void main(String[] args) {
        Client client = new Client();
        Account account = new Account(1000, "uptowncat");
        PayStrategy payStrategy = (PayStrategy)XmlUtil.getBean();
        client.setPayStrategy(payStrategy);
        client.algorithm(account, 100);
        System.out.println(account);
    }
    
    public void algorithm(Account account, double cost) {
        payStrategy.pay(account, cost);
    }
    
    
}
View Code

  (5)Account.java

技术分享
public class Account {
    private double balance;
    private String name;

    public Account(int balance, String name) {
        super();
        this.balance = balance;
        this.name = name;
    }

    public Account() {
        super();
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Account [balance=" + balance + ", name=" + name + "]";
    }
    

}
View Code

  (6)XmlUtil.java

技术分享
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 辅助工具类
 * @author Administrator
 *
 */
public class XmlUtil {
    public static final Object getBean() {
        try {
            String path = XmlUtil.class.getResource("/com/uptowncat/strategy/config.xml").getPath();
            Document doc = new SAXReader().read(path);
            Element root = doc.getRootElement();
            String className = root.element("className").getStringValue();
            Class<?> factory = Class.forName(className);
            return factory.newInstance();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
}
View Code

  (7)config.xml

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <className>com.uptowncat.strategy.CommonPayStrategy</className>
</config>
View Code

  4.优缺点

    优点:

      (1)符合开闭原则,可以在不修改原有系统上选择算法行为,增强系统的灵活性

      (2)使用策略模式可以避免使用多重的条件转移语句

    缺点:策略模式在有的情况下可能会产生很多的策略类和对象,在一定程度上会增加系统的复杂性。

 

   参考资料:《设计模式》刘伟

  Well begin is a half done!

策略模式

标签:factor   选择算法   com   实现   play   roo   使用   ring   pat   

原文地址:http://www.cnblogs.com/uptowncat/p/7223312.html

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