版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
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練習:一個電子商務系統(tǒng),其中有一個控制器對象(TaskControlle
26、r),用于處理銷售請求,能夠確認何時有人在請求銷售訂單,并將請求轉給SalesOrder對象處理。SalesOrder對象的功能包括:允許客戶通過GUI填寫訂單,處理稅額的計算,處理訂單和打印銷售收據(jù)。新需求:要處理多種稅額計算的方法,美國、加拿大、中國三個國家的稅收方法Software Design Patterns - Strategy3331 July 2022Software Design Patterns - Strategy3431 July 2022Software Design Patterns - Strategy3531 July 2022練習:某電影院售票系統(tǒng)為不同類型的
27、用戶提供不同打折方式(Discount),學生憑學生證享受8折優(yōu)惠(StudentDiscount),兒童享受減免10元優(yōu)惠(ChildrenDiscount),VIP用戶除享受半價優(yōu)惠還可積分(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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國產(chǎn)業(yè)園區(qū)物業(yè)管理行業(yè)全國市場開拓戰(zhàn)略制定與實施研究報告
- 2025-2030年中國宴會用餐行業(yè)開拓第二增長曲線戰(zhàn)略制定與實施研究報告
- 2025-2030年中國玩具行業(yè)商業(yè)模式創(chuàng)新戰(zhàn)略制定與實施研究報告
- 自動噴水滅火系統(tǒng)設計規(guī)范
- 服裝個性訂制消費愿望調查
- 2025-2030年中國電力物聯(lián)網(wǎng)行業(yè)市場全景評估及發(fā)展趨向研判報告
- 2025年中國野牡丹行業(yè)市場深度分析及未來發(fā)展趨勢預測報告
- 江蘇省南京市玄武區(qū)2023-2024學年九年級上學期期末化學試題
- 產(chǎn)品檢驗知識培訓課件
- 寧夏銀川一中、昆明一中2023屆高三聯(lián)合二??荚嚁?shù)學(理)試題 附答案
- 0的認識和加、減法(說課稿)-2024-2025學年一年級上冊數(shù)學人教版(2024)001
- 2025年廣西旅發(fā)南國體育投資集團限公司招聘高頻重點提升(共500題)附帶答案詳解
- 2024-2025學年銅官山區(qū)數(shù)學三年級第一學期期末調研試題含解析
- 江西省2023-2024學年高二上學期期末教學檢測數(shù)學試題 附答案
- 碳匯計量與監(jiān)測技術智慧樹知到期末考試答案章節(jié)答案2024年浙江農(nóng)林大學
- 可用性控制程序
- GB/T 17554.1-2006識別卡測試方法第1部分:一般特性測試
- 說明書hid500系列變頻調速器使用說明書s1.1(1)
- 橫版榮譽證書模板可修改打印 (9)
- 建設銀行股份關聯(lián)交易申報及信息披露系統(tǒng)操作手冊新一代
- 建筑工程施工勞務清包工合同
評論
0/150
提交評論