【正文】
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 ? Transaction state ? Lazy materialization 2020/11/4 59 Shanghai Jiaotong University Problem: How to map classes to data in a RDB ? Solution (Representing objects as tables): define a table for each persistent class ? Each attribute bees a column ? New problem: How to deal with plex attributes (see “Representing object relationships as Tables”) 2020/11/4 60 Shanghai Jiaotong University 對象表示為表模式 2020/11/4 61 Ma n u f a c t u r e rn a m ec i t y. . .. . .n a m e c i t yN o w amp。 Z e nc i t y = M u mb a iC 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 How to consistently define the mapping between objects and db records ? Solution: Define an object identifier (OID) for each object, and put the OID in corresponding row of table 2020/11/4 62 Shanghai Jiaotong University 對象標(biāo)識符模式 2020/11/4 63 O I Dx y z 1 2 3a b c 3 4 5T h i s i s a s i m p l i f i e d d e s i g n .I n r e a l i t y , t h e O I D m a y b ep l a c e d i n a P r o x y c l a s s .p r i m a r y k e yM a n u f a c t u r e rc i t yn a m eo i d : O I D. . .. . .n a m e c i t yN o w amp。 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。 IMapper get(OID) : Object put( OID, Object ) ... Class UML notation : This is a qualified association. It means: 1. There is a 1M association from PersistenceFacade to IMapper objects. 2. With a key of type Class, an IMapper is found (., via a HashMap lookup) Shanghai Jiaotong University 模板方法模式 ? Problem: How to implement a variations of an algorithm that bine generic and specialized functionality? ? Solution: Provide a template method in base class that implements skeleton of algorithm, override varying parts in subclasses (using “hook” methods) 2020/11/4 66 Shanghai Jiaotong University 2020/11/4 67 A b s t r a c t C l a s st e mp l a t e M e t h o d ( )p r i m i t i v e O p e r a t i o n ( )c o n c r e t e O p e r a t i o n ( )C o n c r e t e C l a s sp r i m i t i v e O p e r a t i o n ( )t e mp l a t e M e t h o d{ ... p r i m i t i v e O p e r a t i o n ( ) ... c o n c r e t e O p e r a t i o n ( ) ...}T e m p l a t e M e t h o d ( G o F p a t t e r n )d e f i n e s s k e l e t o n o f a n a l g o r i t h m ,w i t h v a r y i n g a n d u n v a r y i n g p a r t sa b s t r a c t p r i m i t i v e o p e r a t i o n s v a r y i n g p a r t o v e r r i d e n i n s u b c l a s sc o n c r e t e o p e r a t i o n s d e f a u l t b e h a v i o r i f c a n b e o v e r r i d e n i n s u b c l a s s , t h e n i s c a l l e d a h o o k m e t h o dShanghai Jiaotong University Using template method pattern for materialization 2020/11/4 68 Abstract RDBMapper tableName : String + 171。 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 )。 } } IMapper ProductSpecification RDBMapper + 171。 ProductSpecificationRDBMapper(tabName) getObjectFromRecord(OID, DBRecord) : Object { // hook method override protected Object getObjectFromRecord( OID oid, DBRecord dbRec ) { ProductSpecification ps = new ProductSpecification()。 ( (PRICE) )。 ( (DESC) )。 } } { private DBRecord getDBRecord OID oid ) { String key = ()。 } } 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