📄 index.html
字号:
<ul>
<blockquote>
<pre>inline void ItemIterator::next()
{
pos++;
}
bool ItemIterator::is_done() const
{
return pos >= items.size();
}</pre>
</blockquote>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: Putting Patterns to Work (cont.)
</font></h2>
<font size="+1">
<ul>
<li>Consider the current format of the invoice:
<script><!--
image( "un08.png" )
//--></script>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: Putting Patterns to Work (cont.)
</font></h2>
<font size="+1">
<ul>
<li>Sufficient for all applications?</li>
<li>Printing a Web page? Need HTML tags</li>
<li>Need multiple algorithms for formatting</li>
<li>Use STRATEGY pattern</li>
<li>Design i/f to abstract out essential steps of the algorithm:
<ul>
<li>Print header</li>
<li>Print footer</li>
<li>Print column entry (string or number)</li>
</ul>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: Putting Patterns to Work (cont.)
</font></h2>
<font size="+1">
<ul>
<li>The i/f:
<blockquote>
<pre>class InvoicePrinter
{
public:
virtual void print_header(string s) = 0;
virtual void print_string(string value, bool pad_right) = 0;
virtual void print_number(double value, int precision) = 0;
virtual void print_footer(string s, double total) = 0;
};</pre>
</blockquote>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: Putting Patterns to Work (cont.)
</font></h2>
<font size="+1">
<ul>
<li>Make the strategy object available to <tt>Invoice::print</tt>:
<blockquote>
<pre>
void Invoice::print(InvoiceFormatter& formatter)
{
print_header("I N V O I C E");
print_string("Description", true);
print_string("Unit Price", false);
print_string("Qty", false);
print_string("Total Price", false);
double amount_due = 0;
for (ItemIterator iter = inv.create_iterator();
!iter.is_done(); iter.next())
{
Item* it = iter.get();
print_string(it->get_description(), true);
print_number(it->get_unit_price(), 2);
print_number(it->get_quantity(), 0);
print_number(it->get_total_price(), 2);
amount_due = amount_due + it->get_total_price();
}
print_footer("AMOUNT DUE:", amount_due);
}</pre>
</blockquote>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: Putting Patterns to Work (cont.)
</font></h2>
<font size="+1">
<ul>
<li>Relationships between classes for formatting:
<script><!--
image( "fig05.png" )
//--></script>
</li>
</ul>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>product.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "product.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>item.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "item.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>productitem.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "productitem.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>bundle.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "bundle.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>itemiterator.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "itemiterator.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>invoiceprinter.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "invoiceprinter.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>simpleinvoiceprinter.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "simpleinvoiceprinter.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>invoice.h</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "invoice.h", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>product.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "product.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>item.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "item.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>productitem.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "productitem.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>bundle.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "bundle.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>itemiterator.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "itemiterator.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study:
(<tt>simpleinvoiceprinter.cpp</tt>)</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "simpleinvoiceprinter.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>invoice.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "invoice.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">26.7 Case Study: (<tt>invoicetest.cpp</tt>)
</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "invoicetest.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">More Patterns</font></h2>
<font size="+1">
<p>Quick table of patterns from <u>Design Patterns</u>:</p>
<table border='1' cellpadding='4'>
<tr bgcolor='#00ffff'>
<th>Pattern Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<th>Abstract Factory</th>
<td>An abstract class defines methods that construct related products.
Concrete factories create these product sets</td>
<td>An abstract class specifies methods for constructing buttons, methods,
and so on. Each user interface look and feel supplies a concrete
subclass</td>
</tr>
<tr>
<th>Bridge</th>
<td>An abstraction and its implementation have separate inheritance
hierarchies</td>
<td>A hierarchy of window types has separate implementations in various
operating systems</td>
</tr>
<tr>
<th>Builder</th>
<td>A builder class has methods to build parts of a complex product, and to
retrieve the completed product</td>
<td>A document builder has methods to build paragraphs, tables, and so
on</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">More Patterns (cont.)</font></h2>
<font size="+1">
<table border='1' cellpadding='4'>
<tr bgcolor='#00ffff'>
<th>Pattern Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<th>Chain of Responsibility</th>
<td>A request is passed to the first handler in a chain. Each handler acts
on the request (or chooses not to act), and passes the request on to the
next handler</td>
<td>An event-handling mechanism passes a mouse or key event to a component,
which then passes it to the parent component</td>
</tr>
<tr>
<th>Command</th>
<td>Commands are implemented as objects</td>
<td>A word processor stores recently issued commands so that they can be
undone</td>
</tr>
<tr>
<th>Decorator</th>
<td>The behavior of a class is enhanced, keeping its interface</td>
<td>A user interface component is decorated with scroll bars or borders</td>
</tr>
<tr>
<th>Facade</th>
<td>A complex subsystem is accessed through a single class</td>
<td>A driver class provides access to the functionality of a database
system</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">More Patterns (cont.)</font></h2>
<font size="+1">
<table border='1' cellpadding='4'>
<tr bgcolor='#00ffff'>
<th>Pattern Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<th>Factory Method</th>
<td>A virtual constructor can be redefined by derived classes</td>
<td>Collection classes that derive from a common base class redefine the
create_iterator function</td>
</tr>
<tr>
<th>Flyweight</th>
<td>Uses shared objects instead of large numbers of separate objects with
identical state</td>
<td>A word processor uses shared objects for styled characters rather than a
separate object for each character</td>
</tr>
<tr>
<th>Interpreter</th>
<td>A class hierarchy represents grammar rules. The interpreter recursively
evaluates a parse tree of rule objects</td>
<td>A program interactively evaluates mathematical expressions by building
and evaluating a parse tree</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">More Patterns (cont.)</font></h2>
<font size="+1">
<table border='1' cellpadding='4'>
<tr bgcolor='#00ffff'>
<th>Pattern Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<th>Mediator</th>
<td>An object encapsulates the interaction of other objects</td>
<td>All components in a dialog box notify a mediator of state changes. The
mediator updates affected components</td>
</tr>
<tr>
<th>Memento</th>
<td>An object yields an opaque snapshot of a part of its state, and can
later return its state from that snapshot</td>
<td>An undo mechanism requests a memento from an object before mutating it.
If the operation is undone, the memento is used to roll the object back
to its old state</td>
</tr>
<tr>
<th>Observer</th>
<td>An object wants to be notified when another object generates an
event</td>
<td>User interface components generate events, such as button clicks and
text changes. A dialog box observes the events and repaints its
contents</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">More Patterns (cont.)</font></h2>
<font size="+1">
<table border='1' cellpadding='4'>
<tr bgcolor='#00ffff'>
<th>Pattern Name</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<th>Proxy</th>
<td>A service needs to be made more versatile without affecting the service
provider or client</td>
<td>A proxy class sends client requests to a server object on a different
computer</td>
</tr>
<tr>
<th>Singleton</th>
<td>All clients need access to a single shared object of a class</td>
<td>A random number singleton gives all clients access to the same
generator</td>
</tr>
<tr>
<th>State</th>
<td>A separate object is used for each state. State-dependent code is
distributed over the various state classes</td>
<td>An image editor has different drawing states. Each state is handled by
a separate tool object</td>
</tr>
<tr>
<th>Visitor</th>
<td>A structure with a fixed set of element classes needs an extensible set
of operations</td>
<td>An XML visitor visits a tree of XML elements, applying arbitrary
operations to each node</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">Chapter Summary</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">
<ol>
<li>Iterators are preferred to cursors</li>
<li>A design pattern uses a standard format to give proven advice about a
problem in software design</li>
<li>The ITERATOR pattern teaches how to access the elements of an aggregate
object</li>
<li>The ADAPTER pattern teaches how to use a class in a context that requires
a different interface</li>
<li>The STRATEGY pattern teaches how to supply variants of an algorithm to a
client</li>
<li>The TEMPLATE METHOD pattern teaches how to supply varying behavior
patterns to an algorithm</li>
<li>The COMPOSITE pattern teaches how to combine several objects into an
object that has the same behavior as its parts</li>
<li>Design patterns apply in specific situations that are described by the
context and solution parts of the pattern</li>
</ol>
</font>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -