Questions tagged [template-method]

Template Method is an Object Oriented design pattern that let's a subclass implement particular steps of an algorithm defined in the superclass.

Template Method is an Object Oriented design pattern that let's a subclass implement particular steps of an algorithm defined in the superclass. Mainly used in frameworks, to allow the code using the frameword to 'plug-in' to an algorithms defined by the framework.

17 questions
12
votes
4 answers

"Factory Method is a specialization of Template Method". How?

Similarities and differences between the two: Template Method Relies on inheritance. Defines the steps of an algorithm, and leaves the task of implementing them to subclasses. Factory Method Relies on inheritance. A superclass defines an…
11
votes
1 answer

Template pattern with varying input type in overridden method

I am trying to use template pattern to define a generic algorithm in java. But the method that needs to be overridden, takes an object as input. This object will vary depending on the concrete implementation. So I decided to use inheritance in input…
Rishi
  • 113
  • 1
  • 6
4
votes
3 answers

Opposite pattern of Template Method

Do we have pattern, which is opposite to Template Method? I mean, in base class we define parts of algorithm and abstract method which implements algorithm. Then in derived class, in that abstract method, we can mix those parts as we want.
Yurii N.
  • 341
  • 3
  • 12
3
votes
5 answers

Design Pattern for interdependent abstract methods

I want to model some mathematical structures. For this purpose I want to define an interface, an abstract class for general purpose algorithms and concrete implementations of that class (I have three in mind). Now the situation arises that the…
2
votes
0 answers

Access modifiers in combination of interpreter pattern with template method pattern

Motivation: (Skip to "The Problem" if you don't need motivation for it) As a project for myself, I'm writing an expression parser for certain kinds of mathematical expressions, and I'm using the interpreter pattern (as it often is used) for my…
2
votes
3 answers

Static (alternative to class based template method pattern for imperative object oriented languages?

EDIT: note I want a static compile time method, when I know exactly what needs to go where at compile time. I often find myself having multiple functions which follow the same pattern, but only a few parts change. Often I opt for the Template…
Krupip
  • 1,252
  • 11
  • 19
2
votes
1 answer

Is the -Impl suffix a legitimate naming convention for a Hook method in Java?

I was going over some old code and found the following peculiar naming convention at a template method implementation. // TEMPLATE METHOD // Checks condition and fail fast if condition is met. // Otherwise call the hook method (to be implemented by…
2
votes
1 answer

Is this considered an implementation of the Template Method design pattern?

Please consider a StringBank class. This class holds a list of Strings and can return them to the client. It's code (irrelevant stuff omitted): abstract class StringBank{ List strings; public List getStrings(){ …
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
1
vote
2 answers

Template Method pattern. Problems extending signature due to a new object type

I have a conceptual question about SW design. I prepared the following example: One abstract base class: internal abstract class BaseFamilyObject { // //... // internal abstract IEnumerable
Jack
  • 21
  • 2
1
vote
3 answers

Constructors vs getters for implementing the templating method with invariant dependencies?

Suppose I'd like to implement the templating pattern, but the only real differences between the subclasses are their choices of some invariant dependencies. Is there a drawback to preferring this style: public abstract class AbstractClass { …
kinbiko
  • 111
  • 3
1
vote
2 answers

Processing and sending processed data to super from child class constructor

I want to do some initialization in child class constructor and pass result to super(). But Java doesn't allow any processing in child class constructor before super() call. Whats a good way to solve this problem?
1
vote
1 answer

Template method pattern - abstract classes vs interface delegation

Template method pattern is commonly implemented with abstract classes. interface Algorithm { void perform(); } abstract class AlgorithmBase implements Algorithm { abstract void step1(); abstract void step2(); private void…
1
vote
1 answer

Virtual method returning a unique collection - how to ensure and hint?

I have a virtual method that returns a collection of items that must be unique. I want to make sure that it will be obvious when overriding the method. What is the best way to do this? Here is an example, where "Two" is being returned twice…
Den
  • 4,827
  • 2
  • 32
  • 48
1
vote
1 answer

Is this the template method pattern?

Could someone tell me whether the following is A) a good solution to the problem of repeating code and B) an example of the template method? Let's say I have one table, named VEHICLES, which I use to store different types of vehicles (e.g. car, van,…
user1578653
  • 329
  • 2
  • 7
0
votes
2 answers

Does Template pattern violate Single Responsibility principle?

Assume we have the following class: class Foo { public: void func() { _func1(); _func2(); } private: virtual void _func1(); virtual void _func2(); }; This class, from one side, specifies the interface (foo method), and,…
undermind
  • 117
  • 1
1
2