1.32k likes | 1.39k Vues
DESIGNING USE-CASE REALIZATIONS WITH GoF DESIGN PATTERNS. 徐迎晓 xuyingxiao@126.com 复旦大学软件学院. OUTLINE. Adapter Factory Singleton Strategy Composition Facade Observer. Adapter. 问题:如何解决接口不兼容问题,几个类似的构件接口不同,如何为其提供稳定的接口 ? 解决:通过中间的适配器对象,将构件原有接口转换成另一个接口.
E N D
DESIGNING USE-CASEREALIZATIONS WITH GoFDESIGN PATTERNS 徐迎晓 xuyingxiao@126.com 复旦大学软件学院
OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer
Adapter • 问题:如何解决接口不兼容问题,几个类似的构件接口不同,如何为其提供稳定的接口? • 解决:通过中间的适配器对象,将构件原有接口转换成另一个接口
support external third-party services, credit authorization services,
support external third-party services, • inventory systems,
support external third-party services, • accounting systems,
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.
" 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
OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer
Factory • who creates the adapters? • a domain object (such as a Register)? • 如何确定创建哪种adapter? such as TaxMaster-Adapter or GoodAsGoldTaxProAdapter?
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.
domain layer软件对象相对强调纯应用逻辑的职责 • 另一组软件对象负责与外部系统的连接
Context I Problem • 当有特殊考虑时如何创建对象? (如创建对象的逻辑很复杂,或想将创建对象的职责分离出来以获得更高的内聚度等) • Solution • Create a Pure Fabrication object called a Factory that handles the creation.
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();
Factory objects have several advantages: • the responsibility of complex creation分离到高内聚的helper objects中 • 将潜在的复杂的创建对象的逻辑隐藏 • 可进而引入增强性能的内存管理策略,如object caching or recycling.
Also is an example of a partial data-driven design • 通过改变属性值来选择创建不同的adapter对象 • Protected Variations :Adapter的实现类在变化
OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer
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?
Con text I Problem • 只允许创建一个实例—it is a “singleton.”需要为对象提供全局的、单点访问 • Solution • Define a static method of the class that returns the singleton.
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; } } 保存全类唯一的实例 对客户隐藏构造器 客户只能从此处获得实例
eager initializaition public class Singleton { private static Singleton _instance = new Singleton(); public static Singleton getInstance() { return _instance; } }
Conclusion • 综合使用Adapter, Factory, and Singleton patterns,针对外部系统 (tax calculators, accounting systems等)的各种接口变化提供了PV
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
OUTLINE • Adapter • Factory • Singleton • Strategy • Composition • Facade • Observer
Strategy (GoF) • 如何提供复杂的pricing logic(圣诞促销、会员价,……) • The pricing strategy(a rule, policy, or algorithm) can vary during different period
Context / Problem • 对于变化着的、但又相关的算法和策略如何设计?如何设计才能改变这些算法和策略? • Solution • 将每个algorithm/policy/strategy定义在具有共同接口的不同的类中.