Sample Application

[Previous] [Next]

In order to demonstrate the applicability of the Object By Value design pattern, I have created a sample offline banking system called the BSTAM Banker. It effectively allows bank customers to perform various actions against their accounts off line without having to sacrifice functionality for performance. The user will receive an almost instant reply to all banking services for any type of request (e.g., deposit funds, withdraw funds). This application only permits downloading an account for offline use; in a complete system you could expect the capability to update your bank account by uploading changes back to the bank's computer system. The performance costs of the entire transaction would be significantly less than if the customer were making the transaction on line. With offline banking, all account changes are submitted in a single transmission, while with online banking, every service request results in a remote transmission in both directions—one for the request and the other for the reply from the object providing the service.

You might ask, "How is offline banking implemented in the BSTAM Banker?" Answer: using the Object By Value design pattern. BSTAM Banker contains four major components: BankSvr.EXE, BankClient.EXE, Account-Lib.DLL, and ObjTransformLib.DLL.

  • BankSvr is an ActiveX EXE that you should think of as the bank's central computer system. It contains the original SavingsAccount objects that will be passed by value to BankClient.
  • BankClient is the COM client (Standard EXE) software given to all bank customers to allow them to do offline banking from their home computer by means of a Windows graphical user interface (GUI). Under the hood, BankClient obtains a local copy of a Savings-Account object passed by value from BankSvr.
  • AccountLib is an ActiveX DLL class library that defines the SavingsAccount class. The SavingsAccount class is persistable, thus making it possible to pass it by value.
  • ObjTransformLib is an ActiveX DLL class library that defines the Transformer class used to facilitate a persistable object transformation to and from a byte array data stream.

When the bank customer clicks the Retrieve Account button in the BSTAM Banker GUI to download a savings account, the following sequence of events, illustrated in Figure 6-4, occurs to allow BankClient to obtain a SavingsAccount object by value from BankSvr.

  • BankClient binds to a Teller object that resides in BankSvr. Since the object is outside BankClient's address space, COM intercedes by creating a Teller proxy object that resides in BankClient's address space and a Teller stub object in BankSvr's address space. Requests for service from the Teller object are transmitted across process boundaries by these components. This layer is inaccessible to and can be taken for granted by the Visual Basic programmer.
  • BankClient makes a request to the Teller object to retrieve a Savings-Account object that is returned as a byte array:
  •  Private Sub cmdRetrieveAcct_Click() Dim byteArr() As Byte Dim bRetCode As Boolean Dim strPrompt As String strPrompt = "Enter one of the following accounts: " _ & vbNewLine _ & "orion0425, spencer0220, or matthew0316" m_strAcctNo = InputBox(strPrompt, _ "BSTAM Banker - Retrieve Account")  ' Obtain a copy of a SavingsAccount object in the form ' of a byte array from the cross-process BankSvr COM ' server. bRetCode = m_Teller.RetrieveAccount(m_strAcctNo, _ byteArr)  

  • The RetrieveAccount method of the BankSvr's Teller object retrieves the requested SavingsAccount object from its in-memory database (which is really a Dictionary object). It then converts the SavingsAccount object to a byte array data stream by invoking the ObjectTransformer.ObjectToStream method and returns it to the BankClient caller.
  •  Public Function RetrieveAccount(AcctNo As String, _ ByteArr() As Byte) As Boolean ' Locate and return SavingsAccount object by value to ' caller. Dim sa As AccountLib.SavingsAccount ' Locate a SavingsAccount object in the SavingsAccount ' Dictionary object (m_SavingsAccounts), and transform ' it to a byte array that is returned to the caller. If m_SavingsAccounts.Exists(AcctNo) Then Set sa = m_SavingsAccounts(AcctNo) ByteArr = m_Transformer.ObjectToStream(AcctNo, sa) RetrieveAccount = True Else  

  • BankClient converts the byte array data stream into a local copy of a SavingsAccount object by invoking the StreamToObject method of the ObjectTransformer object.
  •  Private Sub cmdRetrieveAcct_Click()  ' Transform byte array into a local copy of the ' SavingsAccount from the out-process. Set m_SavingsAcct = m_Transformer.StreamToObject( _ m_strAcctNo, byteArr)  

click to view at full size.

Figure 6-4. Object diagram of the BSTAM Banker sample application that illustrates passing a SavingsAccount object by value from BankSvr to BankClient.

The key participants of this sample application that make up the Object By Value design pattern include the following:

  • SavingAccount (ConcretePersistableObject). This class must be and is persistable because its Persistable property value is set as such and because it provides behavior to read and write its state to and from a byte array data stream by implementing ClassReadProperties and ClassWriteProperties event handlers as shown in the following code extract.
  •  Private Sub Class_ReadProperties(PropBag As PropertyBag) ' Read in property values from a byte array via the ' PropertyBag object. With PropBag m_strAccountNo = .ReadProperty("AccountNumber") m_strAccountOwner = .ReadProperty("AccountOwner") m_dblInterestRate = .ReadProperty("InterestRate") m_dblBalance = .ReadProperty("AccountBalance") End With End Sub Private Sub Class_WriteProperties(PropBag As PropertyBag) ' Write out property values to a byte array via the ' PropertyBag object. With PropBag .WriteProperty "AccountNumber", m_strAccountNo .WriteProperty "AccountOwner", m_strAccountOwner .WriteProperty "InterestRate", m_dblInterestRate .WriteProperty "AccountBalance", m_dblBalance End With End Sub 

  • Transformer (ObjectTransformer). This class defines the behavior required to facilitate transformation of a persistable SavingsAccount object to and from a byte array data stream. The source for this class is identical to what was illustrated in the Implementation section of this chapter.

Refer to the companion CD for the complete source code.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net