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
3
votes
5 answers

How to change method defaults in Python?

I'm building a class, Child, that inherits from another class, Parent. The Parent class has a loadPage method that the Child will use, except that the Child will need to run its own code near the end of the loadPage function but before the final…
Tanner Semerad
  • 12,472
  • 12
  • 40
  • 49
3
votes
3 answers

What is the "template method pattern" in cocoa with Object C ? ( Language comparison thinking )

Here is template method pattern , Java and C++ can implement it easily with virtual function. How about Object C to implement this pattern ? Any example in cocoa touch (iOS) ?
3
votes
2 answers

Template design pattern in JDK, could not find a method defining set of methods to be executed in order

I am reading about Template design pattern. As per my current understanding, Template design pattern can be used when we have an algorithm with defined set of processes(methods) to be done in order. Main players are 1.Abstract Template class…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
3
votes
1 answer

How to "hide" constructor when using a factory?

I am faced with a situation, where I would like to call a virtual method from the constructor. This is of course not possible (or at least it does not produce the desired behavior). In this answer as a work-around it is proposed to use a factory…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
3
votes
3 answers

template method pattern

May i know how to create childClass if my childClass method getInfoFromDB() and saveToDB() need to do different logic? public abstract class BaseClass { public abstract Object doTransaction(); public Object executeTrans() { //do…
cometta
  • 35,071
  • 77
  • 215
  • 324
3
votes
1 answer

Template method pattern - prevent direct method calls in derived classes

I'm not sure if I understand template method pattern correctly. Here is my simplified base class implementation: public abstract class AlgorithmBase { protected void AlgorithmMethod() { if(!OnSimulationStart()) { …
2
votes
2 answers

Difference between Template Method Pattern and using Abstract (base) classes?

After a several hour (re)search, I just can't come up with a explainable difference between a normal Abstract class and the use of a Template Pattern. The only thing I see is: while using an Abstract Class you are required to implement all methods.…
Dominique
  • 439
  • 2
  • 6
  • 15
2
votes
3 answers

Singleton abstract class in Kotlin

I am implementing a library following a template method design pattern. It involves creating costly IO connection. To avoid any resource leaks, i want to enforce singleton instance on abstract class level. client just need to override the method…
Rajeev
  • 4,762
  • 8
  • 41
  • 63
2
votes
2 answers

Template method and inheritance or composition

I have these classes: @Data @AllArgsConstructor @NoArgsConstructor public class User { private String name; private int age; } @Data @AllArgsConstructor @NoArgsConstructor public class Admin { private String name; private int…
ip696
  • 6,574
  • 12
  • 65
  • 128
2
votes
0 answers

How to replace template method pattern with functional style?

You can see the code here The concrete problem that I'm trying to solve is this. Say that I need to provide REST interface to some entities modeled (sqlalchemy in my case) with some tool stored in database. Say that this collection is called…
user1685095
  • 5,787
  • 9
  • 51
  • 100
2
votes
2 answers

When to use template method pattern

Hi I have a problem where i have to perform similar steps / actions (which differ slightly) on different sets of data (the data can be slightly different also with regards to its strucure). There are a few steps that i need to perform: connect,…
Bill
  • 141
  • 3
2
votes
1 answer

Can there be more than one template method in an implementation of the Template Method pattern?

Can an abstract class in an implementation of the Template Method pattern have more than one template method?
JUM
  • 25
  • 3
2
votes
3 answers

Template method pattern where each implementation requires different arguments?

I have a base abstract class that needs an algorithm for Authentication. I have two implementations of this, one will hash a password and compare it to a stored hash, the other will use windows active directory. But before actually doing the hash…
Didier A.
  • 4,609
  • 2
  • 43
  • 45
2
votes
3 answers

Understanding Template method pattern

From what I understand, Template method is nothing but ordinary method that calls virtual or abstract methods defined in child class. Am I right, or is there something else important about this pattern that I miss? abstract class Foo { public void…
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
2
votes
1 answer

Refactoring class design to convey the design intention

I have following class design. The complete code is available in " How to achieve this functionality using Generics? ". The code works fine and resolves the casting issue mentioned in " Refactoring Code to avoid Type Casting " In the…
LCJ
  • 22,196
  • 67
  • 260
  • 418