14
Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain

Designing a Persistence Framework With Patterns

  • Upload
    toyah

  • View
    51

  • Download
    0

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

Page 1: Designing a Persistence Framework With Patterns

Designing a Persistence Framework

With Patterns

Presented ByDr. Shazzad Hosain

Page 2: Designing a Persistence Framework With Patterns

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.

Page 3: Designing a Persistence Framework With Patterns

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

Page 4: Designing a Persistence Framework With Patterns

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.

Page 5: Designing a Persistence Framework With Patterns
Page 6: Designing a Persistence Framework With Patterns

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

Page 7: Designing a Persistence Framework With Patterns

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

Page 8: Designing a Persistence Framework With Patterns

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.

Page 9: Designing a Persistence Framework With Patterns

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.

Page 10: Designing a Persistence Framework With Patterns

General strategy insert, update, delete

Page 11: Designing a Persistence Framework With Patterns

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

Page 12: Designing a Persistence Framework With Patterns

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

Page 13: Designing a Persistence Framework With Patterns
Page 14: Designing a Persistence Framework With Patterns

ReferencesChapter 34 of “Applying UML and Patterns – An

Introduction to Object-Oriented Analysis and Design and the Unified Process” – by Craig Larman