Upload
toyah
View
51
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Designing a Persistence Framework With Patterns. Presented By Dr. Shazzad Hosain. Transactional States and the State Pattern. Persistent objects can be inserted, deleted or modified - PowerPoint PPT Presentation
Citation preview
Designing a Persistence Framework
With Patterns
Presented ByDr. Shazzad Hosain
Transactional States and the State Pattern
Persistent objects can be inserted, deleted or modifiedOperating on a persistent object does not cause an
immediate database update; rather, an explicit commit operation must be performed.
Persistent Objects
public void commit () { switch ( state ) { case OLD_DIRTY: // ….. break ; case OLD_CLEAN: // ….. break ; }}
public void rollback() { switch ( state ) { case OLD_DIRTY: // ….. break ; case OLD_CLEAN: // ….. break ; }}
An alternative to this repeating case logic structure is the GoF State pattern
State PatternContext / Problem: An object’s behavior is
dependent on its state, and its methods contain case logic reflecting conditional state-dependent actions. Is there an alternative to conditional logic ?
Solution: Create state classes for each state, implementing a common interface. Delegate state-dependent operations from the context object to its current state object. Ensure the context object always points to a state object reflecting its current state.
Transactional States
public class PObjectState { public void commit (PersistenceObject obj) { // no operation } public void delete (PersistenceObject obj) { // no operation } public void rollback (PersistenceObject obj) { // no operation } // …..}
public class NewState extends PObjectState { public void commit (PersistenceObject obj) { PersistenceFacade.getInstance().insert (obj); obj.setState ( OldCleanState.getInstance() ) ; }}
Edit Example public class ProductSpecificationJFrame {
public void onClickEdit () { ProductSpecification ps = new ProductSpecification () ; get product specification data from database set table attributes e.g. name, price etc. to ps ps.setState ( new OldCleanState () ) ; display the ps values to text boxes or controls }
public void onClickSave () { ps.setName ( txtName.getText () ) ps.setPrice ( txtPrice.getText () ) ps.setState ( newOldDirtyState () ) ; display name, price for further verification }
public void onClickCommit () { ps.commit () ; } // …..}
Pull Data from DB
Display to GUI
Edit at GUI
Click Save Button and Display the
Edited Data
Finally Commit to DB
Designing a Transaction with the Command PatterA transaction include inserting, updating, and
deleting objects.One transaction could contain two insert, one
update and three deletes, for example.The order of database tasks within a transaction can
influence its success and performance.Example: A referential integrity constraint
Table A contains a foreign key of Table BSuppose a transaction contains an INSERT in Table B
and UPDATE in Table A.Lets say UPDATE executes before the INSERT, a
referential integrity error is raised.
The Command PatternContext / Problem: How to handle requests
or tasks that need functions such as sorting (prioritizing), queuing, delaying, logging or undoing?
Solution: Make each task a class that implements a common interface.
General strategy insert, update, delete
ExampleEnd Sale with Payment
Insert Payment TableInsert
SalesLineItem Table
Update Inventory Table
PayID CC No Total Date
10 523…234
5000 12/3/2011
SLI_ID
PayID ItemID
Qty
101 10 201 3
102 10 305 2
ItemID
qty
201 500
305 600
tbl_payment
tbl_sales_line_itemtbl_inventory
PersistenceFacade.getInstance.put (CCNo, sale)
put (ccNo, sale){ payID = getNextPaymentID () ; Transaction t = new Transaction () t.addInsert ( new Payment (ccNo, payID, sale.getTotal() ) for each sli in sale object sli.addPayID ( payID ) t.addInsert ( sli ) t.addUpdate ( new Inventory ( sli.getItemID () ) ) t.sort () ; t.commit () ;}
Lazy Materialization with a Virtual Proxy
Defer the materialization of an object until it is absolutely required, usually for performance reasons
Suppose ProductSpecification objects reference a Manufacturer object, but only very rarely does it need to be materialized from the database
Deferred materialization of “children” objects is known as lazy materialization.
ProductSpecification
title: StringPrice: floatManufacturer: Manufacturer
ReferencesChapter 34 of “Applying UML and Patterns – An
Introduction to Object-Oriented Analysis and Design and the Unified Process” – by Craig Larman