About two days ago I asked a question about the differences between these two patterns. after reading about the template method patterns in detail I found that although there are lots of similarities between them layer supertype is not the same as method template and vice versa. I've blogged about Layer Supertype before in here, now I want to talk about method template pattern. Basically, the template method is to run common functionality of a base class from the child class and use the objects of the child type in the common method functionality of the base type. It means that the best way of implementing the pattern is to create a base abstract class with abstract methods to implement the implementer of the base abstract class members. Take a look this sample in C#:
public abstract class Order { protected abstract bool CheckAccountAmount(); protected abstract bool CheckProductAvailibility(); public bool MakeOrder() { if (CheckAccountAmount()) { if (CheckProductAvailibility()) { //Do Order process } else { return false; } return false; } return false; } } public class Customer : Order { protected override bool CheckAccountAmount() { // recieve the information from credit card servers return true; } protected override bool CheckProductAvailibility() { // go to the database to see if the product is available return true; } } public class Member : Order { protected override bool CheckAccountAmount() { // go to the database to check if the members has enough account in his/her account return true; } protected override bool CheckProductAvailibility() { // // go to the database to see if the product is available return true; } } public class Presentation { public void Operatio() { //call customer methods for order Order customer = new Customer(); customer.MakeOrder(); //call member methods for order Order member = new Member(); member.MakeOrder(); } }
As you can see, there is some common operation in both Member and Customer, I've created the MakeOrder method in the base class to be available for member and Customer. Making Order is a common task for Member and Customer on creating order and each of them can use it for its own order process. Making order is just for making an order and works only for it! I mean that It's a common task for making an order in the system but what do you think about creating a basket for customers and members? Can you use the same method (MakeOrder)? you should create another base class for it yeah?
the main goal of this blog post is to define the differences between template method pattern and layer supertype. If you have a look at my post about the layer supertype you can see that there are more similarities between them. Both of them reduce the duplication and reimplementation of code. I agree with Adriano comment on my question, it's better to talk about the similarities than differences of these patterns. But I think that template method patterns works on the same implementation of subtypes and redefine certain steps of an algorithm without changing the Algorithm's Structure. And it's mostly about sequential and common processes but layer supertype works on similarities of some operation on types and it's A type that acts as the supertype for all types in its layer.
Category: Software
Tags: Design Pattern