Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, the template pattern is a bit more than just overriding a method in the base class.</p> <p>Template pattern can be used when an outline of an algorithm is concretely defined, however the steps of the algorithm are left abstract. That means that the steps can be implemented in different ways. But, the general outline of the algorithm is not expected to change.</p> <p>An example that I have just created on the fly:</p> <pre><code>class Life { public method goThroughTheDay(){ goToWork(); eatLunch(); comeBackHome(); programABitMore(); } abstract method goToWork(); abstract method eatLunch(); abstract method comeBackHome(); abstract method programABitMore(); } class GoodLife extends Life { //override all the abstract methods here } //The client application Life life = new GoodLife(); life.goThroughTheDay(); </code></pre> <p>Basically, the way a day is expected to run down is concretely defined in the Life class. However, the details of the process are taken care by the subclass (ie. GoodLife). GoodLife class will implement steps very differently than a possible ToughLife class. </p> <p>There are some variations to this pattern; for example some of the steps can also be concretely defined. In the example, the eatLunch() can be concretely defined in the Life class; meaning that the subclasses are not expected to change this behaviour.</p> <p>The pattern makes a lot of sense if you have a relatively complex algorithm that could be implemented in different ways.</p> <p><strong>======================================</strong></p> <p>I somehow missed the part with Objective-C in my answer. Here is how it would look in Objective-C:</p> <pre><code>@interface Life : NSObject - (void) goThroughTheDay; - (void) goToWork; // Abstract - (void) eatLunch; // Abstract - (void) comeBackHome; // Abstract - (void) programABitMore; // Abstract @end @implementation Life - (void) goThroughTheDay { [self goToWork]; [self eatLunch]; [self comeBackHome]; [self programABitMore]; } - (void) goToWork { [self doesNotRecognizeSelector:_cmd]; } - (void) eatLunch { [self doesNotRecognizeSelector:_cmd]; } - (void) comeBackHome { [self doesNotRecognizeSelector:_cmd]; } - (void) programABitMore { [self doesNotRecognizeSelector:_cmd]; } @end @interface GoodLife : Life @end @implementation GoodLife - (void) goToWork { NSLog(@"Good Work"); } - (void) eatLunch { NSLog(@"Good Lunch"); } - (void) comeBackHome { NSLog(@"Good Comeback"); } - (void) programABitMore { NSLog(@"Good Programming"); } @end </code></pre> <p>Objective-C doesn't have built-in support for abstract classes, so I worked around it using the <code>doesNotRecognizeSelector:</code> method. A lot more details about the abstract classes &amp; Objective-C can be found <a href="https://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload