Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
/// <summary>
/// Enum to define different types of Requests.
/// </summary>
internal enum RequestType
{
Contact,
Messages,
Memory
}
/// <summary>
/// Base class for all Requests.
/// </summary>
internal abstract class RequestBase
{
public readonly RequestType type ;
protected RequestBase(RequestType type)
{
this.type = type;
}
}
#region Request Implementation
/// <summary>
/// Contact Request object to hold contact information.
/// </summary>
internal class ContactRequest : RequestBase
{
public int DeviceID { get; private set; }
public string ContactName { get; private set; }
public ContactRequest(int ID, string name) : base(RequestType.Contact)
{
this.DeviceID = ID;
this.ContactName = name;
}
}
/// <summary>
/// Message Request object to hold Messages information.
/// </summary>
internal class MessagesRequest : RequestBase
{
public int DeviceID { get; private set; }
public string Message { get; private set; }
public MessagesRequest(int ID, string message)
: base(RequestType.Messages)
{
this.DeviceID = ID;
this.Message = message;
}
}
/// <summary>
/// Memory Request object to hold Memory information.
/// </summary>
internal class MemoryRequest : RequestBase
{
public int DeviceID { get; private set; }
public int TotalMemory { get; private set; }
public int MemoryLeft { get; private set; }
public MemoryRequest(int ID, int totalMemory, int memoryLeft) : base(RequestType.Memory)
{
this.DeviceID = ID;
this.TotalMemory = totalMemory;
this.MemoryLeft = memoryLeft;
}
}
#endRegion "End of Request Implementation"
/// <summary>
/// Base class for all Request Handlers.
/// </summary>
internal abstract class RequestHandlerBase
{
private RequestHandlerBase NextHandler { get; set; }
public void Execute(RequestBase request)
{
this.Process(request);
PushNext(request);
}
protected abstract void Process(RequestBase request);
public RequestHandlerBase SetNextHandler(RequestHandlerBase handler)
{
this.NextHandler = handler;
return this.NextHandler;
}
private void PushNext(RequestBase request)
{
if (this.NextHandler != null)
this.NextHandler.Execute(request);
}
}
#region Request Handler Implementation
/// <summary>
/// Contact Handler object to Handle Contact Request information.
/// </summary>
internal class ContactRequestHandler : RequestHandlerBase
{
protected override void Process(RequestBase request)
{
if (request.type == RequestType.Contact)
{
var contact = (ContactRequest)request;
Console.WriteLine("Processing Contact Request. \n Device ID-> {0} - Contact Name ->{1}", contact.DeviceID, contact.ContactName);
}
}
}
/// <summary>
/// Messages Handler object to Handle Message Request information.
/// </summary>
internal class MessagesRequestHandler : RequestHandlerBase
{
protected override void Process(RequestBase request)
{
if (request.type == RequestType.Messages)
{
var message = (MessagesRequest)request;
Console.WriteLine("Processing Messages Request. \n Device ID-> {0} - Message ->{1}", message.DeviceID, message.Message);
}
}
}
/// <summary>
/// Memory Handler object to Handle Memory Request information.
/// </summary>
internal class MemoryRequestHandler : RequestHandlerBase
{
protected override void Process(RequestBase request)
{
if (request.type == RequestType.Memory)
{
var memory = (MemoryRequest)request;
Console.WriteLine("Processing Messages Request. \n Device ID-> {0} - Total Memory ->{1} - Memory Left -> {2}", memory.DeviceID, memory.TotalMemory, memory.MemoryLeft);
}
}
}
#endregion "End of Request Handler Implementation"
static void Main(string[] args)
{
// Create Request Handler Lists.
var handler = new ContactRequestHandler();
handler.SetNextHandler(new MessagesRequestHandler())
.SetNextHandler(new MemoryRequestHandler());
// Create Multiple requests.
List request = new List();
request.Add(new ContactRequest(2342244, "Nokia-X23"));
request.Add(new MessagesRequest(2342244, "Hello Everyone ! how r u?"));
request.Add(new MemoryRequest(2342244,2048,543));
request.ForEach(x => {
handler.Execute(x);
});
}
/// <summary>
/// Enum to define different types of Requests.
/// </summary>
internal enum RequestType
{
Contact,
Messages,
Memory,
Location
}
/// <summary>
/// Location Request object to hold Location information.
/// </summary>
internal class LocationRequest : RequestBase
{
public int DeviceID { get; private set; }
public int Latitude { get; private set; }
public int Longitude { get; private set; }
public LocationRequest(int ID, int latitude, int longitude) : base(RequestType.Location)
{
this.DeviceID = ID;
this.Latitude = latitude;
this.Longitude = longitude;
}
}
/// <summary>
/// Location Handler object to Handle Location Request information.
/// </summary>
internal class LocationRequestHandler : RequestHandlerBase
{
protected override void Process(RequestBase request)
{
if (request.type == RequestType.Location)
{
var location = (LocationRequest)request;
Console.WriteLine("Processing Location Request. \n Device ID-> {0} - Latitude->{1} - Longitude -> {2}", location.DeviceID, location.Latitude, location.Longitude);
}
}
}
static void Main(string[] args)
{
// Create Request Handler Lists.
var handler = new ContactRequestHandler();
handler.SetNextHandler(new MessagesRequestHandler())
.SetNextHandler(new MemoryRequestHandler())
.SetNextHandler(new LocationRequestHandler());
// Create Multiple requests.
List<RequestBase> request = new List<RequestBase>();
request.Add(new ContactRequest(2342244, "Nokia-X23"));
request.Add(new MessagesRequest(2342244, "Hello Everyone ! how r u?"));
request.Add(new MemoryRequest(2342244,2048,543));
request.Add(new LocationRequest(2134324, 23434, 4645654));
request.ForEach(x => {
handler.Execute(x);
});
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)