Section J.9. Class Transaction


J.9. Class transaction

Class transaction (Fig. J.8) is an abstract base class that represents the notion of an ATM transaction. It contains the common features of derived classes BalanceInquiry, Withdrawal and Deposit. This class expands upon the "skeleton" code first developed in Section 11.8. Line 3 declares this class to be MustInherit (the Visual Basic equivalent of an abstract class). Lines 46 declare the class's Private instance variables. Recall from the class diagram of Fig. 11.20 that class transaction contains an property AccountNumber that indicates the account involved in the transaction. Line 4 implements the instance variable accountNumberValue to maintain the AccountNumber property's data. We derive attributes screen (implemented as instance variable screenHandle in line 7) and bankDatabase (implemented as instance variable bankDatabaseHandle in line 8) from class transaction's associations modeled in Fig. 11.19. All transactions require access to the ATM's screen and the bank's database.

Figure J.8. MustInherit base class transaction represents an ATM transaction.

  1  ' Transaction.vb  2  ' MustInherit base class Transaction represents an ATM transaction.  3  Public MustInherit Class Transaction  4     Private accountNumberValue As Integer ' indicates account involved  5     Private screenHandle As Screen ' ATM's screen  6     Private bankDatabaseHandle As BankDatabase ' account info database  7  8     ' constructor invoked by derived classes using MyBase.New  9     Public Sub New(ByVal userAccount As Integer, _ 10        ByVal userScreen As Screen, ByVal database As BankDatabase) 11        accountNumberValue = userAccount 12        screenHandle = userScreen 13        bankDatabaseHandle = database 14     End Sub ' New 15 16     ' property AccountNumber 17     Public ReadOnly Property AccountNumber() As Integer 18        Get 19           Return accountNumberValue 20        End Get 21     End Property ' AccountNumber 22 23     ' property ScreenReference 24     Public ReadOnly Property ScreenReference() As Screen 25        Get 26           Return screenHandle 27        End Get 28     End Property ' ScreenReference 29 30     ' property BankDatabaseReference 31     Public ReadOnly Property BankDatabaseReference() As BankDatabase 32        Get 33           Return bankDatabaseHandle 34        End Get 35     End Property ' BankDatabaseReference 36 37     ' perform the transaction (overridden by each derived class) 38     Public MustOverride Sub Execute() 39  End Class ' Transaction 

Class transaction has a constructor (lines 914) that takes the current user's account number and references to the ATM's screen and the bank's database as arguments. Because TRansaction is a MustInherit class (line 3), this constructor will never be called directly to instantiate TRansaction objects. Instead, this constructor will be invoked by the constructors of the transaction derived classes via MyBase.New.

Class transaction has three Public ReadOnly propertiesAccountNumber (lines 1721), ScreenReference (lines 2428) and BankDatabaseReference (lines 3135). Derived classes of transaction inherit these properties and use them to gain access to class TRansaction's Private instance variables. Note that we use the word "Reference" in the names of the ScreenReference and BankDatabaseReference properties for claritywe wanted to avoid property names that are the same as the class names Screen and BankDatabase, which can be confusing.

Class TRansaction also declares a MustOverride method Execute (line 38). It does not make sense to provide an implementation for this method in class TRansaction, because a generic transaction cannot be executed. Thus, we declare this method to be MustOverride, forcing each transaction derived class to provide its own concrete implementation that executes the particular type of transaction.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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