Changes

Jump to: navigation, search

Template Method

92 bytes added, 19:56, 19 March 2007
Apache Tomcat
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 Application 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>
/*
* Copyright 1997-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avalon.framework.component.test;
import junit.framework.TestCase;import org.apache.avalon.framework.component.Component;import package org.apache.avalon.framework.component.ComponentException;import org.apache.avalon.framework.component.DefaultComponentManagertest;
/**import org.apache.avalon.framework.CascadingError; * Test the basic public methods of DefaultComponentManagerimport org.apache. * * @author <a href="mailto:dev@avalon.apacheframework.org">Avalon Development Team</a> */public final class DefaultComponentManagerTestCase extends TestCase{CascadingThrowable;
class DefaultRoleA implements Component,RoleA { public DefaultRoleA() { } }import junit.framework.TestCase;
class DefaultRoleB implements Component,RoleB { public DefaultRoleB() { } }</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'''.
private DefaultComponentManager m_componentManager;<pre>public class CascadingErrorTestCase extends TestCase{}</pre>
protected boolean m_exceptionThrown;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 DefaultComponentManagerTestCasevoid testConstructor()
{
thisassertNotNull( new CascadingError( null, null ) ); assertNotNull( new CascadingError("DefaultComponentManager Test Casemsg", null ) ); } assertNotNull( new CascadingError( "msg", new RuntimeException() ) ); assertNotNull( new CascadingError( null, new RuntimeException() ) );
public DefaultComponentManagerTestCase //assertNotNull( final String name new CascadingError( "msg" )); { //ambiguous assertNotNull( new CascadingError( null ) ); super//assertNotNull( name new CascadingError() );
}
</pre><pre> // From CascadingExceptionTestCase.java protected public void setUptestConstructor() throws Exception
{
m_componentManager = assertNotNull( new DefaultComponentManagerCascadingException(null, null ) ); m_exceptionThrown = falseassertNotNull( new CascadingException( "msg", null ) ); } assertNotNull( new CascadingException( "msg", new RuntimeException() ) ); assertNotNull( new CascadingException( null, new RuntimeException() ) );
protected void tearDown assertNotNull( new CascadingException("msg" ) ); throws Exception {// ambiguous assertNotNull( new CascadingException( null ) ); m_componentManager = null//assertNotNull( new CascadingException() );
}
</pre>
Source from [http://www.google.com/** * lookup contractcodesearch?hl=en&q=show:tIVs6rnDDGE: * return first component found for role * search in hirarchy from current componentManager upQLDR0Cw82Lo&sa=N&ct=rdl&cs_p=https://svn.apache. * if no compnent exist for role a in hierarchy * throw ComponentException *org/repos/asf/excalibur/trunk&cs_f=framework/api/src/test/org/apache/avalon/framework/test/| Apache 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 void testlookup1 int invoke(Msg msg, MsgContext ep ) throws ExceptionIOException
{
DefaultRoleB roleBinBase = new DefaultRoleBif(); DefaultRoleB roleBinParent apr= new DefaultRoleB(); DefaultRoleA roleAinParent = new DefaultRoleA(null )return 0m_componentManagerlog.putdebug(RoleA"ChannelShm.ROLE,roleAinParentinvoke: " + ep ); m_componentManagersuper.putnativeDispatch(RoleB.ROLEmsg, ep, JK_HANDLE_SHM_DISPATCH,roleBinParent); DefaultComponentManager baseComponentManager = new DefaultComponentManager(m_componentManager0 ); baseComponentManager.put(RoleB.ROLE,roleBinBase)return 0; Object lookupAinBase = baseComponentManager.lookup(RoleA.ROLE); } Object lookupBinBase = baseComponentManager.lookup(RoleB.ROLE);</pre> Object lookupBinParent = m_componentManager.lookup(RoleBIn the ''''Shm14.ROLE); assertTrue( lookupAinBase instanceof RoleA); assertEquals( lookupBinBasejava'''' file, roleBinBase ); assertEquals( lookupBinParentthe method from the base class is overridden with the following new one - its implementation changed, roleBinParent ); assertEquals( lookupAinBase,roleAinParent); }but the same structure.<pre> public void testlookup2 int invoke(Msg msg, MsgContext ep ) throws ExceptionIOException
{
m_componentManager.putif (RoleAlog.ROLE,new DefaultRoleAisDebugEnabled()); Object o = null; try { o = m_componentManagerlog.lookupdebug(RoleB.ROLE); } catch (ComponentException ce) { m_exceptionThrown = true; } if (o == null) assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown ); else assertTrue("component was found by lookup ,when there was no componentChannelShm14.invoke: ",false + ep );  }
public void testhasComponent() throws Exception { m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); assertTrue(m_componentManager.hasComponent(RoleA.ROLE)); assertTrue(!m_componentManager.hasComponent(RoleB.ROLE)); }  public void testmakeReadOnly() throws Exception { //before read only m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); Object a = m_componentManager.lookup(RoleA.ROLE); assertTrue( a instanceof RoleA); m_componentManager.makeReadOnly(); //after read only try { m_componentManager.put(RoleB.ROLE,new DefaultRoleB()); } catch (IllegalStateException se) { m_exceptionThrown = true; } assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown )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/>
[http://en.wikipedia.org/wiki/Template_method_pattern| Template Method on Wikipedia]<br/><br/><br/>
[[BTP600|BTP600 Wiki Page]]<br/>
[http://en.wikipedia.org/wiki/Template_method_pattern| Template Method on Wikipedia]<br/>
1
edit

Navigation menu