Questions tagged [template-method-pattern]

The Template Method pattern is a design pattern that defines the program skeleton of an algorithm in a method, called a template method, which defers some steps to subclasses. It is one of the Gang of Four's behavioral design patterns.

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

More information is available on Wikipedia.

120 questions
7
votes
2 answers

Template method pattern without inheritance

How can a variant of the Template Method pattern be implemented whereby the concrete class does not inherit from the base class, but the overall feature of the pattern is maintained. The reason it cannot inherit is that it's forced to inherit from…
Herman Schoenfeld
  • 8,464
  • 4
  • 38
  • 49
6
votes
3 answers

Elegant way to implement template method pattern in Golang

Is there an elegant canonical way to implement template method pattern in Go? In C++ this looks like this: #include #include class Runner { public: void Start() { // some prepare stuff... Run(); …
user6256186
  • 103
  • 1
  • 5
6
votes
2 answers

Inline a virtual function in a method when the object has value semantics

Consider the following code with a template method design pattern: class A { public: void templateMethod() { doSomething(); } private: virtual void doSomething() { std::cout << “42\n”; …
6
votes
3 answers

Template Method pattern for static classes

I have a util class that perform some work. Obviously, it is closed for extension and all methods are static. For the sake of simplicity, the class looks like this: public final class Util { private Util() { } public static void doWork() { …
mishadoff
  • 10,719
  • 2
  • 33
  • 55
6
votes
3 answers

Implementing the Template Method pattern in C#

The template method pattern provides that the abstract base class has a not overridable method: this method implements the common algorithm and should not overridden in the subclasses. In Java the template method is declared final within the…
enzom83
  • 8,080
  • 10
  • 68
  • 114
5
votes
3 answers

Template method pattern with implementation specific parameter type

I often get into situation when I'd like to use template method pattern, but the template method expects a different type of a parameter, like this: public abstract class AbstractFoo { public void process(TypeA a, TypeB b) { //do common…
lisak
  • 21,611
  • 40
  • 152
  • 243
5
votes
2 answers

Is Factory method pattern a specialized case of Template method pattern?

GOF talks about frameworks for "Factory method" pattern. Frameworks need objects but implementation of objects depends upon application hence an abstract method to create the object is created. Also as return type is needed so interface for the…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
5
votes
1 answer

Template method in javascript

I want, in javascript, to implement the template method pattern. I have a PropertyDecorator with some subclasses: OpenButtonDecorator, SeeButtonDecorator and so on. I want to have in Property decorator the next function: var build = function(){ …
pablorc
  • 940
  • 1
  • 8
  • 20
5
votes
3 answers

Why is JdbcTemplate an example of the Template method design pattern

I was reading about design patterns, in particular about the template method, when my attention was caught by this question on SO. After reading the explanation and specific code I am still wondering why this is an example of the 'Template method'…
5
votes
1 answer

Is it possible to use the template method pattern in the constructor?

Possible Duplicate: Calling virtual functions inside constructors I have a class Shape and its subclass Sphere : //Shape : class Shape { public: Shape(const string& name); virtual ~Shape(); virtual string…
codablank1
  • 6,055
  • 5
  • 19
  • 29
5
votes
3 answers

What is the use of Template Method in Base Classes?

Well, I was going through this excellent article on MSDN about "Base Class Usage". While I understand the concept of base class and interfaces, I am unable to comprehend the usage of Template methods in the second paragraph of this article…
Aakash
  • 695
  • 3
  • 10
  • 25
5
votes
4 answers

What are the similarities between the Template Method and Strategy design patterns

is this an example of TemplateMethod Pattern?? public abstract class Character{ public final void useWeapon(){ useBusterSword(); useMateriaBlade(); useUltimateWeapon(); } public abstract void useBusterSword(); …
4
votes
2 answers

Implementing templated template method

Note: The following question is about the Template Method Design Pattern and C++ function templates. To distinguish both, I will use italics when referring to the design pattern and bold when referring to C++ templates. The idea of the template…
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
4
votes
4 answers

Is it possible to have a method which derived classes cannot call, but consumers of a class can call?

I'm implementing the template method pattern. There are a couple of settings that clients can change on this template method; but I would like to prevent implementers of the template method from changing these settings, because in such cases they…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
4
votes
2 answers

How to combine template method pattern and multiple inheritance?

Can I change this code to make it work? Is it possible to combine template method pattern and multiple inheritance? It seems to be very convenient to implement different algorithms in different classes. Thank you. class TBase { public: virtual…
typedef
  • 1,800
  • 3
  • 11
  • 19