Template Pattern

  • Behavioral pattern
  • The intent of this pattern is to define the skeleton of an algorithm in an operation , deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm structure.
  • In Template pattern – an abstract class defines way(s)/template(s) to execute its methods. Its subclasses can ovveride the method implementation as per need but the invocation is to be in the same way as defined by an abstract class
				
					namespace templatepattern
{
    public abstract class Game
    {
        public abstract void TicketBooking();
        public abstract void Toss();
        public abstract void GameStart();

        public void Initialize()
        {
            TicketBooking();
            Toss();
            GameStart();
        }
    }

    public class Cricket : Game
    {
        public override void GameStart()
        {
            Console.WriteLine("Cricket Game Started");
        }

        public override void TicketBooking()
        {
            Console.WriteLine("TicketBooking started");
        }

        public override void Toss()
        {
            Console.WriteLine("Cricket Toss is Started");
        }
    }

    public class Football : Game
    {
        public override void GameStart()
        {
            Console.WriteLine("Football Game Started");
        }

        public override void TicketBooking()
        {
            Console.WriteLine("TicketBooking started");
        }

        public override void Toss()
        {
            Console.WriteLine("Football Toss is Started");
        }
    }
}

				
			
				
					// See https://aka.ms/new-console-template for more information

using templatepattern;

Game obj = new Cricket();
obj.Initialize();
Console.WriteLine();

Game obj1 = new Football();
obj1.Initialize();
				
			

TicketBooking started
Cricket Toss is Started
Cricket Game Started

TicketBooking started
Football Toss is Started
Football Game Started

Leave a Comment