CSC/ECE 517 Fall 2010/ch7 7f PW: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
=The Call Super Anti-pattern=
=The Call Super Anti-pattern=
==What is the Call Super Anti-pattern==
==What is the Call Super Anti-pattern==
The Call Super [http://en.wikipedia.org/wiki/Anti-pattern anti-pattern] shows up occasionally in object oriented code.  An anti-pattern may be commonly used, but is often considered bad practice or is not effective at producing the desired results.  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.  <br>
The Call Super [http://en.wikipedia.org/wiki/Anti-pattern anti-pattern] shows up occasionally in object oriented code.  An anti-pattern may be commonly used, but is often considered bad practice or is not effective at producing the desired results.  The call super anti-pattern occurs any time an inherited class overrides a method but is still required to call super() at some point during the method.  It is not a good idea to require something else being called at some point during the method because the programmer could forget to make the call or do something incorrectly.  The reason this anti-pattern is seen a lot in programming is because many languages don't have a way to force super being called from the subclass.  One language does however have this feature and that is [http://en.wikipedia.org/wiki/BETA BETA.] <br>
Diagram of Call Super Anti-patter<br>
[[Image:Callsuper.png|Call Super Anti-pattern]]<br>
[[Image:Callsuper.png|Call Super Anti-pattern]]<br>


===Example of the Call Super Anti-pattern===
===Example of the Call Super Anti-pattern===
In the example below, the super class of TestCase is extended by the TestCaseOne class.  The subclass TestCaseOne calls setup at the beginning of its own setup() method and then does any extra steps.
<pre>
<pre>
public class TestCase
public class TestCase
Line 22: Line 24:


==Example Using Alternative Implementation (Template Method Pattern)==
==Example Using Alternative Implementation (Template Method Pattern)==
In the example below, the super class implementation of setup() calls another method doExtraStuff() which is abstract and therefore has to be implemented by the subclass when it is extended.
<pre>
<pre>
public class TestCase
public class TestCase

Revision as of 01:42, 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. An anti-pattern may be commonly used, but is often considered bad practice or is not effective at producing the desired results. The call super anti-pattern occurs any time an inherited class overrides a method but is still required to call super() at some point during the method. It is not a good idea to require something else being called at some point during the method because the programmer could forget to make the call or do something incorrectly. The reason this anti-pattern is seen a lot in programming is because many languages don't have a way to force super being called from the subclass. One language does however have this feature and that is BETA.
Diagram of Call Super Anti-patter
Call Super Anti-pattern

Example of the Call Super Anti-pattern

In the example below, the super class of TestCase is extended by the TestCaseOne class. The subclass TestCaseOne calls setup at the beginning of its own setup() method and then does any extra steps.

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)

In the example below, the super class implementation of setup() calls another method doExtraStuff() which is abstract and therefore has to be implemented by the subclass when it is extended.

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]