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

📄 pat5j.htm

📁 四人帮《设计模式》一书英文版本
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD>	<TITLE>Template Method</TITLE>
<SCRIPT>
function setFocus() {	
	if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) {
	return;
	} else {
	self.focus();
	}
}
</SCRIPT>
</HEAD>

<BODY   BGCOLOR         = #FFFFFF onLoad="setFocus()";>

<A NAME="top"></A>
<A NAME="TemplateMethod"></A>
<A NAME="intent"></A>
<H2><A HREF="#motivation"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 ALT="next: 
Motivation"></A> Intent</H2> 

<A NAME="auto1000"></A>
<P>Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. Template Method lets subclasses redefine
certain steps of an algorithm without changing the algorithm's
structure.</P>

<A NAME="motivation"></A>
<H2><A HREF="#applicability"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 
ALT="next: Applicability"></A> Motivation</H2> 

<A NAME="auto1001"></A>
<P>Consider an application framework that provides Application and
Document classes.  The Application class is responsible for opening
existing documents stored in an external format, such as a file.  A
Document object represents the information in a document once it's
read from the file.</P>

<A NAME="auto1002"></A>
<P>Applications built with the framework can subclass Application and
Document to suit specific needs.  For example, a drawing application
defines DrawApplication and DrawDocument subclasses; a spreadsheet
application defines SpreadsheetApplication and SpreadsheetDocument
subclasses.

<A NAME="325c"></A>
<P ALIGN=CENTER><IMG SRC="tmeth007.gif" tppabs="http://ultra/development/DesignPatterns/hires/Pictures/tmeth007.gif"></P>

<A NAME="auto1003"></A>
<P>The abstract Application class defines the algorithm for opening and
reading a document in its OpenDocument operation:</P>

<A NAME="auto1004"></A>
<PRE>
    void Application::OpenDocument (const char* name) {
        if (!CanOpenDocument(name)) {
            // cannot handle this document
            return;
        }
    
        Document* doc = DoCreateDocument();
    
        if (doc) {
            _docs->AddDocument(doc);
            AboutToOpenDocument(doc);
            doc->Open();
            doc->DoRead();
        }
    }
</PRE>

<A NAME="auto1005"></A>
<P>OpenDocument defines each step for opening a document.  It checks if
the document can be opened, creates the application-specific Document
object, adds it to its set of documents, and reads the Document from a
file.</P>

<A NAME="deftempmeth"></A>
<P>We call OpenDocument a <STRONG>template method</STRONG>.  A template method
defines an algorithm in terms of abstract operations that subclasses
override to provide concrete behavior.  Application subclasses define
the steps of the algorithm that check if the document can be opened
(CanOpenDocument) and that create the Document (DoCreateDocument).
Document classes define the step that reads the document (DoRead).
The template method also defines an operation that lets Application
subclasses know when the document is about to be opened
(AboutToOpenDocument), in case they care.</P>

<A NAME="auto1006"></A>
<P>By defining some of the steps of an algorithm using abstract
operations, the template method fixes their ordering, but it lets
Application and Document subclasses vary those steps to suit their
needs.</P>

<A NAME="applicability"></A>
<H2><A HREF="#structure"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 ALT="next: 
Structure"></A> Applicability</H2> 

<A NAME="auto1007"></A>
<P>The Template Method pattern should be used</P>

<UL>

<A NAME="auto1008"></A>
<LI>to implement the invariant parts of an algorithm once and leave it up
to subclasses to implement the behavior that can vary.</LI>

<A NAME="auto1009"></A>
<P></P>

<A NAME="refactoring"></A>
<LI>when common behavior among subclasses should be factored and localized
in a common class to avoid code duplication. This is a good example of
"refactoring to generalize" as described by Opdyke and
Johnson [<A HREF="bibfs.htm#opdyke-johnson_refactoring2" tppabs="http://ultra/development/DesignPatterns/hires/bibfs.htm#opdyke-johnson_refactoring2" TARGET="_mainDisplayFrame">OJ93</A>].  You first identify the
differences in the existing code and then separate the differences
into new operations.  Finally, you replace the differing code with a
template method that calls one of these new operations.</LI>

<A NAME="auto1010"></A>
<P></P>

<A NAME="auto1011"></A>
<LI>to control subclasses extensions. You can define a template method
that calls "hook" operations (see Consequences) at specific points,
thereby permitting extensions only at those points.</LI>

</UL>

<A NAME="structure"></A>
<H2><A HREF="#participants"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 ALT="next: 
Participants"></A> Structure</H2> 

<P ALIGN=CENTER><IMG SRC="tmethod.gif" tppabs="http://ultra/development/DesignPatterns/hires/Pictures/tmethod.gif"></P>

<A NAME="participants"></A>
<H2><A HREF="#collaborations"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 
ALT="next: Collaborations"></A> Participants</H2>

<UL>

<A NAME="auto1012"></A>
<LI><B>AbstractClass</B> (Application)

<A NAME="auto1013"></A>
<P></P>

<UL>

<A NAME="oper-primitive1"></A>
<LI>defines abstract <STRONG>primitive operations</STRONG> that concrete
subclasses define to implement steps of an algorithm.</LI>

<A NAME="auto1014"></A>
<P></P>

<A NAME="skeleton"></A>
<LI>implements a template method defining the skeleton of an algorithm.
The template method calls primitive operations as well as operations
defined in AbstractClass or those of other objects.</LI>

</UL>

<A NAME="auto1015"></A>
<P></P>

<A NAME="auto1016"></A>
<LI><B>ConcreteClass</B> (MyApplication)

<A NAME="auto1017"></A>
<P></P>

<UL>

<A NAME="auto1018"></A>
<LI>implements the primitive operations to carry out subclass-specific
steps of the algorithm.</LI>

</UL>

</UL>

<A NAME="collaborations"></A>
<H2><A HREF="#consequences"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 
ALT="next: Consequences"></A> Collaborations</H2>

<UL>

<A NAME="auto1019"></A>
<LI>ConcreteClass relies on AbstractClass to implement the invariant steps of 
the algorithm.</LI>

</UL>

<A NAME="consequences"></A>
<H2><A HREF="#implementation"><IMG SRC="down3.gif" tppabs="http://ultra/development/DesignPatterns/hires/gifsb/down3.gif" BORDER=0 
ALT="next: Implementation"></A> Consequences</H2> 

<A NAME="auto1020"></A>
<P>Template methods are a fundamental technique for code reuse.  They are
particularly important in class libraries, because they are the means
for factoring out common behavior in library classes.</P>

<A NAME="auto1021"></A>
<P>Template methods lead to an inverted control structure that's
sometimes referred to as "the Hollywood principle," that is, "Don't
call us, we'll call you" [<A HREF="bibfs.htm#hollywood-principle" tppabs="http://ultra/development/DesignPatterns/hires/bibfs.htm#hollywood-principle" TARGET="_mainDisplayFrame">Swe85</A>].  This refers to
how a parent class calls the operations of a subclass and not the
other way around.</P>

<A NAME="oper-concrete"></A>
<P>Template methods call the following kinds of operations:</P>

<UL>

<A NAME="auto1022"></A>
<LI>concrete operations (either on the ConcreteClass or on
client classes);</LI>

⌨️ 快捷键说明

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