桥接模式的目的是把抽象和具体实现分离,其uml类图如下所示:
package com.utstar.pattern.bridge;
public interface SendMessage { abstract void send(String message,String name); }
|
package com.utstar.pattern.bridge;
public class EmailSend implements SendMessage{ public void send(String message,String name){ System.out.println(message+" through email send to"+ name); } }
|
package com.utstar.pattern.bridge;
public class SystemSend implements SendMessage{ public void send(String message,String name){ System.out.println(message+" through send to "+name); } }
|
package com.utstar.pattern.bridge;
public abstract class Message{ public SendMessage sendMethd ; public Message(SendMessage s){ this.sendMethd = s; } public abstract void send(String message,String name); }
|
package com.utstar.pattern.bridge;
public class CommandMessage extends Message{ public CommandMessage(SendMessage s){ super(s); } public void send(String message,String name){ this.sendMethd.send(message, name); } }
|
package com.utstar.pattern.bridge;
public class ErgentMessage extends Message{ public ErgentMessage(SendMessage s){ super(s); } public void send(String m,String n){ m+="加急"; this.sendMethd.send(m,n); } }
|
package com.utstar.pattern.bridge;
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"); } }
|
阅读(1717) | 评论(0) | 转发(0) |