Topic: Factory模式之我见 |
Print this page |
1.Factory模式之我见 | Copy to clipboard |
Posted by: johnx Posted on: 2003-03-04 10:36 不知道以下观点是否正确 全面,请各位大虾批评指正: Factory模式只是把对象的创建 引用进行了封装 比如有下面三个class public abstract class A{ } public class subA1 extends A{ } public class subA2 extends A{ } 在程序应用中,你需要创建subA1&subA2的实例进行应用 那么在程序中需要 subA1 a1=new subA1(); subA2 a2=new subA2(); 当需要需要这些对象的地方越来越多时; 当你的需要对你的A再次继承subA3 subA4...时; 那你的程序中new的地方越来越多 越来越混乱 你需要一种好的方式来进行管理 public class Factory{ public static create(){ if (...) {return new subA1();} else if (...) {return new subA2();} else {return new subA3();} } 这样就可以将对象的创建集中到这个Factory class中 对于类的管理就方便了许多。 在程序中只需要 A a=Factory.create(。。。); 就能得到你需要的子类的对象了 |
2.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: richardluo Posted on: 2003-03-05 14:53 基本正确,但方法返回类型应该是抽象产品类,呵呵 |
3.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-06 01:30 This is correct(plus luo's comment). And there is more than that in java. In your post, the nasty part is that create method since there are a lot of if-else statements in the code. This really smells bad. So try to remove that. There is an example in application framework from me. The bottom line is that we don't want to change the code whenever possible(although we, as energetic developers, like to code, ). |
4.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: sukai Posted on: 2003-03-12 11:59 哦,那段代码的风格太差了。很多时候,我们只需要在abstract class 中把create()声明为抽象方法,在具体的子类中,重写这个方法。factory method的意图是让子类决定实例化哪一个类。 使用的时候,根据"对接口编程“的原则,尽可能避免声明具体子类的对象就可以了。 |
5.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: yj780210 Posted on: 2003-03-13 08:53 johnx wrote: 我个人提点不同意见,我感觉还是用class variable 好一点,因为对一重量级的对象来说,每次都要重新创建,太浪费了。另外如果每次都要产生不同的实例。可以考虑用clone。 同时选择中个人不倾向用if 用switch比if快。 同时设置几个static变量也可以让client很容易知道能够产生什么样的东西。几个东西。 |
6.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: rainting Posted on: 2003-03-24 23:09 Although you adopt factory patterm , and maybe you still fell into a chaos with with so many "if..else...", which is a bad style. A better choice would be like as: 1) Abstract Class --> Interface 2) if..else... --> dynamic classloader ( if many object types to be created ) or if...else... -> abstract factory pattern |
7.heavy object creation -- Re:Factory模式之我见 [Re: yj780210] | Copy to clipboard |
Posted by: rainting Posted on: 2003-03-24 23:11 yj780210 wrote: If the object is a heavy one, please care about object creation. In this case, Singleton or objects pool would be better. |
8.Re:Factory模式之我见 [Re: yj780210] | Copy to clipboard |
Posted by: emarket Posted on: 2003-04-20 23:02 更general一点,在factoryMethod里最好用 reflection api去create object, 然后再一个xml文件中决定critica 和 class 的对应 这样新加入一个实现类,可以不用改任和现成的代码 只需要改一下对应的xml文件就行了 yj780210 wrote: |
9.Re:Factory模式之我见 [Re: johnx] | Copy to clipboard |
Posted by: richardluopeng Posted on: 2003-04-22 13:45 同意楼上的观点 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |