Difference between revisions of "Template Method"
(→Code Samples) |
(→Code Samples) |
||
Line 6: | Line 6: | ||
== UML Diagram == | == UML Diagram == | ||
− | == Code | + | == Pseudo Code == |
− | |||
− | |||
<pre> | <pre> | ||
− | abstract class | + | abstract class AbstractRenderer |
− | + | { | |
− | + | // Define the order in which the graphical routines should be executed | |
− | + | void render() | |
− | + | { | |
− | + | // First draw the graphics | |
− | + | drawGraphics(); | |
− | + | ||
− | + | // Then draw the GUI on top of the graphics | |
− | + | drawGUI(); | |
− | + | } | |
− | + | ||
− | + | void drawGraphics(); | |
− | + | void drawGUI(); | |
− | |||
− | |||
} | } | ||
− | + | ||
− | class | + | class Renderer extends AbstractRenderer |
− | + | { | |
− | + | void drawGraphics() | |
− | + | { | |
− | + | // Draw the graphics here | |
− | + | } | |
− | + | ||
− | + | void drawGUI() | |
− | + | { | |
− | + | // Draw the graphical user interface here | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
</pre> | </pre> |
Revision as of 16:12, 19 March 2007
Template Method
Template Method, a class behavioral pattern, provides the general steps of a method while deferring the implementation to its subclasses. Used to encapsulate algorithms, it can help reduce code duplication and maximizes the reuse of subclasses. Generally, an abstract base class is created, defining a template method of an algorithm. Later on, the subclasses can alter and implement the behavior.
UML Diagram
Pseudo Code
abstract class AbstractRenderer { // Define the order in which the graphical routines should be executed void render() { // First draw the graphics drawGraphics(); // Then draw the GUI on top of the graphics drawGUI(); } void drawGraphics(); void drawGUI(); } class Renderer extends AbstractRenderer { void drawGraphics() { // Draw the graphics here } void drawGUI() { // Draw the graphical user interface here } }