CSC/ECE 517 Fall 2010/ch7 7f PW: Difference between revisions
No edit summary |
|||
Line 8: | Line 8: | ||
public class TestCase | public class TestCase | ||
public void setup() { | public void setup() { | ||
... | |||
} | } | ||
public class TestCaseOne extends TestCase | public class TestCaseOne extends TestCase | ||
public void setup() { | public void setup() { | ||
super.setup(); | super.setup(); | ||
... | |||
} | } | ||
Line 20: | Line 20: | ||
==Alternative Implementation== | ==Alternative Implementation== | ||
Instead of forcing a method to call super(), it is generally considered a better practice to use the [http://en.wikipedia.org/wiki/Template_method_pattern template method pattern.] In this pattern, the method that would be required to be called via super(), that method would actually call another method that is abstract has to be created by the subclass. | Instead of forcing a method to call super(), it is generally considered a better practice to use the [http://en.wikipedia.org/wiki/Template_method_pattern template method pattern.] In this pattern, the method that would be required to be called via super(), that method would actually call another method that is abstract has to be created by the subclass. | ||
==Example Using Alternative Implementation (Template Method Pattern)== | |||
<pre> | |||
public class TestCase | |||
public void setup() { | |||
..... | |||
doExtraStuff(); | |||
} | |||
abstract void doExtraStuff(); | |||
} | |||
public class TestCaseOne extends TestCase | |||
void doExtraStuff() { | |||
... | |||
} | |||
</pre> | |||
==References== | ==References== | ||
[1] Wikipedia, the free encyclopedia: Call Super, 2010. Wikimedia Foundation, Inc.: [http://en.wikipedia.org/wiki/Call_super] | [1] Wikipedia, the free encyclopedia: Call Super, 2010. Wikimedia Foundation, Inc.: [http://en.wikipedia.org/wiki/Call_super] | ||
[2] MF Bliki: Call Super[http://martinfowler.com/bliki/CallSuper.html] | [2] MF Bliki: Call Super[http://martinfowler.com/bliki/CallSuper.html] |
Revision as of 01:08, 30 November 2010
The Call Super Anti-pattern
What is the Call Super Anti-pattern
The Call Super anti-pattern shows up occasionally in object oriented code. Any time an inherited class overrides a method but is still required to call super() at some point during the method, then that is a case of this anti-pattern. It is not a good idea to require something else being called at some point during the method.
Example of the Call Super Anti-pattern
public class TestCase public void setup() { ... } public class TestCaseOne extends TestCase public void setup() { super.setup(); ... }
Alternative Implementation
Instead of forcing a method to call super(), it is generally considered a better practice to use the template method pattern. In this pattern, the method that would be required to be called via super(), that method would actually call another method that is abstract has to be created by the subclass.
Example Using Alternative Implementation (Template Method Pattern)
public class TestCase public void setup() { ..... doExtraStuff(); } abstract void doExtraStuff(); } public class TestCaseOne extends TestCase void doExtraStuff() { ... }
References
[1] Wikipedia, the free encyclopedia: Call Super, 2010. Wikimedia Foundation, Inc.: [1] [2] MF Bliki: Call Super[2]