国立政治university电子计算机中心.pptVIP

  • 2
  • 0
  • 约4.35千字
  • 约 45页
  • 2017-05-06 发布于四川
  • 举报
国立政治university电子计算机中心

2001/11/1 Understanding Design Patterns - for DIDC 廖峻鋒 Apr 28,2004 NCCU Computer Center Agenda Software Patterns觀念複習 LSP、OCP and DI Template Method、Factory、Singleton DesignPatterns的分類 Recommended Readings Q A Patterns 與棋譜 Why Patterns ? 增廣見聞、增加經驗。 開發人員共同的術語。 簡化軟體設計文件中的描述。 The Pattern System 由[POSA96]首先提出對Patterns的分類 將Pattern從「鉅觀」到「微觀」加以分類 Architectural Patterns (例如:MVC、Layer) Design Patterns (例如:Gof的23個patterns) Idioms (例如大量接合字串問題或Code Style) 一些重要的OO技術 LSP:Liskov代換法則 OCP:Open – Close Principle Dependency Injection:元件注入技術 LSP:Liskov代換法則 所有的子類別皆可代父出征 實作界面就可保証符合規格 Interface Interface規定了一組契約(method),所有實作它的類別都要實作所有方法。 Client呼叫的是Interface中的方法,所以元件的抽換對Client來說是感覺不到的(不用改code)。 OCP Open for extension;close for modification Refer to “The Open – Close Princlple Tutorial”. 元件注入 Dependency Injection Dependency Injection provide help us to implement the IoC Interface Injection (type 1 IoC) Setter Injection (type 2 IoC) Constructor Injection (type 3 IoC) Before Adapting DI After Adapting DI LoginModule Before Adapting DI After Adapting DI DI Frameworks Apache Avalon Framework (type 1 IoC) Spring Framework (type 2 IoC) Pico Container (type 3 IoC) Template Method /Hook Method Framework的作者通常將主要邏輯寫在Template Method 中。 Template Method會呼叫若干Hook Method,Hook Method通常就是「變異點(Hot Spot)」。 覆寫Hook Method,就可改變Template Method的行為。( 例如Servlet的doGet()/doPost() ) Servlet Factory 一般create一個類別的instance時,我們會使用new 如果在runtime才能決定要new那個類別怎麼辦? 範例:時常需要切換多台資料庫 範例:時常需要切換多台資料庫 Factory的型式 simple static factory Factory method (Gof) Abstract factory (Gof) Simple Static Factory CarFactory的實作 public class CarFactory { public static createCar(String carType){ if (carType==…) else if(…) else … } } Refactoring by Factory Method Pattern Using Map as Factory Container CarFactory carFactory = getCarFactory(“BMW”); BmwCar bmwCar = (BmwCar) carFactory.create(); getCarFactory without mutiple if…else CarFactory getCarFactory(String carType) { return carFactoryContainer.get(carType); } Si

文档评论(0)

1亿VIP精品文档

相关文档