* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3.2.8策略模式 strategy模式参与者 Strategy:定义所有支持算法的公共接口。Context使用该接口来调用ConcreteStrategy定义的算法。 ConcreteStrategy:用Strategy接口实现具体算法。 Context:用一个ConcreteStrategy对象来配置。维护一个Strategy对象的引用。可定义一个接口使Stategy访问它的数据。 * 3.2.8策略模式 实例:电子商务网站购物车系统,不同商品不同促销方法 Strategy public abstract class Strategy { public abstract void method(); } ConcreteStrategy public class strategyImplA extends Strategy { public void method() { System.out.println(“A折扣10%); }} public class StrategyImplB extends Strategy { public void method() { System.out.println(“B折扣18%); }} public class StrategyImplC extends Strategy { public void method() { System.out.println(“C折扣60%); }} * 3.2.8策略模式, 实例: Context public class Context { Strategy stra; public Context(Strategy stra) { this.stra = stra; } public void doMethod() { stra.method(); } } Test(client) public class Test { public static void main(String[] args) { Context ctx = new Context(new StrategyImplA()); ctx.doMethod(); ctx = new Context(new StrategyImplB()); ctx.doMethod(); ctx = new Context(new StrategyImplC()); ctx.doMethod(); }} * 3.2.9装饰模式 decorator模式动态地给一个对象添加一些额外的职责。 适用性 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 职责是可以撤消的。 当不能采用子类方法进行扩充时。 * 3.2.9装饰模式 decorator模式结构 * 3.2.9装饰模式 decorator模式行为 Decorator 将请求转发给其 Component对象。 在转发请求之前、之后,Decorator 可以选择性地执行一些附加的操作。 * 3.2.9装饰模式 decorator模式参与者 Component:定义对象接口,这些对象可以动态地添加职责。 ConcreteComponent:定义对象,这些对象可以附加一些职责。 Decorator:维持一个指向Component对象的引用,并定义一个与Component接口一致的接口。 ConcreteDecorator:向Component添加职责。 * 3.2.9装饰模式 实例:图形界面工具箱(GUI toolkit) 如一个TextView对象(在窗口中显式文本,默认没有滚动条、边框)。当TextView需要时,可以用附加上滚动条ScrollDecorator。同样方法,可以附加上边框BorderDecorator。 这样的结果可以对TextView使用decorators实现。 * 3.2.9装饰模式 实例:图形界面工具箱(GUI tool
您可能关注的文档
- 讲座中小学体育课的设计.ppt
- 教学指导意见变化(简洁省培).ppt
- 阶段复习指导(一).ppt
- 届大洋洲与澳大利亚.ppt
- 金地亚竞品报告.ppt
- 金属工艺学绪论第一章.ppt
- 紧急救护(培训).ppt
- 经济政治与社会中职.ppt
- 九级历史第一轮复习九下第一单元.ppt
- 九芝堂营销策略企划案(终稿).ppt
- 广东省广州省实验中学教育集团2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州大学附属中学2025-2026学年八年级上学期奥班期中物理试题(解析版).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(含答案).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(解析版).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 2026《中国人寿上海分公司营销员培训体系优化研究》18000字.docx
- 《生物探究性实验教学》中小学教师资格模拟试题.docx
最近下载
- 2025统编版小学道德与法治三年级下册教学计划、教学设计附教学反思(附目录).docx
- 2026年廉政答案考试题库(考试直接用).docx VIP
- 虚列费用培训课件.ppt VIP
- Z20名校联盟(浙江省名校新高考研究联盟)2026届高三第二次联考日语含答案.doc VIP
- 煤矿2026年机电工作计划方案.docx VIP
- 企业合规法律服务指引(湖北省律师协会法律顾问专委会).pdf VIP
- Qu系列说明书QU16&QU24(加强版).pdf VIP
- 数学与生活:一元二次方程的起源.pptx VIP
- 事业单位d类综合应用能力必背知识点.pdf VIP
- (课堂用)TheFutureoftheEnglish(课件.ppt VIP
原创力文档

文档评论(0)