例子
- 给用户在不同平台发送消息(微信,APP,H5)
public abstract class AbsUserManager
{
protected AbsUserManager(AbsUserManagerProvider userManagerProvider)
{
UserManagerProvider = userManagerProvider;
}
protected AbsUserManagerProvider UserManagerProvider { get; }
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="msg"></param>
public void SendMessage(Guid userId, string msg)
{
UserManagerProvider.SendMessage(userId, msg);
}
/// <summary>
/// 强制下线
/// </summary>
/// <param name="userId"></param>
public void TapeOut(Guid userId)
{
UserManagerProvider.TapeOut(userId);
}
}
public abstract class AbsUserManagerProvider
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="msg"></param>
public abstract void SendMessage(Guid userId, string msg);
/// <summary>
/// 强制下线
/// </summary>
/// <param name="userId"></param>
public abstract void TapeOut(Guid userId);
}
public class AppUserManager : AbsUserManager
{
public AppUserManager(AppUserManagerProvider userManagerProvider) : base(userManagerProvider)
{
}
}
public class AppUserManagerProvider : AbsUserManagerProvider
{
public override void SendMessage(Guid userId, string msg)
{
Console.WriteLine($"从APP向{userId.ToString()}发送消息:{msg}");
}
public override void TapeOut(Guid userId)
{
Console.WriteLine($"从APP把{userId.ToString()}踢下线");
}
}
public class H5UserManager : AbsUserManager
{
public H5UserManager(H5UserManagerProvider userManagerProvider) : base(userManagerProvider)
{
}
}
public class H5UserManagerProvider : AbsUserManagerProvider
{
public override void SendMessage(Guid userId, string msg)
{
Console.WriteLine($"从H5向{userId.ToString()}发送消息:{msg}");
}
public override void TapeOut(Guid userId)
{
Console.WriteLine($"从H5把{userId.ToString()}踢下线");
}
}
public class WechatUserManager : AbsUserManager
{
public WechatUserManager(WechatUserManagerProvider userManagerProvider) : base(userManagerProvider)
{
}
}
public class WechatUserManagerProvider : AbsUserManagerProvider
{
public override void SendMessage(Guid userId, string msg)
{
Console.WriteLine($"从微信向{userId.ToString()}发送消息:{msg}");
}
public override void TapeOut(Guid userId)
{
throw new NotImplementedException();
}
}
static void Main(string[] args)
{
Guid userId = Guid.NewGuid();
string msg = "123123";
new H5UserManager(new H5UserManagerProvider()).SendMessage(userId, msg);
new AppUserManager(new AppUserManagerProvider()).SendMessage(userId, msg);
new WechatUserManager(new WechatUserManagerProvider()).SendMessage(userId, msg);
new H5UserManager(new H5UserManagerProvider()).TapeOut(userId);
new AppUserManager(new AppUserManagerProvider()).TapeOut(userId);
new WechatUserManager(new WechatUserManagerProvider()).TapeOut(userId);
}
个人总结
桥接模式就是 关联的属性是抽象类