Changes

Jump to: navigation, search

Template Method

5,768 bytes added, 19:56, 19 March 2007
Apache Tomcat
== UML Diagram ==
[[Image:TemplateMethod.JPG]] Source from GoF's Design Patterns - Elements of Reusable Object -Oriented Software (pg. 325) == Pseudo Code Examples =='''=== Java'''===
<pre>
//Coercion Polymorphism
}
</pre>
 Source: [from Javacamp, http://www.javacamp.org/designPattern/template.html/ === PHP 5.0 === <pre>abstract class AbstractClass { public final function templateMethod() { print "AbstractClass::templateMethod() called.\n"; $this->mandatoryOperation(); $this->optionalOperation(); }  protected abstract function mandatoryOperation();  protected function optionalOperation() { }}class ConcreteClass extends AbstractClass { protected function mandatoryOperation() { print "ConcreteClass::mandatoryOperation() called.\n"; }  protected function optionalOperation() { print "ConcreteClass::optionalOperation() called.\n"; }}</pre> Source from Zend Technologies, http://www.zend.com/zend/php5/php5-OOP.php?article=php5-OOP&kind=ph&id=3204&open=1&anc=0&view=1 == Real-life Applications == === Apache Excalibur === The following are portions of the code found in [http://www.google.com/codesearch?hl=en&q=show:b65X65FJv3k:tIVs6rnDDGE:CuwdO4_VJq8&sa=N&ct=rd&cs_p=https://svn.apache.org/repos/asf/excalibur/trunk&cs_f=framework/api/src/test/org/apache/avalon/framework/test/CascadingErrorTestCase.java[CascadingErrorTestCase.java]] and [http://www.google.com/codesearch?hl=en&q=show:5CjVErg7uPw:tIVs6rnDDGE:W66LfbDC3n0&sa=N&ct=rd&cs_p=https://svn.apache.org/repos/asf/excalibur/trunk&cs_f=framework/api/src/test/org/apache/avalon/framework/test/CascadingExceptionTestCase.java[CascadingEexceptionTestCase.java]] files.<br/><br/>When comparing these two files, we can see that the two files share a common package '''org.apache.avalon.framework.test'''.<pre> package org.apache.avalon.framework.test; import org.apache.avalon.framework.CascadingError;import org.apache.avalon.framework.CascadingThrowable; import junit.framework.TestCase; </pre> From the following, we can see that the class '''CascadingErrorTestCase''' is extending from an abstract class called '''TestCase'''. This is the same case in the '''CascadingExceptionTestCase.java''' file. Hence, this shows that these two classes are subclasses of the class '''TestCase'''. <pre>public class CascadingErrorTestCase extends TestCase{}</pre> When looking at the testConstructor() in both files, we can see that it implements different methods, therefore its altering the algorithm operation of the file. Therefore we can conclude that the general steps in the constructor is not altered, but the implementation is different.<pre> // From CascadingErrorTestCase.java public void testConstructor() { assertNotNull( new CascadingError( null, null ) ); assertNotNull( new CascadingError( "msg", null ) ); assertNotNull( new CascadingError( "msg", new RuntimeException() ) ); assertNotNull( new CascadingError( null, new RuntimeException() ) );  //assertNotNull( new CascadingError( "msg" ) ); //ambiguous assertNotNull( new CascadingError( null ) ); //assertNotNull( new CascadingError() ); }</pre><pre> // From CascadingExceptionTestCase.java public void testConstructor() { assertNotNull( new CascadingException( null, null ) ); assertNotNull( new CascadingException( "msg", null ) ); assertNotNull( new CascadingException( "msg", new RuntimeException() ) ); assertNotNull( new CascadingException( null, new RuntimeException() ) );  assertNotNull( new CascadingException( "msg" ) ); // ambiguous assertNotNull( new CascadingException( null ) ); //assertNotNull( new CascadingException() ); }</pre> Source from [http://www.google.com/codesearch?hl=en&q=show:tIVs6rnDDGE:QLDR0Cw82Lo&sa=N&ct=rdl&cs_p=https://svn.apache.org/repos/asf/excalibur/trunk&cs_f=framework/api/src/test/org/apache/avalon/framework/test/ |JavacampApache Excalibur Test Framework via Google Code Search] === Apache Tomcat === The following are portions of the code found in [https://svn.apache.org/repos/asf/tomcat/connectors/trunk/jk/java/org/apache/jk/common/Shm.java[Shm.java]] and[https://svn.apache.org/repos/asf/tomcat/connectors/trunk/jk/java/org/apache/jk/common/Shm14.java[Shm14.java]]<br/><br/>Looking at the '''Shm14.java''', we can conclude that '''Shm.java''' is the base class while ''''Shm14.java'''' is the subclass. In addition, ''''Shm.java''' also extends to '''JniHandler''', making it a subclass of that class.<pre>// From Shm14.javapublic class Shm14 extends Shm {}</pre><pre>// From Shm.javapublic class Shm extends JniHandler {}</pre>From the file '''Shm.java''', the following method is given. <pre> public int invoke(Msg msg, MsgContext ep ) throws IOException { if( apr==null ) return 0; log.debug("ChannelShm.invoke: " + ep ); super.nativeDispatch( msg, ep, JK_HANDLE_SHM_DISPATCH, 0 ); return 0; } </pre>In the ''''Shm14.java'''' file, the method from the base class is overridden with the following new one - its implementation changed, but the same structure.<pre> public int invoke(Msg msg, MsgContext ep ) throws IOException { if (log.isDebugEnabled()) log.debug("ChannelShm14.invoke: " + ep );  // return 0; } </pre>Source from [https://svn.apache.org/repos/asf/tomcat/connectors/trunk/jk/java/org/apache/jk/common/ | Apache Tomcat Sample Code via Google Code Search]
== References ==
Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.<br/>== Links ==[http://en.wikipedia.org/wiki/Template_method_pattern| Template Method on Wikipedia]<br/><br/><br/>[[BTP600|BTP600 Wiki Page]]<br/>
1
edit

Navigation menu