策略(Strategy)模式
编程技术  /  houtizong 发布于 3年前   130
//环境角色public class Context{private Strategy strategy;//策略方法public void contextInterface(){strategy.strategyInterface();}}//抽象策略类public abstract class Strategy{//策略方法public abstract void strategyInterface();} //具体策略类public class ConcreteStrategy extends Strategy{public void strategyInterface(){//............}}二、一个图书折扣店的例子//抽象折扣类public abstract class DiscountStrategy{private single price = 0;private int copies = 0;//策略方法public abstract single calculateDiscount();public DiscountStrategy(single price , int copies){this.price = price;this.copies = copies;}}//具体折扣类1(没有折扣)public class NoDiscountStrategy extends DiscountStrategy{private single price = 0;private int copies = 0;public NoDiscountStrtegy(single price, int copies){this.price = price;this.copies = copies;}public single calculateDiscount(){return 0;}}//具体折扣类2public class FlatRateStrategy extends DiscountStrategy{private single price = 0;private int copies = 0;private single amount;public FlatRateStrategy(single price , int copies){this.price = price;this.copies = copies;}public single getAmount(){return amount;}public void setAmount(single amount){this.amount = amount;}public single calculateDiscount(){return copies*amount;}}//具体折扣类3public class PercentageStrategy extends DiscountStrategy{private single percent;private single price = 0;private int copies = 0;public PercentageStrategy(single price , int copies){this.price = price;this.copies = copies;}public single getPercent(){return percent;}public void setPercent(single percent){this.percent = percent;}public single calculateDiscount(){return copies*price*percent;}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接