Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top