2、... interface IntfN {} class NormalInnerClassDispatcher { class Imp implements Intf {} class Imp2 implements Intf2 {} ...... class ImpN implements IntfN {} //class Imp createImp (...) {return new Imp (...);} //class Imp2 createImp2(.
3、..) {return new Imp2(...);} //...... //class ImpN createImpN(...) {return new ImpN(...);} } class AnonymousInnerClassDispatcher { class Imp createImp () {return new Intf () {...};} class Imp2 createImp2() {return new Intf2() {...};} ......
4、 class ImpN createImpN() {return new IntfN() {...};} } interfaceIntf{}interfaceIntf2{}......interfaceIntfN{}classNormalInnerClassDispatcher{classImpimplementsIntf{}classImp2implementsIntf2{}......classImpNimplementsIntfN{}//classImpcreateImp(...){returnnewImp(...);}//classI
6、不想暴露对接口的实现(Imp,Imp2,...,ImpN),可以用一般内部类外加constructorproxy方法实现,其实和匿名内部类是等价的,还不如匿名内部类简洁。 但是如果把N个接口换成N个基类,要求用一个类来产生这N个基类的导出类对象,那么一般内部类比匿名内部类更灵活。Java代码class Base {} class Base2 {} ...... class BaseN {} class NormalInnerClassDispatcher { class Ext extends Base {}
7、 class Ext2 extends Base2 {} ...... class ExtN extends BaseN {} //class Ext createExt (...) {return new Ext (...);} //class Ext2 createExt2(...) {return new Ext2(...);} //...... //class ExtN createExtN(...) {return new ExtN(...);} } c