桥接模式的目的是把抽象和具体实现分离,其uml类图如下所示:
public interface SendMessage {
abstract void send(String message,String name);
}
public class EmailSend implements SendMessage{
public void send(String message,String name){
System.out.println(message+" through email send to"+ name);
}
}
public class SystemSend implements SendMessage{
public void send(String message,String name){
System.out.println(message+" through send to "+name);
}
}
public abstract class Message{
public SendMessage sendMethd ;
public Message(SendMessage s){
this.sendMethd = s;
}
public abstract void send(String message,String name);
}
public class CommandMessage extends Message{
public CommandMessage(SendMessage s){
super(s);
}
public void send(String message,String name){
this.sendMethd.send(message, name);
}
}
public class ErgentMessage extends Message{
public ErgentMessage(SendMessage s){
super(s);
}
public void send(String m,String n){
m+="加急";
this.sendMethd.send(m,n);
}
}
public class TestBridge{
public static void main(String args[]){
SendMessage s1 = new EmailSend();
Message m1 = new CommandMessage(s1);
m1.send("utstarcom","china");
Message m2 = new ErgentMessage(s1);
m2.send("utstarcom","china");
}
}