Definition:
Mediator defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
UML Diagram:
Sample code:
Mediator:
public enum BillerType
{
Vodafone = 1,
BESCOM = 2,
TataIndicom = 3,
Airtel = 4
}
public interface IBillPayAgent : IDisposable
{
bool PayBill(double amount);
}
public class BillPayAgent : IBillPayAgent
{
private BillerType _billerType;
public BillPayAgent(BillerType billerType)
{
this._billerType = billerType;
}
private IBiller Biller
{
get { return BillerFactory.GetFactory(_billerType); }
}
public bool PayBill(double amount)
{
using (Biller)
{
Biller.ReceiveAmount(amount);
}
return true;
}
public void Dispose()
{
Dispose(true);
}
~BillPayAgent()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
{
Vodafone = 1,
BESCOM = 2,
TataIndicom = 3,
Airtel = 4
}
public interface IBillPayAgent : IDisposable
{
bool PayBill(double amount);
}
public class BillPayAgent : IBillPayAgent
{
private BillerType _billerType;
public BillPayAgent(BillerType billerType)
{
this._billerType = billerType;
}
private IBiller Biller
{
get { return BillerFactory.GetFactory(_billerType); }
}
public bool PayBill(double amount)
{
using (Biller)
{
Biller.ReceiveAmount(amount);
}
return true;
}
public void Dispose()
{
Dispose(true);
}
~BillPayAgent()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
Supplier:
public class IBiller : IDisposable
{
public virtual void ReceiveAmount(double amount)
{
Console.WriteLine("Received Amount: " + amount.ToString());
}
public void Dispose()
{
Dispose(true);
}
~IBiller()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
public sealed class Vodafone : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from Vodafone");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class TataIndicom : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from TataIndicom");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class BESCOM : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from BESCOM");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class Airtel : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from Airtel");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
{
public virtual void ReceiveAmount(double amount)
{
Console.WriteLine("Received Amount: " + amount.ToString());
}
public void Dispose()
{
Dispose(true);
}
~IBiller()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
public sealed class Vodafone : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from Vodafone");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class TataIndicom : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from TataIndicom");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class BESCOM : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from BESCOM");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
public sealed class Airtel : IBiller
{
public override void ReceiveAmount(double amount)
{
Console.WriteLine("Greetings from Airtel");
base.ReceiveAmount(amount);
}
protected override void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
}
Consumer:
public class Consumer : IDisposable
{
private IBillPayAgent billPayAgent = default(IBillPayAgent);
public void DoShopping()
{
Console.WriteLine("I am doing shopping");
}
public void PayEBBill(double amount)
{
using (billPayAgent = new BillPayAgent(BillerType.BESCOM))
{
billPayAgent.PayBill(amount);
}
}
public void PayMobileBill(double amount,BillerType billerType)
{
using (billPayAgent = new BillPayAgent(billerType))
{
billPayAgent.PayBill(amount);
}
}
public void Dispose()
{
Dispose(true);
}
~Consumer()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
{
private IBillPayAgent billPayAgent = default(IBillPayAgent);
public void DoShopping()
{
Console.WriteLine("I am doing shopping");
}
public void PayEBBill(double amount)
{
using (billPayAgent = new BillPayAgent(BillerType.BESCOM))
{
billPayAgent.PayBill(amount);
}
}
public void PayMobileBill(double amount,BillerType billerType)
{
using (billPayAgent = new BillPayAgent(billerType))
{
billPayAgent.PayBill(amount);
}
}
public void Dispose()
{
Dispose(true);
}
~Consumer()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
}
}
No comments:
Post a Comment