设计模式之工厂模式
2015-08-09 14:55
330 查看
根据单一职责原则,专门的类用来实例化对象。我们习惯把这种类叫做工厂。
![](http://img.blog.csdn.net/20150809144659407)
public interface Ihelper { void open(); void close(); }
public class ORACLEHelper implements Ihelper{ @Override public void open() { System.out.println("ORACLEHelper open....."); } @Override public void close() { System.out.println("ORACLEHelper close....."); } }
public class SQLHelper implements Ihelper{ @Override public void open() { System.out.println("SQL open....."); } @Override public void close() { System.out.println("SQL close...."); } }
public interface IFactory { public Ihelper create(int i); }
public class Factory implements IFactory{ @Override public Ihelper create(int i) { if(i == 0) return new SQLHelper(); else return new ORACLEHelper(); } }
public static void main(String[] args) { Factory f = new Factory(); Ihelper i = f.create(0); i.open(); } 工厂类的优点是: 利于管理 低耦合 缺点是: 违反开闭原则
相关文章推荐
- 压缩包Mysql的安装以及卸载的简单方法
- Android应用:SurfaceView中添加控件
- 重复子串问题(二):求一个字符串中连续出现次数最多的子字符串
- Windows系统常用修复命令 无须重装系统
- 最小公倍数 1108
- Visual Studio 智能提示功能消失解决办法
- Android Studio入门到精通
- 使用genymotion开发Android配置
- 排序专题之选择排序
- Android ANR
- Arduino:L298N模块使用
- UIView
- 超过lua上帝的语言
- Visual Studio 智能提示功能消失解决办法
- 第21章 动态链接库和钩子(2)
- 1106 排序(类似求和求到手软)
- 国内大型的内部 C# 编程规范
- Visual Studio 起始页中不显示最近使用的项目的解决办法
- 构造方法和匿名对象的使用
- Visual Studio 起始页中不显示最近使用的项目的解决办法