template,method,pattern

Erhan 8/7/2016 0

Template Method Pattern

C#
 Template Method is a widely used design pattern and it provides great flexibility in
our designs. This is one of my favourite patterns.
Challenge
You are creating machines for creating Pizza and Burger. When we look closely we
can see all the machines have some operations in common and in the same order.
. Start
. Produce
. Stop
The Start operation consists of turning on the machines, do the system check for any
troubles and turning on the indicators.
The Produce operation does the respective production of item.
The Stop operation shutdowns the internal workings of the machine, turns off the
indicators and power offs the machine.
The challenge is to create more machines like Cheese Burger, Pan Pizza with less
custom implementations. We need to take care that the duplicate codes are
avoided in our design.
Definition
GoF Definition: "Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. Template Method lets subclasses redefine certain steps of an
algorithm without changing the algorithm’s structure"
Implementation
We can see that all the operation appears in the same sequence. Often the Start
and Stop operation are common to both Pizza and Burger machines.
So we can define a skeleton of algorithm (or the order of invocation) like below:
1. Start
2. Produce
3. Stop
We can see all the machines have this order of execution. Only the Produce
operation will be different

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TemplateMethodPattern
{
public class Machine
{
    public void Execute()
    {
        Start();
        Produce();
        Stop();
    }

    protected virtual void Start()
    {
        Trace.WriteLine("Machine.Starting.." );
    }

    protected virtual void Produce()
    {
        Trace.WriteLine("Machine.Producing.. (But I don't know what to produce)");
    }

    protected virtual void Stop()
    {
        Trace.WriteLine("Machine.Stopping.." );
        Trace.WriteLine(string .Empty);
    }

    public override string ToString()
    {
        return GetType().Name;
    }
}
}

******************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace TemplateMethodPattern
{
    public class BurgerMachine : Machine
    {
        protected override void Produce()
        {
            Trace.WriteLine("BurgerMachine.Producing.." );

            MainForm.Instance.flowLayoutPanel1.Controls.Add(new PictureBox() { Image = Images .Burger, Width = 150, Height = 150, SizeMode = PictureBoxSizeMode .StretchImage, BorderStyle = BorderStyle.Fixed3D });
        }
    }
}
**************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace TemplateMethodPattern
{
    public class PizzaMachine : Machine
    {
        protected override void Produce()
        {
            Trace.WriteLine("PizzaMachine.Producing.." );

            MainForm.Instance.flowLayoutPanel1.Controls.Add(new PictureBox() { Image = Images .Pizza, Width = 150, Height = 150, SizeMode = PictureBoxSizeMode.StretchImage, BorderStyle = BorderStyle .Fixed3D });
        }
    }
}

******************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TemplateMethodPattern
{
    public partial class MainForm : Form
    {
        public static MainForm Instance;

        public MainForm()
        {
            Instance = this;

            InitializeComponent();

            SetTraceListener();

            FillMachineList();
        }

        private void SetTraceListener()
        {
            CustomTraceListener listener = new CustomTraceListener(LogBox);
            Trace.Listeners.Add(listener);
        }

        private void FillMachineList()
        {
            MachineBox.Items.Clear();
            MachineBox.Items.Add( new PizzaMachine ());
            MachineBox.Items.Add( new BurgerMachine ());

            MachineBox.SelectedIndex = 0;
        }

        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            (MachineBox.SelectedItem as Machine ).Execute();
        }
    }
}
*************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace TemplateMethodPattern
{
    public class CustomTraceListener : TraceListener
    {
        private ListBox _logBox;

        public CustomTraceListener(ListBox logBox)
        {
            _logBox = logBox;
        }

        public override void Write( string message)
        {
            WriteLine(message);
        }

        public override void WriteLine( string message)
        {
            _logBox.Items.Add(message);

            _logBox.SelectedIndex = _logBox.Items.Count - 1;
        }
    }
}
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments