1 / 14

Designing a Persistence Framework With Patterns

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

toyah
Download Presentation

Designing a Persistence Framework With Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designing a Persistence FrameworkWith Patterns Presented By Dr. Shazzad Hosain

  2. 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.

  3. 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

  4. 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.

  5. Applying State Pattern

  6. 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 } // ….. }

  7. 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

  8. 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.

  9. 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.

  10. General strategy insert, update, delete

  11. 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 () ; }

  12. 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

  13. References • Chapter 34 of “Applying UML and Patterns – An Introduction to Object-Oriented Analysis and Design and the Unified Process” – by Craig Larman

More Related