2

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. But while using Template Method, you just have to implement those couple of abstract methods.

Can someone please explain to me the (dis?)advantages of Template Pattern and the main difference between Template Pattern and an Abstract Class.

p.s. I just want to know this better for my own understanding/study

EDIT: Thank you for the quick response! But I will explain it a little better (because it is already going the right way :) ). I know what a abstract class is! (and class which methods you have to implement!), and I know the basic idea of a Template Pattern (a kind of a skeleton of an procedural algorithm defined in a class?). I am a little confused because they look-a-like! Also i'm not English, so it's possible that I oversee something! THNX in advance

Johan
  • 74,508
  • 24
  • 191
  • 319
Dominique
  • 439
  • 2
  • 6
  • 15

2 Answers2

2

Like you said, Template is a pattern. Abstract Class is a language specific construct that can be used to implement a template. Some languages (e.g. Python or JavaScript) don't have the notion of Abstract Classes, but that doesn't mean that you can't implement Template in them, just that you have to use a different contract.

One advantage of having abstract classes available in a language is that you can then enforce implementation of required template methods (a contract) at compile time. In a language like JavaScript the contract cannot be enforced and can only be specified through conventions/documentation.

Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
  • so the essential difference is: with template pattern you can/must instantiate the baseclass and you don't have an abstract class but only a couple of abstract methods which can be uniquely implemented in the subclasses? – Dominique Apr 02 '12 at 21:05
  • 1
    There is no essential difference because they fundamentally different concepts that can't be directly compared. It's like asking what the difference between a cab and a Mercedes. There are cabs that use Mercedes, but there are cabs that use Fiats, or even rickshaws, or boats. In Java, Template pattern can be implemented as an abstract class. But it doesn't have to be. For example, many Spring Framework's Template classes are non-abstract classes. But when they are abstract, they have to follow java's rules for abstract classes (ie implementing all methods or keeping 'abstract' keyword). – Dmitry B. Apr 02 '12 at 22:31
  • but can both implementations have some kind of advantages then? I see what you mean! An abstract class can 'force' a template method pattern implementation, right? But it doesn't have to be a Abstract class. The other (logical) difference is, Abstract (class) is an construct and Template an pattern right – Dominique Apr 03 '12 at 07:06
1

An abstract class is a way to implement template pattern. But that is not its only use.

Imagine template pattern as a subset of abstract class. That should clear your doubt.

For the lack of a better word, technically, there is no difference. The difference lies in the way we are using it.

Rishi Goel
  • 670
  • 4
  • 10