声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
/**
* 个人觉得关于桥接模式的例子,蜡笔和毛笔这个例子是最贴切的:http://www.cnblogs.com/zhenyulu/articles/67016.html
* 笔和颜色是可分离的,蜡笔把两者耦合在一起了:一支蜡笔只有一种颜色。大、中、小三种尺寸的蜡笔,12种颜色,那一共需要3*12个类
* 画中国山水画时就不一样了,一支毛笔可以搭配不同的颜色,只需要大、中、小三支毛笔,外加12种颜色,那就只需12+3个类
*
* 书上的例子:
* 消息和消息发送
* 消息有很多种,普通的、加急的
* 消息发送方式也有很多种,站内信的、短信的、邮件的
* 因此要把“消息”和“消息发送方式”分离开来。如果要增加一种消息或者增加一种发送方式,都不会影响到原来的代码
*
* 网上的例子:
* 公路和汽车
* 也是同样的道理
*
* 原来桥接模式就是把类的职责单一化,然后用类组合的形式来实现复杂的功能
*
* 看一下下面的代码,发现桥接模式和策略模式是非常相似的。AbstractBrush就相当于是策略模式里面的Context,只是策略模式里面的Context通常
* 只有一个,但桥接模式的AbstractBrush的实现有多个。可以把策略模式看作是桥接模式的退化
*/
interface IColor {
void sprinkleOnCanvas();
}
class RedColor implements IColor {
public void sprinkleOnCanvas() {
System.out.println("Sprinkle red color on canvas." );
}
}
class BlueColor implements IColor {
public void sprinkleOnCanvas() {
System.out.println("Sprinkle blue color on canvas." );
}
}
abstract class AbstractBrush {
protected IColor color;
public AbstractBrush(IColor color) {
this.color = color;
}
protected void paint() {
this.color.sprinkleOnCanvas();
}
}
class BigBrush extends AbstractBrush{
public BigBrush(IColor color) {
super(color);
}
public void paint() {
System.out.print("Use big brush:");
super.paint();
}
}
class SmallBrush extends AbstractBrush{
public SmallBrush(IColor color) {
super(color);
}
public void paint() {
System.out.print("Use small brush:");
super.paint();
}
}
/**
* 书上的例子
*/
interface IMessageSender {
void send(String message, String toUser);
}
//消息的基类。普通消息、加急消息都是它的子类
abstract class AbstractMessage {
protected IMessageSender sender;
public AbstractMessage(IMessageSender sender) {
this.sender = sender;
}
public void sendMessage(String message, String toUser) {
this.sender.send(message, toUser);
}
}
//用短信的方式发送消息
class MessageSenderInSMS implements IMessageSender {
public void send(String message, String toUser) {
System.out.println("SMS:Send message to " + toUser + ",message is " + message);
}
}
//用email的方式发送消息
class MessageSenderInEmail implements IMessageSender {
public void send(String message, String toUser) {
System.out.println("Email:Send message to " + toUser + ",message is " + message);
}
}
//普通消息
class CommonMessage extends AbstractMessage {
public CommonMessage(IMessageSender sender) {
super(sender);
}
public void sendMessage(String message, String toUser) {
super.sendMessage(message, toUser);
}
}
//加急消息。比普通消息多了一些处理
class UrgencyMessage extends AbstractMessage {
public UrgencyMessage(IMessageSender sender) {
super(sender);
}
public void sendMessage(String message, String toUser) {
message = "(urgency!)" + message;
super.sendMessage(message, toUser);
}
public Object watch(String messageID) {
System.out.println("Watching. MessageID = " + messageID);
return null;
}
}
public class BridgePatternBrush {
public static void main(String[] args) {
//先用红色。大笔和小笔各画一次
IColor color = new RedColor();
AbstractBrush bigBrush = new BigBrush(color);
AbstractBrush smallBrush = new SmallBrush(color);
bigBrush.paint();
smallBrush.paint();
//换一种颜色
IColor color2 = new BlueColor();
bigBrush = new BigBrush(color2);
smallBrush = new SmallBrush(color2);
bigBrush.paint();
smallBrush.paint();
//sms
IMessageSender smsSender = new MessageSenderInSMS();
AbstractMessage commonMessage = new CommonMessage(smsSender);
AbstractMessage urgencyMessage = new UrgencyMessage(smsSender);
commonMessage.sendMessage("hello", "Kobe");
urgencyMessage.sendMessage("hello", "Kobe");
//email
IMessageSender emailSender = new MessageSenderInEmail();
commonMessage = new CommonMessage(emailSender);
urgencyMessage = new UrgencyMessage(emailSender);
commonMessage.sendMessage("hello", "Kobe");
urgencyMessage.sendMessage("hello", "Kobe");
}
}