Ehsan Ghanbari

Experience, DotNet, Solutions

Difference between layer super type and template method patterns

About two days ago I asked a question about the differences of these two patterns. after reading about the template method patterns in detail I found that although there are lots of similarities between them but layer super type is not the same as method template and vice versa. I've blogged about Layer Super type before in here, now I want to talk about method template pattern. Basically 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#:

  1.  public abstract class Order
  2.     {
  3.         protected abstract bool CheckAccountAmount();
  4.         protected abstract bool CheckProductAvailibility();
  5.  
  6.         public bool MakeOrder()
  7.         {
  8.             if (CheckAccountAmount())
  9.             {
  10.                 if (CheckProductAvailibility())
  11.                 {
  12.                     //Do Order process
  13.                 }
  14.                 else
  15.                 {
  16.                     return false;
  17.                 }
  18.                 return false;
  19.             }
  20.             return false;
  21.         }
  22.     }
  23.  
  24.  
  25.     public class Customer : Order
  26.     {
  27.         protected override bool CheckAccountAmount()
  28.         {
  29.             // recieve the information from credit card servers
  30.             return true;
  31.         }
  32.  
  33.         protected override bool CheckProductAvailibility()
  34.         {
  35.             // go to the database to see if the product is available
  36.             return true;
  37.         }
  38.     }
  39.  
  40.  
  41.     public class Member : Order
  42.     {
  43.         protected override bool CheckAccountAmount()
  44.         {
  45.             // go to the database to check if the members has enough account in his/her account
  46.             return true;
  47.         }
  48.  
  49.         protected override bool CheckProductAvailibility()
  50.         {
  51.             //  // go to the database to see if the product is available
  52.             return true;
  53.         }
  54.     }
  55.  
  56.  
  57.     public class Presentation
  58.     {
  59.         public void Operatio()
  60.         {
  61.             //call customer methods for order
  62.             Order customer = new Customer();
  63.             customer.MakeOrder();
  64.  
  65.             //call member methods for order
  66.             Order member = new Member();
  67.             member.MakeOrder();
  68.         }
  69.     }
  70.  

 

As you can see, there are some common operation in both Member and Customer, I've created the MakeOrder method in 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 order and works only for it! I mean that It's a common task for making order in the system but what do you think about creating 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 of template method pattern and layer super type. If you have a look on my post about the layer super type you can see that there are more similarities between them. Both of them reduce the duplication and reimplementation of code. I'm 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 super type works on similarities of some operation on types and it's A type that acts as the supertype for all types in its layer.

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus