【正文】
ss 171。 Z e no i d = x y z 1 2 3C e l e s t i a lS h o r t e n i n gS a n R a m o nShanghai Jiaotong University Problem: Who should be responsible for materialization and dematerialization ? Info expert suggests the object should do this itself (. direct mapping). But hard to program and to maintain. ? Solution: make a separate “database mapper” class responsible for materialization, dematerialization, object caching ? One for each persistent class 2020/11/4 64 Shanghai Jiaotong University 數(shù)據(jù)庫映射器或代理模式 2020/11/4 65 each mapper gets and puts objects in its own unique way, depending on the kind of data store and format 1 PersistenceFacade getInstance() : PersistenceFacade get( OID, Class ) : Object put( OID, Object ) ... ProductSpecification RDBMapper ... get( OID) : Object put( OID, Object ) ... ProductSpecification FlatFileMapper ... get( OID) : Object put( OID, Object ) ... Manufacturer RDBMapper ... get( OID) : Object put( OID, Object ) ... note that the Class as a parameter is no longer needed in this version of get , as the class is hardwired for a particular persistent type 1 171。 ProductSpecificationRDBMapper(tabName) getObjectFromRecord(OID, DBRecord) : Object { // hook method override protected Object getObjectFromRecord( OID oid, DBRecord dbRec ) { ProductSpecification ps = new ProductSpecification()。 } } Shanghai Jiaotong University 緩存管理模式 ? Problem: How to efficiently materialize frequently accessed objects? ? database mappers maintain their own cache ? One cache per mapper class 2020/11/4 69 Shanghai Jiaotong University 事務(wù)狀態(tài)和狀態(tài)模式 2020/11/4 70 O l d C l e a n O l d D i r t yO l d D e l e t ec o m m i t / d e l e t ed e l e t eN e w[ f r o m D B ][ n e w ( n o t f r o m D B ) ]s a v ec o m m i t / u p d a t ed e l e t er o l l b a c k / r e l o a dr o l l b a c k / r e l o a dc o m m i t / i n s e r tS t a t e c h a r t : P e r s i s t e n t O b j e c tL e g e n d :N e w n e w l y c r e a t e d 。 ( (DESC) )。 AbstractRDBMapper( tableName ) getObjectFromStorage(OID) : Object {leaf} getObjectFromRecord(OID, DBRecord) : Object {abstract} getDBRecord(OID) : DBRecord Abstract PersistenceMapper + get( OID) : Object {leaf} getObjectFromStorage(OID) : Object {abstract} ... { protected final Object getObjectFromStorage( OID oid ) { dbRec = getDBRecord( oid )。 ? 對輸出機(jī)制的變化實(shí)現(xiàn)了防止變異 ? 一致的錯(cuò)誤匯報(bào)風(fēng)格 ? 集中控制公共的錯(cuò)誤通知策略 ? 性能也有所改進(jìn) 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) ?問題 : 不希望或不可能直接訪問真正的主題對象時(shí),應(yīng)該如何 ?解決方案 : 通過代理對象增加一層間接性,代理對象實(shí)現(xiàn)與主題對象相同的接口,并且負(fù)責(zé)控制和增強(qiáng)對主題對象的訪問 2020/11/4 51 Shanghai Jiaotong University Proxy 結(jié)構(gòu) 2020/11/4 52 interface ISubjectInterface foo() RealSubject foo() { ... preprocessing () ... postprocessing } Client subject : ISubjectInterface doBar() 1 1 1 1 Proxy realSubject : ISubjectInterface foo() { ... whatever () ... whatever } subject actually references an instance of Proxy, not RealSubject realSubject will actually reference an instance of RealSubject Shanghai Jiaotong University (6)抽象工廠 (GoF) ?可能需要與多種不同的物理設(shè)備打交道 ,… ?這些設(shè)備可能來自不同的廠商,如 IBM, Epson, 它們的驅(qū)動十分類似 (Java Classes) ?問題 : 如何創(chuàng)建實(shí)現(xiàn)相同接口的相關(guān)類的族 ? ?解決方案 : 定義一個(gè)公共接口 (abstract factory), 具體的工廠繼承它 2020/11/4 53 Shanghai Jiaotong University Abstract factory 2020/11/4 54 interface IJavaPOSDevicesFactory getNewCashDrawer() : getNewCoinDispenser() : ... IBMJavaPOSDevicesFactory ... getNewCashDrawer() : getNewCoinDispenser() : ... { return new () } interface isDrawerOpened() ... NCRJavaPOSDevicesFactory ... getNewCashDrawer() : getNewCoinDispenser() : ... { return new () } this is the Abstract Factoryan interface for creating a family of related objects isDrawerOpened() ... isDrawerOpened() ... Shanghai Jiaotong University (7)Do it myself ?我是對實(shí)際對象的抽象,由我來完成這些通常由實(shí)際對象完成的事情 ?與信息專家模式相比較 (Text Object does spellchecking), 多態(tài) (Circle Object draws itself) 2020/11/4 55 Shanghai Jiaotong University Do it myself (polymorphism) 2020/11/4 56 Payment amount authorize() CashPayment authorize() CreditPayment authorize() CheckPayment authorize() DebitPayment authorize() By Polymorphism, each payment type should authorize itself. This is also in the spirit of Do it Myself (Coad) Shanghai Jiaotong University ?采用模式來設(shè)計(jì)持久化框架 ?某些與數(shù)據(jù)處理相關(guān)的模式 : ?將對象表示為表 ?Representing object relationships as Tables ?對象標(biāo)識 ?數(shù)據(jù)庫映射器 Database Mapper/Broker ?緩存管理 Cache management ?新的 GoF 模式 : Template, State, Command, Virtual Proxy 2020/11/4 57 Shanghai Jiaotong University (1)持久化框架 ? What is a persistence service? ? What is a framework? ? Basic requirements of a persistence service: ? Store and retrieve objects ? Commit and rollback 2020/11/4 58 Shanghai Jiaotong University (2)Key ideas ? Schema ? Mapping (between class and persistent store) ? Object identity ? Materialization and dematerialization ? Caching