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
0
votes
2 answers

Adding invariants in non virtual interface idiom

Suppose I have the following hierarchy using the NVI idiom : class Base { public: virtual ~Base() {} void foo() { cout << "Base::foo" << endl; foo_impl(); } private: virtual void foo_impl() = 0; }; class A : public…
0
votes
1 answer

Template method pattern with setTimeout in JavaScript

I have a close function that will close some instance. The class that includes the function allows derived classes to override close. Here, I want to make sure that close always calls dispose even in derived classes. I achieve this by the…
CookieEater
  • 2,237
  • 3
  • 29
  • 54
0
votes
2 answers

Template method pattern mess

Here the hook functions themselves need to check the typeid of a data member that also belongs to hierarchy of classes. So I define a template method for that hierarchy of classes. This is the mess I run into: void Person::leave() { // code …
prestokeys
  • 4,817
  • 3
  • 20
  • 43
0
votes
3 answers

Does my example for using the Strategy design pattern with the Template Method design pattern make sense?

For some homework, we have to devise an example (with classes) where both the Strategy and Template Method design patterns come together to complement each other and make them more customizable as a result. Or "use template method to provide more…
0
votes
3 answers

Manipulating properties in init that are set up by a sub-class in Objective-C

I have an abstract interface in Objective-C where every sub-class needs to set up a property and then do the exact same thing with that property at the end of init. I'm trying to avoid duplicated code with something like this: Interface…
godel9
  • 7,340
  • 1
  • 33
  • 53
0
votes
1 answer

generic parsing of json to java objects

i have some java classes like MyClass1 and MyClass2 whose state is saved in json objects: "MyClass1":[{"var1":"value1","var2":"value2"},{"var1":"value3","var2":"value4"}] "MyClass2":{"var11":"value5","var22":"value6"} Now I want to write a generic…
user1414745
  • 1,317
  • 6
  • 25
  • 45
0
votes
4 answers

C# Refactoring the same action with different details using design patterns

I try to find the way for refactoring my code but no idea how to do this. For example, we have several classes class A { string name; int year; } class B { long id; string code; DateTime currentTime; } class C { …
Ray
  • 1,788
  • 7
  • 55
  • 92
0
votes
2 answers

Concrete method in super class-abstract class

I have an abstract class Shape, some sub classes and some methods to calculate area, perimeter and to draw the shape that are overridden. I'm trying to find a Template Method in the abstract class for this application but I can't think of any. I…
0
votes
3 answers

OOP - 'Default' bad class name?

I am implementing a template method type of pattern and have several classes to implement the behaviour. As an example, my structure is as follows: TemplateAbstract Type CustomType1 CustomType2 CustomType3 …
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
0
votes
3 answers

Template method need an object declared in the child constructor

a child class extending a mother class which, in its constructor call a template method instianted in the child class because it need a value obtained in the child constructor. How Can I do something like this, without changing my Parent constructor…
Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72
0
votes
1 answer

Is it a good practice to implement Template Method Pattern via C# events?

I am now trying to understand some code and I have found a pattern, which seem a bit strange to me. There is a user's control class with 'EditorOpen' event. At first, I thought this name is incorrect, because it does not end with '-ing' or '-ed', as…
Michal Czardybon
  • 2,795
  • 4
  • 28
  • 40
-1
votes
1 answer

How can I loop through a List using the template-method pattern?

I'm looking for an example implementation of the template-method pattern in Java. Suppose, for example, I'd like to create a generic class that can loop through a List and execute a template method with signature void execute(T t) on each item…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
-1
votes
1 answer

Add functionalities to List in Java

I need to add the following functionalities over any implementation of List<>: - the resulted object should never show the OutOfBounds Exception for positive indexes - when you call get(x), with x=size, it will automatically increase the list up…
Scartz
  • 513
  • 1
  • 4
  • 5
-1
votes
1 answer

Stateless Template method implementation

Let's say I have a Strategy interface : public interface Strategy { void perform(); } And a template method to implement it : public abstract class AbstractStrategy implements Strategy { @Override public void perform() { …
bowmore
  • 10,842
  • 1
  • 35
  • 43
-2
votes
1 answer

what are the key strategies of template method pattern

In an interview i was asked about the key strategies of template method pattern. I answered Inheritance.Is this the right answer? If anyone can direct me in the right direction? Thanks in advance.
1 2 3 4 5 6 7
8