Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created January 6, 2014 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemrekeskin/8282604 to your computer and use it in GitHub Desktop.
Save yemrekeskin/8282604 to your computer and use it in GitHub Desktop.
Sample Template Method Design Pattern
class Program
{
static void Main(string[] args)
{
BaseProcessor reportProcessor = new ReportProcessor();
reportProcessor.ProcessorCompleted += reportProcessor_ProcessorCompleted;
reportProcessor.ProcessorStarting += reportProcessor_ProcessorStarting;
reportProcessor.ProcessorStarted += reportProcessor_ProcessorStarted;
// !!!!
reportProcessor.Process();
Console.ReadLine();
}
static void reportProcessor_ProcessorStarted(object sender, ProcessorEventArg e)
{
Console.WriteLine("Started");
}
static void reportProcessor_ProcessorStarting(object sender, ProcessorEventArg e)
{
Console.WriteLine("Starting");
}
static void reportProcessor_ProcessorCompleted(object sender, ProcessorEventArg e)
{
Console.WriteLine("Completed");
}
}
public class ProcessorEventArg
: EventArgs
{
}
public class ProcessorStatus
{
public enum Current
{
Starting = 1,
Started = 2,
Completing = 3,
Completed = 4,
Error = 5
}
}
public interface IProcessor
{
void Process();
}
/// <summary>
/// Abstract Class
/// </summary>
public abstract class BaseProcessor
: IProcessor
{
public delegate void ProcessorStartingEventHandler(object sender, ProcessorEventArg e);
public delegate void ProcessorStartedEventHandler(object sender, ProcessorEventArg e);
public delegate void ProcessorCompletedEventHandler(object sender, ProcessorEventArg e);
public delegate void ProcessorErrorEventHandler(object sender, ProcessorEventArg e);
public event ProcessorStartingEventHandler ProcessorStarting = null;
public event ProcessorStartedEventHandler ProcessorStarted = null;
public event ProcessorCompletedEventHandler ProcessorCompleted = null;
public event ProcessorErrorEventHandler ProcessorError = null;
public ProcessorStatus.Current Status { get; private set; }
protected virtual void OnProcessorStarting(ProcessorEventArg e)
{
if (ProcessorStarting != null)
ProcessorStarting(this.GetType(), e);
}
protected virtual void OnProcessorStarted(ProcessorEventArg e)
{
if (ProcessorStarted != null)
ProcessorStarted(this.GetType(), e);
}
protected virtual void OnProcessorCompleted(ProcessorEventArg e)
{
if (ProcessorCompleted != null)
ProcessorCompleted(this.GetType(), e);
}
protected virtual void OnProcessorError(ProcessorEventArg e)
{
if (ProcessorError != null)
ProcessorError(this.GetType(), e);
}
// Primitive Methods
// Primitive methodları alacağımız abstract method
// BaseProcessor sınıfını kalıtan her sınıf bu methodu uygulamak zorunda
public abstract List<Action> ProcessorMethods();
// Türemiş sınıfta (ReportProcessor) verilen primitive methodlar teker teker çalıştırılır
// Bu method SubProcess methodunda kontrollü olarakl çalıştırılır.
private void RunProcessorMethods()
{
List<Action> methods = this.ProcessorMethods();
if (methods.Count > 0 && methods.Count != 0)
{
foreach (Action method in methods)
method();
}
else
{
throw new ApplicationException("Çalıştırılacak method içermiyor");
}
}
// Template Method
public void Process()
{
ProcessorEventArg e = new ProcessorEventArg();
// starting loging for real job's log table
this.Status = ProcessorStatus.Current.Starting;
OnProcessorStarting(e);
SubProcess(e);
}
public void SubProcess(ProcessorEventArg e)
{
// started loging
this.Status = ProcessorStatus.Current.Started;
OnProcessorStarted(e);
try
{
RunProcessorMethods();
}
catch (Exception ex)
{
this.Status = ProcessorStatus.Current.Error;
OnProcessorError(e);
// error logging
}
// completing loging
this.Status = ProcessorStatus.Current.Completing;
OnProcessorCompleted(e);
// completed loging
this.Status = ProcessorStatus.Current.Completed;
}
}
/// <summary>
/// Concrete Class
/// </summary>
public class ReportProcessor
: BaseProcessor
{
public override List<Action> ProcessorMethods()
{
List<Action> reportJobs = new List<Action>();
reportJobs.Add(GetReportDefinitions);
reportJobs.Add(RunReportJob);
reportJobs.Add(ValidateReport);
reportJobs.Add(CreateReportFile);
return reportJobs;
}
// Primitive Method 1
public void GetReportDefinitions()
{
Console.WriteLine("GetReportDefinitions");
}
// Primitive Method 2
public void RunReportJob()
{
Console.WriteLine("RunReportJob");
}
// Primitive Method 3
public void ValidateReport()
{
Console.WriteLine("ValidateReport");
}
// Primitive Method 4
public void CreateReportFile()
{
Console.WriteLine("CreateReportFile");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment