150 likes | 374 Views
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
E N D
Designing a Persistence FrameworkWith Patterns Presented By Dr. Shazzad Hosain
Transactional States and the State Pattern • Persistent objects can be inserted, deleted or modified • Operating 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 Pattern • Context / 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 NewState extends PObjectState { public void commit (PersistenceObject obj) { PersistenceFacade.getInstance().insert (obj); obj.setState ( OldCleanState.getInstance() ) ; } } public class PObjectState { public void commit (PersistenceObject obj) { // no operation } public void delete (PersistenceObject obj) { // no operation } public void rollback (PersistenceObject obj) { // no operation } // ….. }
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 Patter • A 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 B • Suppose 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 Pattern • Context / 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.
tbl_payment Example • End Sale with Payment tbl_sales_line_item tbl_inventory Insert Payment Table Insert SalesLineItem Table PersistenceFacade.getInstance.put (CCNo, sale) Update Inventory Table 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: String Price: float Manufacturer: Manufacturer
References • Chapter 34 of “Applying UML and Patterns – An Introduction to Object-Oriented Analysis and Design and the Unified Process” – by Craig Larman