设计模式中的Bridge模式
来源:互联网 发布:你最讨厌的人 知乎 编辑:程序博客网 时间:2024/05/17 22:59
此模式是完成“抽象”与“实现”相分离,专业说是“解藕” ,数据对象与操作行为相分离。下面写一个简单的例子与各位同行共享:
- 先建一个数据操作的接口Operator public interface Operator ...{
String work(String arg);
} - 实现数据操作Operator 接口 public class Insert implements Operator...{
public String work(String arg) ...{
// TODO Auto-generated method stub
return "insert "+arg;
}
}
public class Update implements Operator ...{
public String work(String arg) ...{
// TODO Auto-generated method stub
return "update " + arg;
}
} - 再建一个抽象的数据对象,在其中内置一个数据操作接口。 public abstract class Table ...{
protected Operator impl = null;
public abstract void work();
public Operator getImpl() ...{
return impl;
}
public void setImpl(Operator impl) ...{
this.impl = impl;
}
} - 再做两个具体的数据对象类,继承上边的抽象类。 public class Table1 extends Table...{
@Override
public void work() ...{
// TODO Auto-generated method stub
System.out.println(this.impl.work("Table1"));
}
}
public class Table2 extends Table...{
@Override
public void work() ...{
// TODO Auto-generated method stub
System.out.println(this.impl.work("Table2"));
}
} - 把数据对象与数据操作根据业务相组合,完成业务功能。 Table tbl1 = new Table1();
tbl1.setImpl(new Insert());
tbl1.work();
tbl1.setImpl(new Update());
tbl1.work();
Table tbl2 = new Table2();
tbl2.setImpl(new Update());
tbl2.work();
tbl2.setImpl(new Insert());
tbl2.work();
- 设计模式中的Bridge模式
- 设计模式----Bridge模式
- 设计模式:Bridge模式
- 设计模式----Bridge模式
- 设计模式 -- Bridge模式
- 设计模式-Bridge模式
- 【设计模式】bridge模式
- 设计模式----Bridge模式
- 设计模式--------Bridge模式
- 设计模式-Bridge模式
- 设计模式 - Bridge模式
- 设计模式--Bridge 模式
- 设计模式之Bridge
- 设计模式之Bridge
- 设计模式之Bridge
- 设计模式二:bridge
- 设计模式之Bridge
- 设计模式之Bridge
- 中兴笔试资料
- 概念收集
- 谈“80后”程序员为什么找不到工作?
- 惯用美语口语
- 简单的I2C协议理解(转)
- 设计模式中的Bridge模式
- 2007 AIX and UNIX TOP 10!
- 流行美语口语100句
- 何时采用SOA,何时不采用SOA
- 咫尺天涯,时刻为你连线 - CSDN个人空间
- QT4:qt4的最佳开发搭档(ide)-集成开发环境
- Send mail
- TEST
- 禁止保存网页的方法