zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:企业应用开发的思考和策略_Strategy

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class DiscountContext
    {
        // 组合一个DiscountStrategy对象
        private DiscountStrategy strategy;
        // 构造器,传入一个DiscountStrategy对象
        public DiscountContext(DiscountStrategy strategy)
        {
            this.strategy  = strategy;
        }
        // 根据实际所使用的DiscountStrategy对象得到折扣价
        public double getDiscountPrice(double price)
        {
            // 如果strategy为null,系统自动选择OldDiscount类
            if (strategy == null)
            {
                strategy = new OldDiscount();
            }
            return this.strategy.getDiscount(price);
        }
        // 提供切换算法的方法
        public void changeDiscount(DiscountStrategy strategy)
        {
            this.strategy = strategy;
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public interface DiscountStrategy
    {
    	// 定义一个用于计算打折价的方法
    	double getDiscount(double originPrice);
    }
    

      

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class OldDiscount
    	implements DiscountStrategy
    {
    	// 重写getDiscount()方法,提供旧书打折算法
    	public double getDiscount(double originPrice)
    	{
    		System.out.println("使用旧书折扣...");
    		return originPrice * 0.7;
    	}
    }
    

      

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class OldDiscount
    	implements DiscountStrategy
    {
    	// 重写getDiscount()方法,提供旧书打折算法
    	public double getDiscount(double originPrice)
    	{
    		System.out.println("使用旧书折扣...");
    		return originPrice * 0.7;
    	}
    }
    

      

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class StrategyTest
    {
    	public static void main(String[] args)
    	{
    		// 客户端没有选择打折策略类
    		DiscountContext dc = new DiscountContext(null);
    		double price1 = 79;
    		// 使用默认的打折策略
    		System.out.println("99元的书默认打折后的价格是:"
    			+ dc.getDiscountPrice(price1));
    		// 客户端选择合适的VIP打折策略
    		dc.changeDiscount(new VipDiscount());
    		double price2 = 89;
    		// 使用VIP打折得到打折价格
    		System.out.println("109元的书对VIP用户的价格是:"
    			+ dc.getDiscountPrice(price2));
    	}
    }
    

      

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    // 实现DiscountStrategy接口,实现对VIP打折的算法
    public class VipDiscount
    	implements DiscountStrategy
    {
    	// 重写getDiscount()方法,提供VIP打折算法
    	public double getDiscount(double originPrice)
    	{
    		System.out.println("使用VIP折扣...");
    		return originPrice * 0.5;
    	}
    }
    

      

  • 相关阅读:
    C#获取类以及类下的方法(用于Asp.Net MVC)
    ES6学习笔记
    在nuget上发布自己的程序集教程
    C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式
    ASP.Net Mvc实现自定义User Identity用户身份识别系统(2)
    ASP.Net Mvc实现自定义User Identity用户身份识别系统(1)
    C#实现.ini文件读写操作
    C#实现注册表 LocalMachine 目录下CURD工具类
    博客园打赏功能(未申请下js权限使用二维码打赏功能)
    WebServeice 动态代理类
  • 原文地址:https://www.cnblogs.com/tszr/p/12376575.html
Copyright © 2011-2022 走看看