1 / 129

DESIGNING USE-CASE REALIZATIONS WITH GoF DESIGN PATTERNS

DESIGNING USE-CASE REALIZATIONS WITH GoF DESIGN PATTERNS. 徐迎晓 xuyingxiao@126.com 复旦大学软件学院. OUTLINE. Adapter Factory Singleton Strategy Composition Facade Observer. Adapter. 问题:如何解决接口不兼容问题,几个类似的构件接口不同,如何为其提供稳定的接口 ? 解决:通过中间的适配器对象,将构件原有接口转换成另一个接口.

Télécharger la présentation

DESIGNING USE-CASE REALIZATIONS WITH GoF DESIGN PATTERNS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DESIGNING USE-CASEREALIZATIONS WITH GoFDESIGN PATTERNS 徐迎晓 xuyingxiao@126.com 复旦大学软件学院

  2. OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer

  3. Adapter • 问题:如何解决接口不兼容问题,几个类似的构件接口不同,如何为其提供稳定的接口? • 解决:通过中间的适配器对象,将构件原有接口转换成另一个接口

  4. support external third-party services, tax calculators

  5. support external third-party services, credit authorization services,

  6. support external third-party services, • inventory systems,

  7. support external third-party services, • accounting systems,

  8. Adapter pattern是 GRASP building blocks的特例 • It offers Protected Variations from changing external interfaces or third-party packages through the use of an Indirection object that applies interfaces and Polymorphism.

  9. " Analysis" Discoveries During Design: Domain Model • a list of tax line items are associated with a sale, such as state tax, federal tax, and so forth

  10. OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer

  11. Factory • who creates the adapters? • a domain object (such as a Register)? • 如何确定创建哪种adapter? such as TaxMaster-Adapter or GoodAsGoldTaxProAdapter?

  12. another fundamental design principle • Design to maintain a separation of concerns. • 一般作为architectural design principle • one of the essential principles in software engineering • modularize or separate distinct concerns into different areas, so that each has a cohesive purpose.

  13. domain layer软件对象相对强调纯应用逻辑的职责 • 另一组软件对象负责与外部系统的连接

  14. Context I Problem • 当有特殊考虑时如何创建对象? (如创建对象的逻辑很复杂,或想将创建对象的职责分离出来以获得更高的内聚度等) • Solution • Create a Pure Fabrication object called a Factory that handles the creation.

  15. public class Factory{ public static Sample creator(){ .... if (which==1) return new MySample(); else if (which==2) return new HisSample(); } } Sample s; s=Factory.creator(); s.xxx();

  16. Factory objects have several advantages: • the responsibility of complex creation分离到高内聚的helper objects中 • 将潜在的复杂的创建对象的逻辑隐藏 • 可进而引入增强性能的内存管理策略,如object caching or recycling.

  17. Also is an example of a partial data-driven design • 通过改变属性值来选择创建不同的adapter对象 • Protected Variations :Adapter的实现类在变化

  18. OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer

  19. Singleton (GoF) • who creates the factory itself, and how is it accessed? • 整个过程只需要一个factory实例 • 代码中各个地方都可能需要调用factory 中的方法来创建实例--〉Thus, there is a visibility problem: how to get visibility to this single ServicesFactory instance?

  20. Con text I Problem • 只允许创建一个实例—it is a “singleton.”需要为对象提供全局的、单点访问 • Solution • Define a static method of the class that returns the singleton.

  21. lazy initializaition // Singleton.java public class Singleton { private static Singleton _instance = null; private Singleton(){} public static Singleton getInstance() { if (null==_instance) _instance = new Singleton(); return _instance; } } 保存全类唯一的实例 对客户隐藏构造器 客户只能从此处获得实例

  22. eager initializaition public class Singleton { private static Singleton _instance = new Singleton(); public static Singleton getInstance() { return _instance; } }

  23. 使用

  24. UML表示

  25. Fig. 26.7

  26. Conclusion • 综合使用Adapter, Factory, and Singleton patterns,针对外部系统 (tax calculators, accounting systems等)的各种接口变化提供了PV

  27. 1 : Store : ServicesFactory create create : Register accountingAdapter = getAccountingAdapter create : SAPAccounting Adapter accountingAdapter : : Register SAPAccountingAdapter makePayment create ( cashTendered ) SOAP over : Payment HTTP postSale ( sale ) «actor» xxx : SAPSystem Fig. 26.8

  28. OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer

  29. Strategy (GoF) • 如何提供复杂的pricing logic(圣诞促销、会员价,……) • The pricing strategy(a rule, policy, or algorithm) can vary during different period

  30. Context / Problem • 对于变化着的、但又相关的算法和策略如何设计?如何设计才能改变这些算法和策略? • Solution • 将每个algorithm/policy/strategy定义在具有共同接口的不同的类中.

  31. Fig. 26.10

  32. Creating a Strategy with a Factory

More Related