validatedobject.htm
来自「The goal of this library is to make ODBC」· HTM 代码 · 共 381 行 · 第 1/2 页
HTM
381 行
have left in the account</span><br>
double amtWithdrawn = (balance
- amt >= 0 ? amt : balance);<br>
balance -= amtWithdrawn;<br>
return amtWithdrawn; <span class="codeComment">//
return actual amount withdrawn</span><br>
}<br>
<br>
<span class="codeComment">// called by creditors to make bank pay!
Account could get frozen here!</span><br>
<span class="codeComment">// assume bank always pays creditors even if
account is frozen</span><br>
<span class="codeComment">// calls to any other MoneyAccount methods
should properly discover</span><br>
<span class="codeComment">// any new debt and thus freeze account</span><br>
void pay_creditors(unsigned double amt) {
balance -= amt; }<br>
};<br>
<br>
void IWillThrow()<br>
{ <br>
<span class="codeComment">// create account and then perform some
transactions</span><br>
MoneyAccount acct(200.00);<br>
acct.deposit(700.00);<br>
acct.withdraw(550.00);<br>
<br>
<span class="codeComment">// uh oh ... now creditors take my money away,
my account is frozen</span><br>
acct.pay_creditors(1000);<br>
<br>
<span class="codeComment">// now trying to do something with my frozen
account will throw ValidityException</span><br>
acct.withdraw(50.00);<br>
} <br>
<br>
int main()<br>
{<br>
try<br>
{<br>
<span class="codeComment">// call our method which
throws</span><br>
IWillThrow();<br>
}<br>
catch (RootException &ex)<br>
{<br>
<span class="codeComment">// can also say: cout
<< ex << endl;</span><br>
<span class="codeComment">// operator<<() for
RootExceptions just streams out what()</span><br>
cout << ex.what()
<< endl;<br>
}<br>
return 0; <span class="codeComment">// won't reach here ... exception
thrown above</span><br>
}</code></p>
<h3>Model of</h3>
<p>None.</p>
<h3>Public base classes</h3>
<p><font size="3">None.</font></p>
<h3>Members</h3>
<table border="2">
<tr>
<th>Member </th>
<th>Where defined </th>
<th>Description </th>
</tr>
<tr>
<td valign="top"><font size="2" face="Courier New">enum
Validity { INVALID = 0, VALID = 1 };</font></td>
<td valign="top"><tt>ValidatedObject</tt> </td>
<td valign="top"><font size="3">Enumerated type. Is this
object valid or not? </font><font size="2"
face="Courier New">INVALID </font><font size="3">means
object is in an invalid state. </font><font size="2"
face="Courier New">VALID </font><font size="3">means
object is in an valid state. </font></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Courier New">enum
Revalidation { NONE_TRIED = 0, REVALIDATE_FAILED = 1 };</font></td>
<td valign="top"><tt>ValidatedObject</tt> </td>
<td valign="top">Enumerated type. Flag passed to <tt>ValidityException
</tt>when its thrown indicating whether the failure
occurred on an unsuccessful revalidation attempt.</td>
</tr>
<tr>
<td valign="top"><font size="2" face="Courier New">ValidatedObject()</font></td>
<td valign="top"><tt>ValidatedObject</tt> </td>
<td valign="top">Default constructor. Initially, the
object is in a<font size="3" face="Courier New"> </font><font
size="2" face="Courier New">VALID</font><font size="3">
state.</font></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Courier New">Validity
GetValidity()</font></td>
<td valign="top"><tt>ValidatedObject</tt> </td>
<td valign="top">Return the state of the object as a <font
size="2" face="Courier New">Validity </font><font
size="3">value.</font></td>
</tr>
<tr>
<td valign="top"><tt>virtual bool valid()</tt></td>
<td valign="top"><tt>ValidatedObject</tt></td>
<td valign="top">Returns whether the object is in a valid
state.To define the critieria used to define whether the
object is in a valid state or not, override this method.
Often, in your <font size="2" face="Courier New">valid() </font>method,
you may wish to test whether the object is already valid
to save some work as no further testing is required if
the object is found to already be invalid. </td>
</tr>
<tr>
<td valign="top"><tt>virtual void invalidate()</tt></td>
<td valign="top"><tt>ValidatedObject</tt></td>
<td valign="top">Invalidate this object by changing the
state to <font size="2" face="Courier New">INVALID. </font>You
can override this method if your class needs to also set
some other flags or doing some other processing when it
becomes <font size="2" face="Courier New">INVALID</font>.</td>
</tr>
<tr>
<td valign="top"><tt>virtual void validate()</tt></td>
<td valign="top"><tt>ValidatedObject</tt> </td>
<td valign="top">Validates the object by calling <tt>valid().
</tt><font size="3">If the object is an invalid state,
throws a </font><font size="2" face="Courier New">ValidityException.
</font>You can override this method if your class needs
to also set some other flags, needs to do some other
processing at validation time., or you want other
behavior than an exception being thrown when the object
proves to be <font size="2" face="Courier New">INVALID.</font>
You can choose whether any of your object's method needs
to validate the object or not by calling <font size="2"
face="Courier New">validate()</font>at the beginning of
the method (or at any other time in the method) if you
wish to validate the object's state before proceeding. If
a method does not need validation because you wish to
force the operation to succeed (even if the object is in
an <font size="2" face="Courier New">INVALID </font>state),
just don't call <tt>validate().</tt></td>
</tr>
<tr>
<td valign="top"><tt>virtual void revalidate()</tt></td>
<td valign="top"><tt>ValidatedObject</tt></td>
<td valign="top">Attempts to revalidate the object by
trying to reconciling its internal state. Unless you
override the <font size="2" face="Courier New">revalidate()
</font>method, once an object becomes invalid, it remains
invalid permanently and all validated methods will throw
a <font size="2" face="Courier New">ValidityException</font>.
Your <font size="2" face="Courier New">revalidate() </font>method
tries to reconcile the state of the object, doing
whatever processing or cleanup it needs to return the
object to a valid state. This reconciling of the object
can either always succeed, sometimes succeed (and either
throwing <tt>ValidityException </tt>or doing something
else in case of failure), or never succeed (also
achievable just by not overriding <font size="2"
face="Courier New">revalidate(), </font>which will throw <tt>ValidityException</tt>).</td>
</tr>
</table>
<h3>Notes</h3>
<p>None.<font size="3"><tt><br>
</tt></font></p>
<h3>See also</h3>
<p><a href="RootException.htm"><font size="2" face="Courier New">RootException</font></a>,
<a href="ValidityException.htm"><font size="2" face="Courier New">ValidityException</font></a>.<br>
<br>
</p>
<hr>
<p><a href="index.htm"><img src="dtl_home.gif" alt="[DTL Home]"
width="54" height="54"></a> <br>
</p>
<p>Copyright
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?