- 新建A司机类,类名:ADriver
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorPattern
{
public class ADriver : AbstractCompany, IDriver
{
public void Receive()
{
Console.WriteLine("ADriver:接收到信息");
}
public void Send()
{
dCompany.Sender();
}
}
}
- 新建B司机类,类名:BDriver
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorPattern
{
public class BDriver : AbstractCompany, IDriver
{
public void Receive()
{
Console.WriteLine("BDriver:接收到消息");
}
public void Send()
{
dCompany.Sender();
}
}
}
- 新家司机接口类,类名:IDriver
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorPattern
{
public interface IDriver
{
void Send();
void Receive();
}
}
- 新建公司抽象类,类名:AbstractCompany
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorPattern
{
public abstract class AbstractCompany
{
public DDCompany dCompany { get; set; }
}
}
- 新建公司实现类,类名:
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorPattern
{
public class DDCompany
{
private List list = new List();
public void Add(IDriver driver)
{
list.Add(driver);
}
public void Sender()
{
list.ForEach(d => {
d.Receive();
});
}
}
}
- 入口函数类,类名:
using System;
namespace MediatorPattern
{
internal class Program
{
static void Main(string[] args)
{
ADriver aDriver = new ADriver();
BDriver bDriver = new BDriver();
DDCompany dDCompany = new DDCompany();
dDCompany.Add(bDriver);
aDriver.dCompany = dDCompany;
aDriver.Send();
}
}
}