Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample 2 : Template Method Design Pattern
class Program
{
static void Main(string[] args)
{
BaseProcessor reportProcessor = null;
reportProcessor = new ImportReportProcessor();
reportProcessor.ProcessorCompleted += reportProcessor_ProcessorCompleted;
reportProcessor.ProcessorStarting += reportProcessor_ProcessorStarting;
reportProcessor.ProcessorStarted += reportProcessor_ProcessorStarted;
// to getting import report
reportProcessor.Process();
reportProcessor = new ExportReportProcessor();
// to getting export report
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
public abstract void GetReportDefinitions();
public abstract void RunReportJob();
public abstract void ValidateReport();
public abstract void CreateReportFile();
public abstract void ExtractReportFile();
// 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
{
GetReportDefinitions();
RunReportJob();
ValidateReport();
CreateReportFile();
ExtractReportFile();
}
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 ImportReportProcessor
: BaseProcessor
{
// primitive method 1
public override void GetReportDefinitions()
{
Console.WriteLine("ImportReportProcessor.GetReportDefinitions");
}
// primitive method 2
public override void RunReportJob()
{
Console.WriteLine("ImportReportProcessor.RunReportJob");
}
// primitive method 3
public override void ValidateReport()
{
Console.WriteLine("ImportReportProcessor.ValidateReport");
}
// primitive method 4
public override void CreateReportFile()
{
Console.WriteLine("ImportReportProcessor.CreateReportFile");
}
// primitive method 5
public override void ExtractReportFile()
{
Console.WriteLine("ImportReportProcessor.ExtractReportFile");
}
}
/// <summary>
/// Concrete Class
/// </summary>
public class ExportReportProcessor
: BaseProcessor
{
// primitive method 1
public override void GetReportDefinitions()
{
Console.WriteLine("ExportReportProcessor.GetReportDefinitions");
}
// primitive method 2
public override void RunReportJob()
{
Console.WriteLine("ExportReportProcessor.RunReportJob");
}
// primitive method 3
public override void ValidateReport()
{
Console.WriteLine("ExportReportProcessor.ValidateReport");
}
// primitive method 4
public override void CreateReportFile()
{
Console.WriteLine("ExportReportProcessor.CreateReportFile");
}
// primitive method 5
public override void ExtractReportFile()
{
Console.WriteLine("ExportReportProcessor.ExtractReportFile");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment