第11讲架构与其他模式内容摘要:

tion model is required. Not a Swing or GUI class. Just a plain object which the GUI objects Shanghai Jiaotong University 3. 更多模式 Shanghai Jiaotong University 使用模式解决某些实际问题 :  Reliability and recovery Local caching Handling failure  Handling payments  7个新模式 2020/11/4 38 Shanghai Jiaotong University 采用适配器和多态模式实现缓存 2020/11/4 39 interface IProductsAdapter getSpecification( itemID ) : ProductSpecification DBProductsAdapter getSpecification( itemID ) LocalProducts remoteProductsService: IProductAdapter getSpecification( itemID ) BigWebServiceProductsAdapter getSpecification( itemID ) 1 1 Implements the adapter interface, but is not really an adapter for a second ponent. Rather, it itself implements the local service function. ProductCatalog productsService : IProductAdapter getSpecification() 1 1 Shanghai Jiaotong University Singleton 和 Factory 提供了实现接口的类 2020/11/4 40 :Store :Register pc: ProductCatalog create() 2: create(pc) 1: create() singleton :ServicesFactory psa : LocalProducts externalService : DBProductsAdapter : psa := getProductsAdapter() IProductsAdapter : create( externalService ) : create() 本地服务获取了对外部服务的适配器的引用 IProductsAdapter 返回本地服务 Shanghai Jiaotong University 从缓存或者数据库中获取产品信息 2020/11/4 41 : LocalProducts remoteService : DBProductsAdapter IProductsAdapter : KeyIndexedFileOf SerializedObjects 1: ps := get( id ) 3 [not in file ]: put( id, ps ) ps := getSpecification(id) 2 [not in file] : ps := getSpecification(id) continued in another diagram IProductsAdapter Shanghai Jiaotong University 处理故障 本地缓存中未命中且访问外部产品服务失败时,如何处理 ?  用信号通知收银员人工输入价格和描述 ?  取消输入该商品项 ? 解决方案 : 抛出异常  低层的异常转换成高层次中有意义的异常  依据问题而不是抛出者命名异常 2020/11/4 42 Shanghai Jiaotong University 处理错误 (seq diagram) 2020/11/4 43 exception DBUnavailableException() : DBProducts Adapter IProductsAdapter : Persistence Facade UML notation : * All asynchronous messages, including exceptions, are illustrated with a stick arrowhead. * Exceptions are shown as messages indicated by the exception class name. * An optional exception or signal stereotype is legal (an exception is a kind of signal in the UML), if increased visibility is desired. ps := get(...) ps := getSpecification(id) : Object resultSet := executeQuery(...) exception SQLException() note the difference between synchronous and asynchronous message arrowheads in the UML recall that indicating the instance of type Object is useful when one wants to indicate the interface, but not the class of an instance exception ProductInfoUnavailableException() stopping the message line at this point indicates the PersistenceFacade object is catching the exception Shanghai Jiaotong University Exceptions in a UML class diagram 2020/11/4 44 exceptions caught are modeled as a kind of operation handling a signal PersistenceFacade usageStatistics : Map Object get(Key, Class) throws DBUnavailableException, FatalException put(Key, Object) { throws= (DBUnavailableException, FatalException) } ... 玸 ignal?SQLException() 玸 ignal?IOException() exceptions FatalException DBUnavailableException exceptions thrown can be listed in another partment labeled exceptions UML notation : The UML has a default syntax for operations. But it does not include an official solution to show exceptions thrown by an operation. There are at least three solutions: 1. The UML allows the operation syntax to be any other language, such as Java. In addition, some UML CASE tools allow display of operations explicitly in Java , Object get(Key, Class) throws DBUnavailableException, FatalException 2. The default syntax allows the last element to be a property string. This is a list of arbitrary property+value pairs, such as { author=Craig, kids=(Hannah, Haley), ...}. Thus, put(Object, id) { throws= (DBUnavailableException, FatalException) } 3. Some UML CASE tools allow one to specify (in a special dialog box) the exceptions that an operation throws. Shanghai Jiaotong University 更多模式 转换异常 对问题而不是抛出者命名 集中错误日志 错误会话 代理 抽象工厂 自力更生 2020/11/4 45 Shanghai Jiaotong University (1)转换异常 将较低层的异常转换为本层次系统中有意义的异常  通过转换,高层次的异常提供了对低层次异常的子系统层次的解释 2020/11/4 46 Shanghai Jiaotong University (2)命名问题,而不是抛出者 异常必须依据其抛出的理由进行命名 抛出者并不能表示问题所在 2020/11/4 47 Shanghai Jiaotong University (3)集中错误日志 使用单实例类访问的集中错误日志对象,所有的异常都向它汇报  提供了一致性  对输出流和格式的柔性定义 2020/11/4 48 Shanghai Jiaotong University (4)错误会话 使用标准的单实例类访问的、应用程序无关的、非用户界面的对象向用户通知错误。  对输出机制的变化实现了防止变异  一致的错误汇报风格  集中控制公共的错误通知策略  性能也有所改进 2020/11/4 49 Shanghai Jiaotong University 处理异常 2020/11/4 50 : DBProducts Adapter : Local Products IProductsAdapter :ProductCatalog ps := getSpec(id) :Register enterItem(id, qty) :ProcessSale Frame exception ProductInfoUnavailableException() singleton :ErrorDialog notify(message, exception) ps := getSpec(id) ps := getSpec(id) continued singleton :ErrorDialog notify (message, exception) * : notify(message) : Object : Object INotifier GUI dialog box text console dialog speech dialog singleton :Log log(exception) IProductsAdapter Convert Exception pattern Error Dialog pattern Centralized Error Logging pattern Shanghai Jiaotong University (5)代理 (GoF) 问题 : 不希望或不可能直接访问真正的主题对象时,应该如何 解决方案 : 通过代理对象增加一层间接性,代理对象实现与主题对象相同的接口,并且负责控制和增强对主题对象的访问 2020/11/4 51 Shanghai Jiaotong University Proxy 结构 2020/11/4 52 interface ISubjectInterface foo() RealSubject foo() { ... preprocessing () ... postprocessing } Client subject : ISubjectInterface doBar() 1 1 1 1 Proxy realSubject : ISubjectInterface foo() { ... whatev。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。