Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1698210
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2011-03-17 17:46:04

桥接模式的目的是把抽象和具体实现分离,其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) |
给主人留下些什么吧!~~