⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>12: friend class CheckingAccountImpl;</TT><TT>13: friend class SavingsAccountImpl;</TT><TT>14: </TT><TT>15: public:</TT><TT>16: </TT><TT>17:     // Destructor.</TT><TT>18:     ~AccountImpl();</TT><TT>19: </TT><TT>20:     // These methods are described in Account.idl.</TT><TT>21:     virtual char* accountNumber();</TT><TT>22:     virtual char* creationDate();</TT><TT>23:     virtual CORBA::Float balance();</TT><TT>24:     virtual CustomerList* getCustomers();</TT><TT>25:     virtual CORBA::Float withdraw(CORBA::Float amount);</TT><TT>26:     virtual CORBA::Float deposit(CORBA::Float amount);</TT><TT>27: </TT><TT>28: protected:</TT><TT>29: </TT><TT>30:     // Constructor.</TT><TT>31:     //</TT><TT>32:     // accountNumber - Account number.</TT><TT>33:     // creationDate - Account creation date.</TT><TT>34:     // initialBalance - Initial Account balance.</TT><TT>35:     // customer - Initial Account owner.</TT><TT>36:     AccountImpl(const char* accountNumber, const char* creationDate,</TT><TT>37:             CORBA::Float initialBalance, Customer_ptr customer);</TT><TT>38: </TT><TT>39: private:</TT><TT>40: </TT><TT>41:     // Default constructor.</TT><TT>42:     AccountImpl();</TT><TT>43: </TT><TT>44:     // This Account's account number.</TT><TT>45:     char* myAccountNumber;</TT><TT>46: </TT><TT>47:     // This Account's creation date.</TT><TT>48:     char* myCreationDate;</TT><TT>49: </TT><TT>50:     // This Account's current balance.</TT><TT>51:     CORBA::Float myBalance;</TT><TT>52: </TT><TT>53:     // This Account's owners.</TT><TT>54:     CustomerList myOwners;</TT><TT>55: };</TT><TT>56: 57: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 6.11. AccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // AccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;AccountImpl.h&quot;</TT><TT> 4: </TT><TT> 5: #include &lt;string.h&gt;</TT><TT> 6: </TT><TT> 7: // Constructor.</TT><TT> 8: //</TT><TT> 9: // accountNumber - Account number.</TT><TT>10: // creationDate - Account creation date.</TT><TT>11: // initialBalance - Initial Account balance.</TT><TT>12: // customer - Initial Account owner.</TT><TT>13: AccountImpl::AccountImpl(const char* accountNumber, const char*</TT><TT>14:         creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>15:         customer) : myAccountNumber(strdup(accountNumber)),</TT><TT>16:         myCreationDate(strdup(creationDate)),</TT><TT>17:         myBalance(initialBalance), myOwners() {</TT><TT>18: </TT><TT>19:     // Add the Customer to the owner list.</TT><TT>20:     myOwners.length(1);</TT><TT>21:     myOwners[0] = Customer::_duplicate(customer);</TT><TT>22: }</TT><TT>23: </TT><TT>24: // Default constructor.</TT><TT>25: AccountImpl::AccountImpl() : myAccountNumber(NULL),</TT><TT>26:         myCreationDate(NULL), myBalance(0.0), myOwners() {</TT><TT>27: </TT><TT>28: }</TT><TT>29: </TT><TT>30: // Destructor.</TT><TT>31: AccountImpl::~AccountImpl() {</TT><TT>32: </TT><TT>33:     free(myAccountNumber);</TT><TT>34:     free(myCreationDate);</TT><TT>35: }</TT><TT>36: </TT><TT>37: char* AccountImpl::accountNumber() {</TT><TT>38: </TT><TT>39:     return CORBA::strdup(myAccountNumber);</TT><TT>40: }</TT><TT>41: </TT><TT>42: char* AccountImpl::creationDate() {</TT><TT>43: </TT><TT>44:     return CORBA::strdup(myCreationDate);</TT><TT>45: }</TT><TT>46: </TT><TT>47: CORBA::Float AccountImpl::balance() {</TT><TT>48: </TT><TT>49:     return myBalance;</TT><TT>50: }</TT><TT>51: </TT><TT>52: CustomerList* AccountImpl::getCustomers() {</TT><TT>53: </TT><TT>54:     return &amp;myOwners;</TT><TT>55: }</TT><TT>56: </TT><TT>57: CORBA::Float AccountImpl::withdraw(CORBA::Float amount) {</TT><TT>58: </TT><TT>59:     myBalance -= amount;</TT><TT>60: </TT><TT>61:     return myBalance;</TT><TT>62: }</TT><TT>63: </TT><TT>64: CORBA::Float AccountImpl::deposit(CORBA::Float amount) {</TT><TT>65: </TT><TT>66:     myBalance += amount;</TT><TT>67: </TT><TT>68:     return myBalance;69: }</TT></FONT></PRE><H3><A NAME="Heading5"></A><FONT COLOR="#000077">Implementing the CheckingAccountInterface</FONT></H3><P>The <TT>CheckingAccount</TT> interface is the easiest interface to implement becauseit doesn't define any additional methods. The IDL definition for the <TT>CheckingAccount</TT>interface is shown in Listing 6.12.<H4><FONT COLOR="#000077">Listing 6.12. CheckingAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccount.idl</TT><TT> 2: </TT><TT> 3: #ifndef CheckingAccount_idl</TT><TT> 4: #define CheckingAccount_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Account.idl&quot;</TT><TT> 7: </TT><TT> 8: // A CheckingAccount is an Account which supports checking. It</TT><TT> 9: // does not gain any interest, as its sibling, the SavingsAccount,</TT><TT>10: // does.</TT><TT>11: interface CheckingAccount : Account {</TT><TT>12: </TT><TT>13: };</TT><TT>14: 15: #endif</TT> </FONT></PRE><P>Again, because there are no attributes or methods defined as part of the <TT>CheckingAccount</TT>interface, there is little to do for the implementation class. Simply providing anempty constructor and destructor is sufficient. The implementation for the <TT>CheckingAccount</TT>interface can be seen in Listings 6.13 and 6.14.<H4><FONT COLOR="#000077">Listing 6.13. CheckingAccountImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccountImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef CheckingAccountImpl_h</TT><TT> 4: #define CheckingAccountImpl_h</TT><TT> 5: </TT><TT> 6: #include &quot;../CheckingAccount_s.h&quot;</TT><TT> 7: #include &quot;AccountImpl.h&quot;</TT><TT> 8: </TT><TT> 9: class CheckingAccountImpl : public _sk_CheckingAccount {</TT><TT>10: </TT><TT>11: public:</TT><TT>12: </TT><TT>13:     // Constructor.</TT><TT>14:     //</TT><TT>15:     // accountNumber - Account number.</TT><TT>16:     // creationDate - Account creation date.</TT><TT>17:     // initialBalance - Initial Account balance.</TT><TT>18:     // customer - Initial Account owner.</TT><TT>19:     CheckingAccountImpl(const char* accountNumber, const char*</TT><TT>20:             creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>21:             customer);</TT><TT>22: </TT><TT>23:     // Destructor.</TT><TT>24:     ~CheckingAccountImpl();</TT><TT>25: </TT><TT>26:     // These methods are described in Account.idl.</TT><TT>27:     virtual char* accountNumber();</TT><TT>28:     virtual char* creationDate();</TT><TT>29:     virtual CORBA::Float balance();</TT><TT>30:     virtual CustomerList* getCustomers();</TT><TT>31:     virtual CORBA::Float withdraw(CORBA::Float amount);</TT><TT>32:     virtual CORBA::Float deposit(CORBA::Float amount);</TT><TT>33: </TT><TT>34: private:</TT><TT>35: </TT><TT>36:     // Default constructor.</TT><TT>37:     CheckingAccountImpl();</TT><TT>38: </TT><TT>39:     // My associated AccountImpl object.</TT><TT>40:     AccountImpl myAccount;</TT><TT>41: };</TT><TT>42: 43: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 6.14. CheckingAccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;CheckingAccountImpl.h&quot;</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: CheckingAccountImpl::CheckingAccountImpl(const char* accountNumber,</TT><TT>12:         const char* creationDate, CORBA::Float initialBalance,</TT><TT>13:         Customer_ptr customer) : myAccount(accountNumber,</TT><TT>14:         creationDate, initialBalance, customer) {</TT><TT>15: </TT><TT>16: }</TT><TT>17: </TT><TT>18: // Default constructor.</TT><TT>19: CheckingAccountImpl::CheckingAccountImpl() : myAccount(NULL, NULL,</TT><TT>20:         0.0, Customer::_nil()) {</TT><TT>21: </TT><TT>22: }</TT><TT>23: </TT><TT>24: // Destructor.</TT><TT>25: CheckingAccountImpl::~CheckingAccountImpl() {</TT><TT>26: </TT><TT>27: }</TT><TT>28: </TT><TT>29: char* CheckingAccountImpl::accountNumber() {</TT><TT>30: </TT><TT>31:     return myAccount.accountNumber();</TT><TT>32: }</TT><TT>33: </TT><TT>34: char* CheckingAccountImpl::creationDate() {</TT><TT>35: </TT><TT>36:     return myAccount.creationDate();</TT><TT>37: }</TT><TT>38: </TT><TT>39: CORBA::Float CheckingAccountImpl::balance() {</TT><TT>40: </TT><TT>41:     return myAccount.balance();</TT><TT>42: }</TT><TT>43: </TT><TT>44: CustomerList* CheckingAccountImpl::getCustomers() {</TT><TT>45: </TT><TT>46:     return myAccount.getCustomers();</TT><TT>47: }</TT><TT>48: </TT><TT>49: CORBA::Float CheckingAccountImpl::withdraw(CORBA::Float amount) {</TT><TT>50: </TT><TT>51:     return myAccount.withdraw(amount);</TT><TT>52: }</TT><TT>53: </TT><TT>54: CORBA::Float CheckingAccountImpl::deposit(CORBA::Float amount) {</TT><TT>55: </TT><TT>56:     return myAccount.deposit(amount);57: }</TT></FONT></PRE><H3><A NAME="Heading6"></A><FONT COLOR="#000077">Implementing the SavingsAccountInterface</FONT></H3><P>The <TT>SavingsAccount</TT> interface is more complicated than the <TT>CheckingAccount</TT>interface and so requires a bit more effort to implement. The <TT>SavingsAccount</TT>IDL definition is shown in Listing 6.15.<H4><FONT COLOR="#000077">Listing 6.15. SavingsAccount.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccount.idl</TT><TT> 2: </TT><TT> 3: #ifndef SavingsAccount_idl</TT><TT> 4: #define SavingsAccount_idl</TT><TT> 5: </TT><TT> 6: #include &quot;Account.idl&quot;</TT><TT> 7: </TT><TT> 8: // A SavingsAccount is an Account which supports savings</TT><TT> 9: // account semantics, such as gaining interest.</TT><TT>10: interface SavingsAccount : Account {</TT><TT>11: </TT><TT>12:     // This Account's interest rate.</TT><TT>13:     readonly attribute float interestRate;</TT><TT>14: </TT><TT>15:     // Set this Account's interest rate to the given rate.</TT><TT>16:     // Returns the previous rate.</TT><TT>17:     float setInterestRate(in float rate);</TT><TT>18: };</TT><TT>19: 20: #endif</TT> </FONT></PRE><P>For the <TT>SavingsAccount</TT> implementation class, you need to define <TT>getinterestRate()</TT>(an accessor for the <TT>interestRate</TT> attribute), <TT>setInterestRate()</TT>,and the class constructor (or constructors) and destructor. The implementation classappears in Listings 6.16 and 6.17.<H4><FONT COLOR="#000077">Listing 6.16. SavingsAccountImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccountImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef SavingsAccountImpl_h</TT><TT> 4: #define SavingsAccountImpl_h</TT><TT> 5: </TT><TT> 6: #include &quot;../SavingsAccount_s.h&quot;</TT><TT> 7: #include &quot;AccountImpl.h&quot;</TT><TT> 8: </TT><TT> 9: class SavingsAccountImpl : public AccountImpl {</TT><TT>10: </TT><TT>11: public:</TT><TT>12: </TT><TT>13:     // Constructor.</TT><TT>14:     //</TT><TT>15:     // accountNumber - Account number.</TT><TT>16:     // creationDate - Account creation date.</TT><TT>17:     // initialBalance - Initial Account balance.</TT><TT>18:     // customer - Initial Account owner.</TT><TT>19:     // interestRate - Initial Account interest rate.</TT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -