Here is a nice little code I did recently, where the Command Pattern is used with a notification observer like pattern.
First of the Command Pattern, a simple interface for task with an execute method
public interface ITask
{
///
/// Execute the task
///
void Execute();
}
Then the notification interface, with 2 methods, one when it started and one when its complete and the event delegate
/// Notification interface
///
public interface INotify
{
///
/// Process has start
///
void ProcessStart();
///
/// Process is Complete
///
void ProcessComplete();
}
public class NotifyEventArgs : EventArgs
{
public enum NotificationStatus
{
START,
COMPLETE,
ERROR
} ;
public NotifyEventArgs(string notficationText, NotificationStatus status)
{
NotifcationText = notficationText;
Status = status;
}
///
/// Status of notification
///
public NotificationStatus Status { get; private set; }
///
/// Text to notify
///
public string NotifcationText { get; private set; }
}
Now the class that implements the interface, I used an abstract class so that I can just use a subclass to implement the simple task I wish to have
public abstract class AbstractTask : ITask, INotify
{
///
/// Event for notification
///
public event EventHandler NotificationChanged;
///
/// Task to execute
///
public void Execute()
{
try
{
ProcessStart();
ProcessingTask();
ProcessComplete();
}
catch (Exception e)
{
LogException(e);
}
}
///
/// When task is started call notification
///
public void ProcessStart()
{
if (NotificationChanged == null)
return;
NotificationChanged(this, new NotifyEventArgs(NotificationText, NotifyEventArgs.NotificationStatus.START));
}
///
/// When task is complete call notification
///
public void ProcessComplete()
{
if (NotificationChanged == null)
return;
NotificationChanged(this, new NotifyEventArgs(NotificationText, NotifyEventArgs.NotificationStatus.COMPLETE));
}
///
/// Log the exception
///
///
protected void LogException(Exception e)
{
if (NotificationChanged != null)
NotificationChanged(this, new NotifyEventArgs(e.ToString(), NotifyEventArgs.NotificationStatus.ERROR));
}
///
/// Text to display for notification
///
protected string NotificationText { get; set; }
///
/// Process task for inheriting class
///
protected abstract void ProcessingTask();
}
Now I can write a simple class that inherits from AbstractTask like this
public class HelloTask: AbstractTask
{
public HelloTask()
{
NotificationText = "Hello Task";
}
protected override void ProcessingTask()
{
Console.WriteLine("Hello World");
}
}
Now to consume the task
public static void Main(string[] args)
{
AbstractTask task = new HelloTask();
task.NotificationChanged += new EventHandler(NotifyAction);
task.Execute();
}
private static void NotifyAction(object source, NotifyEventArgs args)
{
Console.WriteLine(args.NotificationText);
}
This is more or less the simple form of creating a command pattern to run task that includes a notification to send back. It makes sense to have multiple task like creating users and running database scripts etc etc. This allows one to have flexible design and have an IEnumerable<AbstractTask> and run through each one with an execute method.
Hope this helps 🙂 Happy coding.