简单工厂:一个工厂类根据传入的参量决定创建出哪一种产品类的实例
直接一个工厂类,内部通过
swith(operator) {
case"+": oper=new AddOperation();//产生对象
...
而工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
定义一个接口 DAO
Add类实现该接口:AddDAO implements DAO{。。。//产生对象}
客户端代码: DAO dao=new AddDAO();
dao.createOper();//产生对象
...
这样如果需要增加一个功能类,简单工厂需要修改工厂类,而工厂方法只要增加一个类(实现DAO接口)产生相应的功能对象,
这样对比正解释了开放-封闭原则,对扩展开放,对修改关闭。