* 单一职责模式:
* 在软件组件的设计中,如果责任的划分不清楚
* 使用继承得到的结果往往是随着需求的变化,子类急剧膨胀
* 同时充斥着重复代码,这时候的关键是划清责任
* 典型模式:装饰器模式和桥模式
* 桥模式的使用动机:
* 由于某些固有的实现逻辑,使得他们具有两个变化的维度或者多个变化的维度
* 桥模式可以轻松地沿着多个方向变化,而不引入额外地复杂度
namespace 桥模式
{
public class Image
{
}
public abstract class Message
{
// 登录
public abstract void Login(string username, string password);
// 发送消息
public abstract void SendMessage(string message);
// 发送图片
public abstract void SendPicture(Image image);
// 播放声音
public abstract void PlaySound ();
// 绘图
public abstract void DrawShape ();
// 连接
public abstract void Connect ();
}
// 平台实现
public abstract class PCMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("PC端连接");
}
public override void DrawShape()
{
Console.WriteLine("PC端画画");
}
public override void PlaySound()
{
Console.WriteLine("PC端播放声音");
}
}
public abstract class MobileMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("手机端连接");
}
public override void DrawShape()
{
Console.WriteLine("手机端花花");
}
public override void PlaySound()
{
Console.WriteLine("手机端播放");
}
}
// 业务抽象
class PCMessageLite : PCMessageBase
{
public override void Login(string username, string password)
{
this.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
this.DrawShape() ;
}
}
class PCMessagePerfect : PCMessageBase
{
public override void Login(string username, string password)
{
this.Connect();
this.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
this.Connect();
this.DrawShape();
}
}
class MobileMessageLite : MobileMessageBase
{
public override void Login(string username, string password)
{
this.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
this.DrawShape();
}
}
class MobileMessagePerfect : MobileMessageBase
{
public override void Login(string username, string password)
{
this.Connect();
this.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
this.Connect();
this.DrawShape();
}
}
// 显然,为了实现n个平台任务和m个业务任务地组合,需要1+n+m*n个类,很浪费时间
改进版本
namespace 桥模式1
{
public class Image
{
}
public abstract class Message
{
public abstract void Login(string username, string password);
public abstract void SendMessage(string message);
public abstract void SendPicture(Image image);
public abstract void PlaySound();
public abstract void DrawShape();
public abstract void Connect();
}
// 平台实现
public abstract class PCMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("PC端连接");
}
public override void DrawShape()
{
Console.WriteLine("PC端画画");
}
public override void PlaySound()
{
Console.WriteLine("PC端播放声音");
}
}
public abstract class MobileMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("手机端连接");
}
public override void DrawShape()
{
Console.WriteLine("手机端花花");
}
public override void PlaySound()
{
Console.WriteLine("手机端播放");
}
}
// 业务抽象
class PCMessageLite
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.DrawShape();
}
}
class MobileMessageLite
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.DrawShape();
}
}
class PCMessagePerfect
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
message.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.Connect();
message.DrawShape();
}
}
class MobileMessagePerfect
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
message.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.Connect();
message.DrawShape();
}
}
// 这个时候发现PcmessegeLite和MobileMessageLite内类的内容是一样的,于是可以删除其中一个
// 此时函数无法重写这个错误暂时不管
}
再次改进
namespace 桥模式2
{
public class Image
{
}
public abstract class Message
{
public abstract void Login(string username, string password);
public abstract void SendMessage(string message);
public abstract void SendPicture(Image image);
public abstract void PlaySound();
public abstract void DrawShape();
public abstract void Connect();
}
// 平台实现
public abstract class PCMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("PC端连接");
}
public override void DrawShape()
{
Console.WriteLine("PC端画画");
}
public override void PlaySound()
{
Console.WriteLine("PC端播放声音");
}
}
public abstract class MobileMessageBase : Message
{
public override void Connect()
{
Console.WriteLine("手机端连接");
}
public override void DrawShape()
{
Console.WriteLine("手机端花花");
}
public override void PlaySound()
{
Console.WriteLine("手机端播放");
}
}
// 业务抽象
class PCMessageLite
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.DrawShape();
}
}
class PCMessagePerfect
{
public Message message;
public override void Login(string username, string password)
{
message.Connect();
message.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
message.Connect();
message.DrawShape();
}
}
// 此时函数无法重写,其原因为这个类没有了继承,所以加上继承
// PCMessageLite和PCMessagePerfect中都有public Message message;这个字段,可以把这个字段提到其共同的父类上
// 最后得到了结果看桥模式最后.cs
}
最后的代码
namespace 桥模式最后
{
public class Image
{
}
public abstract class Message
{
public MessageImp messageImp;
public abstract void Login(string username, string password);
public abstract void SendMessage(string message);
public abstract void SendPicture(Image image);
}
public abstract class MessageImp
{
public abstract void PlaySound();
public abstract void DrawShape();
public abstract void Connect();
}
// 平台实现
public class PCMessageBase : MessageImp
{
public override void Connect()
{
Console.WriteLine("PC端连接");
}
public override void DrawShape()
{
Console.WriteLine("PC端画画");
}
public override void PlaySound()
{
Console.WriteLine("PC端播放声音");
}
}
public class MobileMessageBase : MessageImp
{
public override void Connect()
{
Console.WriteLine("手机端连接");
}
public override void DrawShape()
{
Console.WriteLine("手机端花花");
}
public override void PlaySound()
{
Console.WriteLine("手机端播放");
}
}
class MessageLite :Message
{
public override void Login(string username, string password)
{
messageImp.Connect();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
messageImp.DrawShape();
}
}
class MessagePerfect:Message
{
public override void Login(string username, string password)
{
messageImp = new MobileMessageBase();
messageImp.Connect();
messageImp.PlaySound();
}
public override void SendMessage(string message)
{
// .....
}
public override void SendPicture(Image image)
{
messageImp.Connect();
messageImp.DrawShape();
}
}
// 这时候类的个数是1+n+m,但是在运行时可以实现n*m个
}