Real World Example
I will start with a real world example.
Let's say that you are developing an application that consists of several roles, for example Administrator, Visitor and Power User.
Each role has its own set of permissions that allows it to perform deferent operations in the application. For example the Administrator can navigate to the admin page while other roles cannot.
You have to create each role with the specified set of operations that it allowed to perform.
Template Method
Here is the definition from the Go4:
"Template Method defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses".
So, if we get back to the real world example, we have all the roles as motioned before and a set of operations. Each role should create a set of operations specific to its permissions.
In order to achieve that first we need those three roles and an abstract class named role that they all inherits from.
Each Role must implement the GetOperation method and by doing so each one is responsible for instantiating deferent set of operations related to it.
Here is a Class Diagram that describes it:
So, the code will look as follows:
public abstract class Role
{
public string Name { get; set; }
public Role(string name)
{
Name = name;
}
/// <summary>
/// This is the template method.
/// </summary>
/// <returns></returns>
public abstract List<Operation> GetOperations();
}public class AdminRole : Role
{
public AdminRole()
: base("Administrator")
{
}
public override List<Operation> GetOperations()
{
List<Operation> operations = new List<Operation>();
operations.Add(new Operation("GoToAdminPage"));
operations.Add(new Operation("GoToMainPage"));
operations.Add(new Operation("SeeUserInformation"));
//and the list goes on...
return operations;
}
}public class PowerUserRole : Role
{
public PowerUserRole()
: base("PowerUser")
{
}
public override List<Operation> GetOperations()
{
List<Operation> operations = new List<Operation>();
operations.Add(new Operation("EditUserInfo"));
operations.Add(new Operation("RemoveUsers"));
operations.Add(new Operation("AddNewUsers"));
//and the list goes on...
return operations;
}
}public class VisitorRole : Role
{
public VisitorRole()
: base("Visitor")
{
}
public override List<Operation> GetOperations()
{
List<Operation> operations = new List<Operation>();
operations.Add(new Operation("GoToVisitorPage"));
operations.Add(new Operation("SeeAboutPage"));
//and the list goes on...
return operations;
}
}public class Operation
{
public string Name { get; set; }
public Operation(string name)
{
Name = name;
}
}
You can download the code from here.
Summary
Template Method is a very common design pattern.
Whenever I consider using it I ask myself the following questions:
- Are there several objects that inherit from a base object?
- Do they have a common operation for creating something?
- It the objects they all create derive from a common object?
If the answer to all those questions is yes, than my scenario is a good candidate for using Template Factory.