Dave
Dave

Reputation: 8461

Template pattern - not useful for small projects

I'm sorry to ask such a localized question but until I get confirmation I don't feel confident moving on with my project.

I have read lots about the template pattern, Wikipedia has a good example.

It shows that you create the basic virtual methods and then inherit the base class and override where you want. The example on the site is for Monopoly and Chess which both inherit the base class.

So, my question is, if you had an application which was only going to be Chess and never anything else, would there be any benefit in using the template pattern (other than as an education exercise)?

Upvotes: 0

Views: 201

Answers (5)

Wouter de Kort
Wouter de Kort

Reputation: 39898

The template pattern is used in specific situations. It is used when you want to sketch out an algorithm but let the specific steps differ.

This could be useful in a Chess application. However, you should not start developing an application with the idea 'I'm going to use this pattern and that one and..'. Instead, you develop the code and you discover that you need certain patterns.

This is where a Test Driven Development approach is really handy. It allows you to refactor your code each step of the way.

A nice book that explains this is Refactoring To Patterns.

Upvotes: 2

Daniel Casserly
Daniel Casserly

Reputation: 3500

It really depends on the parts of the program. The whole idea of Template is to have an algorithm that never changes and to be able to add or edit certain steps of that algorithm.

It may well be that you never change, however, this is the issue with design principles, it IS good practice and you may later wish you'd implemented them. I would say though that if you are 100% sure then you can leave it out as it usually saves time and lines of code. Depends if you want to learn Template usage or not.

Also the GOF principles website is quite good:

Upvotes: 1

O. R. Mapper
O. R. Mapper

Reputation: 20731

No. Expressed in a very simplified and superficial way, the template pattern is just worthwhile starting at a certain relationship between total code size and templated code size. In your example, the chess game is going to be the entire program, so there'll be no need to use the template pattern here.

Upvotes: 2

Micah Armantrout
Micah Armantrout

Reputation: 6971

I would suggest writing your chess game and then if in the future coming back and changing things to fit monopoly too. But its something totally different if you want to use the pattern to learn the pattern, in that case its good to start simple so the complex is easier to understand.

Upvotes: 1

Dan
Dan

Reputation: 13160

No, I think that falls under the category of "You Ain't Gonna Need It."

To be more specific, design patterns exist to solve a particular problem, and if your code doesn't need to solve that problem, all they do is add lines of code without having any benefit.

Upvotes: 9

Related Questions