Click here to Skip to main content
Click here to Skip to main content

Template Method Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Template Method Design Pattern clearly explained with an example.

You can see this and other great articles on Design Patterns here.

The Template Method Design Pattern allows you to declare a general logic in the parent class so that all the child classes can use the general logic. To give an example, the general way of building a car is to build the skeleton first, followed by the installation of the engine, and finally the installation of the doors. This general sequence of building the car is the template method, since you will always need to use this logic no matter what type of cars you are building.

The key to the Template Method pattern is that we put the general logic in the abstract parent class, and let the child class define the specifics. Let’s look at the UML of the Template Method pattern, then we will look at an example to see how it works. The UML of the Template Method pattern is shown below:

  • The Template class is the abstract parent class that contains the general logic.
    • It has the Operation1, Operation2, and Operation3 methods, which are method stubs for the child classes to implement.
      • It has the TemplateMethod method, which contains the general logic. In the diagram, it shows that the general logic calls Operation1, followed by Operation2, and finally Operation3.
    • The Implementation class is the child class of the Template class. You will have multiple Implementation classes where each has its own specific implementation.
      • It has the Operation1, Operation2, and Operation3 methods that contain the actual implementation of the methods.

Let's see an example. In the example, we will be building two types of cars. One is a Porsche, the other is a VW Beetle. Below is the UML for the example:

  • The CarBuilder class is the parent abstract class that contains the general logic.
    • It has the BuildCar method, this is the template method that contains the general logic, which specifies that it must build the skeleton of the car first, followed by the installation of the engine, and finally the installation of the doors.
      • It has the BuildSkeleton, InstallEngine, and InstallDoor method stubs for the child classes to implement.
    • The PorcheBuilder and BeetleBuilder classes are the child classes of the CarBuilder. Each of the child classes will specify how to build their own skeleton, the engine, and the doors.
      • It has the BuildSkeleton, InstallEngine, and InstallDoor methods that contain the actual implementation of the methods.

With the Template Method pattern in place, the client code (calling code) can just be:

CarBuilder c = new BeetleBuilder();
c.BuildCar();

And it will build a Beetle with the specifications of a Beetle.

 

Below are the implementation code and the output of our example. Notice that we can build either a Porsche or a Beetle by reusing the general logic in the parent class and providing only the specifics in the child classes:

class Program
{
    static void Main(string[] args)
    {
        CarBuilder c = new PorcheBuilder();
        c.BuildCar();

        c = new BeetleBuilder();
        c.BuildCar();
    }
}

public abstract class CarBuilder
{
    protected abstract void BuildSkeleton();
    protected abstract void InstallEngine();
    protected abstract void InstallDoor();

    //Template Method that specifies the general logic
    public void BuildCar()
    {
        BuildSkeleton();
        InstallEngine();
        InstallDoor();
    }
}

public class PorcheBuilder : CarBuilder
{
    protected override void BuildSkeleton()
    {
        Console.WriteLine("Building Porche Skeleton");
    }

    protected override void InstallEngine()
    {
        Console.WriteLine("Installing Porche Engine");
    }

    protected override void InstallDoor()
    {
        Console.WriteLine("Installing Porche Door");
    }
}

public class BeetleBuilder : CarBuilder
{
    protected override void BuildSkeleton()
    {
        Console.WriteLine("Building Beetle Skeleton");
    }

    protected override void InstallEngine()
    {
        Console.WriteLine("Installing Beetle Engine");
    }

    protected override void InstallDoor()
    {
        Console.WriteLine("Installing Beetle Door");
    }
}

Liked this article? You can see this and other great articles on Design Patterns here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembersaitmemis11-Nov-11 1:09 
GeneralGood Article PinmemberImtiaz.Ahmed28-May-11 9:20 
GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 7:52 
GeneralRe: My vote of 1 Pinmembersobo12310-Nov-13 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid