/** * @Title:商品抽象折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午03:24:53 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public interface MemberStrategy { public double discountPrice(double bookPrice); }
/** * @Title:初级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午03:53:59 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class JuniorMemberStrategy implements MemberStrategy{ public double discountPrice(double bookPrice) { return bookPrice; } }
/** * @Title:中级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午03:58:01 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class InterMediateMemberStrategy implements MemberStrategy{ @Override public double discountPrice(double bookPrice) { return bookPrice*0.9; } }
/** * @Title:高级会员--折扣类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午03:59:30 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class AdvancedMemberStrategy implements MemberStrategy{ @Override public double discountPrice(double bookPrice) { return bookPrice*0.8; } }
/** * @Title:图书价格类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午04:15:57 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class BookPrice { private MemberStrategy strategy; private double price; /** * @param strategy */ public BookPrice(MemberStrategy strategy,double price) { super(); this.strategy = strategy; this.price=price; } /** * * getRealBookPrice(获取折扣后的价格) * @return double * @createDate 2013-3-12 下午04:30:26 * @since 1.0.0 */ public double getRealBookPrice() { return strategy.discountPrice(this.price); } public MemberStrategy getStrategy() { return strategy; } public void setStrategy(MemberStrategy strategy) { this.strategy = strategy; } }
/** * @Title:图书打折后的价格--测试类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午04:17:23 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class TestBookPrice { public static void main(String[] args) { MemberStrategy strategy=new AdvancedMemberStrategy(); BookPrice bookPrice = new BookPrice(strategy,99.0); System.out.println("图书价格是:"+bookPrice.getRealBookPrice()); } }
/** * @Title:模版方法抽象类--定义一个业务的骨架,而将一些实现步骤延迟到子类中 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午02:10:38 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public abstract class AbstractTemplate { /** * * executeMain(需要在子类实现) * @createDate 2013-3-12 下午02:12:43 * @since 1.0.0 */ public abstract void executeMain(); public void execute() { beforeAction(); executeMain(); afterAction(); } public void beforeAction() { System.out.println("before Action..."); } public void afterAction() { System.out.println("after Action..."); } }
/** * @Title:模版方法子类--实现其中可变的部分 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午02:13:09 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class TemplateImpl extends AbstractTemplate{ public void executeMain() { System.out.println("核心操作处理...实现方式一"); } }
/** * @Title:模版方法测试类 * @Description:TODO * @Company: Orclight's Studio * @author: shuzl 2013-3-12 下午02:14:18 * @motto: Never put off what you can do today until Tomorrow * @version 1.0.0 */ public class TestTemplate { public static void main(String[] args) { AbstractTemplate temp = new TemplateImpl(); temp.execute(); } }