版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、STRATEGYSoftware Design Patterns - Strategy231 July 2022A simple SimUDuck appBut now we need the ducks to flySoftware Design Patterns - Strategy331 July 2022A flying duckSoftware Design Patterns - Strategy431 July 2022It does not look hard for us.Software Design Patterns - Strategy531 July 2022Somet
2、hing happened. Rubber ducks fly around the screen. However, rubber ducks are not supposed to be able to fly. Software Design Patterns - Strategy631 July 2022A great use of inheritance for the purpose of reuse hasnt turned out so well when it comes to maintenance.Software Design Patterns - Strategy73
3、1 July 2022Joe still hopes to use inheritanceSee, the problem is solved.Software Design Patterns - Strategy831 July 2022We can not see the whole situation!Local change may cause global effect! Software Design Patterns - Strategy931 July 2022How about using an interface if there will be more Duck sub
4、classes in the future?Only the ducks that are supposed to FLY will implement that interface and have a fly() method.Problem?Yes, too much duplicated code offly() and quack() in subclasses.Make them abstract classes rather than interface?Software Design Patterns - Strategy1031 July 2022Wouldnt it be
5、dreamy if only there were a way to build software so that when we need to change it, we could do so with the least possible impact on the existing code? Then we could spend less time reworking code and more making the program do cooler things.Software Design Patterns - Strategy1131 July 2022Take wha
6、t varies and encapsulate it so it wont affect the rest of your code.Result: Fewer unintended consequences from code changes and more flexibility in your systems!Software Design Patterns - Strategy1231 July 2022From now on, the Duck behaviors will live in a separate class - a class that implements a
7、particular behavior interface.The Duck classes wont need to know any of the implementation details for their own behaviors.Software Design Patterns - Strategy1331 July 2022We will use an interface to represent each behavior - for instance, FlyBehavior and QuackBehavior - and each implementation of a
8、 behavior will implement one of those interfaces.Result: It is the behavior class, rather than the Duck class, that will implement the behavior interface.It really means “Program to a supertype.”Software Design Patterns - Strategy1431 July 2022Programming to an implementation: Dog d = new Dog(); d.b
9、ark();Programming to an interface/supertype: Animal animal = new Dog(); animal.makeSound();a concrete implementation of AnimalWe know it is a Dog, but we can now use the animal reference polymorphically. animal = getAnimal(); animal.makeSound();We may assign a concrete object at runtime.Software Des
10、ign Patterns - Strategy1531 July 2022Implementing the Duck BehaviorsOther types of objects can reuse our fly and quack behaviors because these behaviors are no longer hidden away in our Duck classes!You are free to add new behaviors without modifying any existing Behavior or Duck classes.Software De
11、sign Patterns - Strategy1631 July 2022A Duck will now delegate its flying and quacking behavior, instead of using fly() and quack() methods defined in its own.Lets implement performQuack(): public abstract class Duck QuackBehavior quackBehavior; .public void performQuack() quackBehavior.quack();How
12、to set this field?Software Design Patterns - Strategy1731 July 2022Concrete duck class: public class MallardDuck extends Duck public MallardDuck() quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); public void display() System.out.println(Im a real Mallard duck.); A MallardDuck uses the
13、Quack class to handle its quack.And it uses FlyWithWings as its FlyBehavior type.Software Design Patterns - Strategy1831 July 2022Duck class: public abstract class Duck FlyBehavior flyBehavior; QuackBehavior quackBehavior; public Duck() abstract void display(); public void performFly() flyBehavior.f
14、ly(); public void performQuack() quackBehavior.quack(); public void swim() System.out.println(All ducks float, even decoys!); Other classes: public interface FlyBehavior public void fly(); public class FlyWithWings implements FlyBehavior public void fly() System.out.println(Im flying!); public inter
15、face QuackBehavior public void quack(); public class Quack implements QuackBehavior public void quack() System.out.println(Quack); FlyNoWayMuteQuackSqueakSoftware Design Patterns - Strategy1931 July 2022Testing: public class MiniDuckSimulator1 public static void main(String args) Duck mallard = new
16、MallardDuck(); mallard.performQuack(); mallard.performFly(); Runing:Software Design Patterns - Strategy2031 July 2022Can we set ducks behavior dynamically?Yes, in the Duck class: public void setFlyBehavior (FlyBehavior fb) flyBehavior = fb; public void setQuackBehavior(QuackBehavior qb) quackBehavio
17、r = qb; A new type of Duck class: public class ModelDuck extends Duck public ModelDuck() flyBehavior = new FlyNoWay(); quackBehavior = new Quack(); public void display() System.out.println(Im a model duck); A new behavior:public class FlyRocketPowered implements FlyBehavior public void fly() System.
18、out.println(Im flying with a rocket); Software Design Patterns - Strategy2131 July 2022Change behavior at runtimeTesting:public class MiniDuckSimulator1 public static void main(String args) Duck mallard = new MallardDuck(); mallard.performQuack(); mallard.performFly(); Duck model = new ModelDuck();
19、model.performFly(); model.setFlyBehavior(new FlyRocketPowered(); model.performFly(); Runing:Software Design Patterns - Strategy2231 July 2022Has-AImplementsIs-AInstead of inheriting their behavior, the ducks get their behavior by being composed with the right behavior object.Software Design Patterns
20、 - Strategy2331 July 2022The flexibility from composition: encapsulate a family of algorithms into their own set of classes; make it possible to change behavior at runtime.Software Design Patterns - Strategy2431 July 2022The Strategy Pattern defines a family of algorithms, encapsulates each one, and
21、 makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.Design Patterns give you a shared vocabulary with other developers and they also elevates your thinking about architectures by letting you think at the pattern level, not the nitty gritty object leve
22、l.Our first patternSoftware Design Patterns - Strategy2531 July 2022Software Design Patterns - Strategy2631 July 2022When to use?Many related classes with only different behaviorsVariants of an algorithmAlgorithms use data which customers should not knowMany if-else for many behaviorsSoftware Design
23、 Patterns - Strategy2731 July 2022Merits?Provide a way to replace inheritanceRemove if-elseProvide different implementations for the same behaviorSoftware Design Patterns - Strategy2831 July 2022Weakness?Customers should know all the strategiesCommunication expense between strategy and contextToo ma
24、ny strategy classesSoftware Design Patterns - Strategy2931 July 2022Knowing concepts like abstraction, inheritance, and polymorphism do not make you a good OO designer.Software Design Patterns - Strategy3031 July 2022Software Design Patterns - Strategy3131 July 2022 Knowing the OO basics does not ma
25、ke you a good OO designer. Good OO designs are reusable, extensible and maintainable. Patterns are not invented, they are discovered. Patterns dont give you code, they give you general solutions to design problems. Software Design Patterns - Strategy3231 July 2022练习:一个电子商务系统,其中有一个控制器对象(TaskControlle
26、r),用于处理销售请求,能够确认何时有人在请求销售订单,并将请求转给SalesOrder对象处理。SalesOrder对象的功能包括:允许客户通过GUI填写订单,处理税额的计算,处理订单和打印销售收据。新需求:要处理多种税额计算的方法,美国、加拿大、中国三个国家的税收方法Software Design Patterns - Strategy3331 July 2022Software Design Patterns - Strategy3431 July 2022Software Design Patterns - Strategy3531 July 2022练习:某电影院售票系统为不同类型的
27、用户提供不同打折方式(Discount),学生凭学生证享受8折优惠(StudentDiscount),儿童享受减免10元优惠(ChildrenDiscount),VIP用户除享受半价优惠还可积分(VIPDiscount)。Software Design Patterns - Strategy3631 July 2022Class MovieTicket private double Price; private Discount discount; public void setPrice(double price) this.price=price; public void setDisco
28、unt(Discount discount) this.discount= discount; public double getPrice() return discount.calculate(this.price);Software Design Patterns - Strategy3731 July 2022Interface Discount public double calculate(double price); Class StudentDiscrount implement Discount public double calculate(double price) re
29、turn price*0.8; Software Design Patterns - Strategy3831 July 2022Class ChildrenDiscrount implement Discount public double calculate(double price) return price-10; Class VIPDiscrount implement Discount public double calculate(double price) return price*0.5; system.out.println(“增加积分!”); Software Design Patterns - Strategy3931 July 2022Class Client public sta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年社交媒体数据挖掘城市居民情绪与公共空间优化
- 2026年大型商业综合体电气安全诊断报告
- 酒店建设项目施工方案
- 2026年学生顶岗实习校企共管案例分析
- 2026年资本市场利息率与投资决策案例
- 2026年消防设施年度检测报告编制规范与示例
- 2026年幼儿情绪失控自伤行为教师干预案例
- 2026年医院洁净室空调节能运行方案
- 2026年生物信息学实验数据分析平台建设
- 2026年连锁酒店的区域市场拓展策略
- 电价及电费获奖课件
- 地质钻探施工方案
- 2024年河北省中考数学试题(含答案解析)
- 急性皮肤衰竭与压力性损伤鉴别
- 《氓》课件 统编版高中语文选择性必修下册
- 化工生产开停车方案
- 学生食堂消防演练方案及流程
- 《工业机器人技术基础》第3章 工业机器人运动学与动力学课件
- 教师职业发展与职称评定
- 可用性控制程序
- 9.3 LLDPE物质安全资料表-2
评论
0/150
提交评论