📄 ch06.htm
字号:
<TT>20: SavingsAccountImpl(const char* accountNumber, const char*</TT><TT>21: creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>22: customer, CORBA::Float interestRate);</TT><TT>23: </TT><TT>24: // Destructor.</TT><TT>25: ~SavingsAccountImpl();</TT><TT>26: </TT><TT>27: // These methods are described in Account.idl.</TT><TT>28: virtual char* accountNumber();</TT><TT>29: virtual char* creationDate();</TT><TT>30: virtual CORBA::Float balance();</TT><TT>31: virtual CustomerList* getCustomers();</TT><TT>32: virtual CORBA::Float withdraw(CORBA::Float amount);</TT><TT>33: virtual CORBA::Float deposit(CORBA::Float amount);</TT><TT>34: </TT><TT>35: // These methods are described in SavingsAccount.idl.</TT><TT>36: virtual CORBA::Float interestRate();</TT><TT>37: virtual CORBA::Float setInterestRate(CORBA::Float rate);</TT><TT>38: </TT><TT>39: private:</TT><TT>40: </TT><TT>41: // Default constructor.</TT><TT>42: SavingsAccountImpl();</TT><TT>43: </TT><TT>44: // This Account's interest rate.</TT><TT>45: CORBA::Float myInterestRate;</TT><TT>46: </TT><TT>47: // My associated AccountImpl object.</TT><TT>48: AccountImpl myAccount;</TT><TT>49: };</TT><TT>50: 51: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 6.17. SavingsAccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "SavingsAccountImpl.h"</TT><TT> 4: </TT><TT> 5: // Constructor.</TT><TT> 6: //</TT><TT> 7: // accountNumber - Account number.</TT><TT> 8: // creationDate - Account creation date.</TT><TT> 9: // initialBalance - Initial Account balance.</TT><TT>10: // customer - Initial Account owner.</TT><TT>11: // interestRate - Initial Account interest rate.</TT><TT>12: SavingsAccountImpl::SavingsAccountImpl(const char* accountNumber,</TT><TT>13: const char* creationDate, CORBA::Float initialBalance,</TT><TT>14: Customer_ptr customer, CORBA::Float interestRate) :</TT><TT>15: myAccount(accountNumber, creationDate, initialBalance,</TT><TT>16: customer), myInterestRate(interestRate) {</TT><TT>17: </TT><TT>18: }</TT><TT>19: </TT><TT>20: // Default constructor.</TT><TT>21: SavingsAccountImpl::SavingsAccountImpl() : myAccount(NULL, NULL,</TT><TT>22: 0.0, Customer::_nil()), myInterestRate(0.0) {</TT><TT>23: </TT><TT>24: }</TT><TT>25: </TT><TT>26: // Destructor.</TT><TT>27: SavingsAccountImpl::~SavingsAccountImpl() {</TT><TT>28: </TT><TT>29: }</TT><TT>30: </TT><TT>31: char* SavingsAccountImpl::accountNumber() {</TT><TT>32: </TT><TT>33: return myAccount.accountNumber();</TT><TT>34: }</TT><TT>35: </TT><TT>36: char* SavingsAccountImpl::creationDate() {</TT><TT>37: </TT><TT>38: return myAccount.creationDate();</TT><TT>39: }</TT><TT>40: </TT><TT>41: CORBA::Float SavingsAccountImpl::balance() {</TT><TT>42: </TT><TT>43: return myAccount.balance();</TT><TT>44: }</TT><TT>45: </TT><TT>46: CustomerList* SavingsAccountImpl::getCustomers() {</TT><TT>47: </TT><TT>48: return myAccount.getCustomers();</TT><TT>49: }</TT><TT>50: </TT><TT>51: CORBA::Float SavingsAccountImpl::withdraw(CORBA::Float amount) {</TT><TT>52: </TT><TT>53: return myAccount.withdraw(amount);</TT><TT>54: }</TT><TT>55: </TT><TT>56: CORBA::Float SavingsAccountImpl::deposit(CORBA::Float amount) {</TT><TT>57: </TT><TT>58: return myAccount.deposit(amount);</TT><TT>59: }</TT><TT>60: </TT><TT>61: CORBA::Float SavingsAccountImpl::interestRate() {</TT><TT>62: </TT><TT>63: return myInterestRate;</TT><TT>64: }</TT><TT>65: </TT><TT>66: CORBA::Float SavingsAccountImpl::setInterestRate(CORBA::Float rate) {</TT><TT>67: </TT><TT>68: CORBA::Float oldInterestRate = myInterestRate;</TT><TT>69: </TT><TT>70: myInterestRate = rate;</TT><TT>71: </TT><TT>72: return oldInterestRate;73: }</TT></FONT></PRE><H2><A NAME="Heading7"></A><FONT COLOR="#000077">Implementing Basic Client Capabilities</FONT></H2><P>Now that you have implemented the basic capabilities of the CORBA server for the<TT>Bank</TT> application, you're ready to begin working on the basic client capabilities.Since most of the work is done by the server in this application, you'll find theclient to be fairly simple by comparison.<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Implementing the Customer Interface</FONT></H3><P>The <TT>Customer</TT> interface encapsulates the attributes and behaviors of a<TT>Customer</TT> of a <TT>Bank</TT>. For the most part, this interface serves asa container for <TT>Account</TT> objects. The IDL definition for the <TT>Customer</TT>interface is shown in Listing 6.18.<H4><FONT COLOR="#000077">Listing 6.18. Customer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Customer.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Customer interface.</TT><TT> 4: interface Customer;</TT><TT> 5: </TT><TT> 6: #ifndef Customer_idl</TT><TT> 7: #define Customer_idl</TT><TT> 8: </TT><TT> 9: // sequence of Customers</TT><TT>10: typedef sequence<Customer> CustomerList;</TT><TT>11: </TT><TT>12: #include "Account.idl"</TT><TT>13: </TT><TT>14: // A Customer can hold one or more Accounts. Presumably, the</TT><TT>15: // Customer is what drives the rest of this application.</TT><TT>16: interface Customer {</TT><TT>17: </TT><TT>18: // This Customer's name.</TT><TT>19: attribute string name;</TT><TT>20: </TT><TT>21: // This Customer's Social Security number.</TT><TT>22: readonly attribute string socialSecurityNumber;</TT><TT>23: </TT><TT>24: // This Customer's address.</TT><TT>25: attribute string address;</TT><TT>26: </TT><TT>27: // This Customer's mother's maiden name.</TT><TT>28: readonly attribute string mothersMaidenName;</TT><TT>29: </TT><TT>30: // Return a list of Accounts held (or co-held) by this</TT><TT>31: // Customer.</TT><TT>32: AccountList getAccounts();</TT><TT>33: };</TT><TT>34: 35: #endif</TT> </FONT></PRE><P>You need to implement <TT>getname()</TT>, <TT>setname()</TT>, <TT>getsocialSecurityNumber()</TT>,<TT>getaddress()</TT>, <TT>setAddress()</TT>, and <TT>getmothersMaidenName()</TT>,which are the accessors and mutators for various attributes of <TT>Account</TT>,along with <TT>getAccounts()</TT> and the class constructor (or constructors) anddestructor. The implementation for <TT>CustomerImpl</TT> appears in Listings 6.19and 6.20.<H4><FONT COLOR="#000077">Listing 6.19. CustomerImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CustomerImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef CustomerImpl_h</TT><TT> 4: #define CustomerImpl_h</TT><TT> 5: </TT><TT> 6: #include "../Customer_s.h"</TT><TT> 7: </TT><TT> 8: class CustomerImpl : public _sk_Customer {</TT><TT> 9: </TT><TT>10: public:</TT><TT>11: </TT><TT>12: // Constructor.</TT><TT>13: //</TT><TT>14: // name - Customer's name.</TT><TT>15: // socialSecurityNumber - Customer's Social Security number.</TT><TT>16: // address - Customer's address.</TT><TT>17: // mothersMaidenName - Customer's mother's maiden name.</TT><TT>18: CustomerImpl(const char* name, const char* socialSecurityNumber,</TT><TT>19: const char* address, const char* mothersMaidenName);</TT><TT>20: </TT><TT>21: // Destructor.</TT><TT>22: ~CustomerImpl();</TT><TT>23: </TT><TT>24: // These methods are described in Customer.idl.</TT><TT>25: virtual char* name();</TT><TT>26: virtual void name(const char* val);</TT><TT>27: virtual char* socialSecurityNumber();</TT><TT>28: virtual char* address();</TT><TT>29: virtual void address(const char* val);</TT><TT>30: virtual char* mothersMaidenName();</TT><TT>31: virtual AccountList* getAccounts();</TT><TT>32: </TT><TT>33: private:</TT><TT>34: </TT><TT>35: // Default constructor.</TT><TT>36: CustomerImpl();</TT><TT>37: </TT><TT>38: // This Customer's name.</TT><TT>39: char* myName;</TT><TT>40: </TT><TT>41: // This Customer's Social Security number.</TT><TT>42: char* mySocialSecurityNumber;</TT><TT>43: </TT><TT>44: // This Customer's address.</TT><TT>45: char* myAddress;</TT><TT>46: </TT><TT>47: // This Customer's mother's maiden name.</TT><TT>48: char* myMothersMaidenName;</TT><TT>49: </TT><TT>50: // This Customer's Accounts.</TT><TT>51: AccountList myAccounts;</TT><TT>52: };</TT><TT>53: 54: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 6.20. CustomerImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CustomerImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "CustomerImpl.h"</TT><TT> 4: </TT><TT> 5: #include <string.h></TT><TT> 6: </TT><TT> 7: // Constructor.</TT><TT> 8: //</TT><TT> 9: // name - Customer's name.</TT><TT>10: // socialSecurityNumber - Customer's Social Security number.</TT><TT>11: // address - Customer's address.</TT><TT>12: // mothersMaidenName - Customer's mother's maiden name.</TT><TT>13: CustomerImpl::CustomerImpl(const char* name, const char*</TT><TT>14: socialSecurityNumber, const char* address, const char*</TT><TT>15: mothersMaidenName) : myName(strdup(name)),</TT><TT>16: mySocialSecurityNumber(strdup(socialSecurityNumber)),</TT><TT>17: myAddress(strdup(address)),</TT><TT>18: myMothersMaidenName(strdup(mothersMaidenName)),</TT><TT>19: myAccounts() {</TT><TT>20: </TT><TT>21: }</TT><TT>22: </TT><TT>23: // Default constructor.</TT><TT>24: CustomerImpl::CustomerImpl() : myName(NULL),</TT><TT>25: mySocialSecurityNumber(NULL), myAddress(NULL),</TT><TT>26: myMothersMaidenName(NULL), myAccounts() {</TT><TT>27: </TT><TT>28: }</TT><TT>29: </TT><TT>30: // Destructor.</TT><TT>31: CustomerImpl::~CustomerImpl() {</TT><TT>32: </TT><TT>33: free(myName);</TT><TT>34: free(mySocialSecurityNumber);</TT><TT>35: free(myAddress);</TT><TT>36: free(myMothersMaidenName);</TT><TT>37: }</TT><TT>38: </TT><TT>39: char* CustomerImpl::name() {</TT><TT>40: </TT><TT>41: return CORBA::strdup(myName);</TT><TT>42: }</TT><TT>43: </TT><TT>44: void CustomerImpl::name(const char* val) {</TT><TT>45: </TT><TT>46: free(myName);</TT><TT>47: myName = strdup(val);</TT><TT>48: }</TT><TT>49: </TT><TT>50: char* CustomerImpl::socialSecurityNumber() {</TT><TT>51: </TT><TT>52: return CORBA::strdup(mySocialSecurityNumber);</TT><TT>53: }</TT><TT>54: </TT><TT>55: char* CustomerImpl::address() {</TT><TT>56: </TT><TT>57: return CORBA::strdup(myAddress);</TT><TT>58: }</TT><TT>59: </TT><TT>60: void CustomerImpl::address(const char* val) {</TT><TT>61: </TT><TT>62: free(myAddress);</TT><TT>63: myAddress = strdup(val);</TT><TT>64: }</TT><TT>65: </TT><TT>66: char* CustomerImpl::mothersMaidenName() {</TT><TT>67: </TT><TT>68: return CORBA::strdup(myMothersMaidenName);</TT><TT>69: }</TT><TT>70: </TT><TT>71: AccountList* CustomerImpl::getAccounts() {</TT><TT>72: </TT><TT>73: return &myAccounts;74: }</TT></FONT></PRE><H3><A NAME="Heading9"></A><FONT COLOR="#000077">Implementing Additional Client Functionality</FONT></H3><P>In addition to the <TT>Customer</TT> interface, any useful client needs to implementadditional functionality. An examination of the <TT>Customer</TT> implementationwill suggest to you why this is so: Although the implementation allows a <TT>Customer</TT>to interact with various other components of the <TT>Bank</TT> application (su
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -