正在学习中!!!!!
upupupupupup

解决方案 »

  1.   

    pf = new PublicFlow(this);
     sm = new smsdeal.SmsMethod(this);
      

  2.   

    不过不如这样:
    public SmsMethod(IOPut ioPut,DataDeal dataDeal, TableDeal  tableDeal, PublicFlow pf) { 
    }
    public PublicFlow(DataDeal dataDeal, IOPut ioPut, Util util) {
    }在Main中装配
    {
    datadeal = new DataDeal();
        ioPut = new djb.util.IOPut();
        util = new djb.util.Util();
        tabledeal = new djb.database.TableDeal();    pf = new PublicFlow(dataDeal,ioPut,util);
        sm = new smsdeal.SmsMethod(ioPut,dataDeal,tableDeal,pf);
        
    }
    比依赖Main清晰
      

  3.   

    to jkit:那你说怎么设计好些呢?
      

  4.   

    这么复杂的装配建议使用pico等ioc container完成
      

  5.   

    to raimundo:pico?ioc container ?
    请将这两个名词简单讲解,谢了!
      

  6.   

    ioc container就是去年超得特热的Inversion of Control Container,以PicoContainer和Spring的BeanFactory为代表,现在似乎改名了,叫Dependency Injector Container。
    意思怎么解释呢?举个例子吧
    如果一个组件A需要B,C,D,E等组件,那么采用Contructor Injector就是
    class A {
        A(B b, C c,D d, E e) {
        }
    }
    把需要的组件在Constructor中声明来,然后使用PicoContainerPicoContainer container = new DefaultPicoContainer();
    container.registerComponent(A.class);
    container.registerComponent(B.class);
    container.registerComponent(C.class);
    container.registerComponent(D.class);
    container.registerComponent(E.class);
    A a = (A) container.getComponentInstance(A.class);pico会根据A的声明,自动装配出A的实例
      

  7.   

    也可以
    container.registerComponent(A.class);
    container.registerInstance(B.class, BInstance);
    container.registerInstance(C.class, CInstance);
    container.registerInstance(D.class, DInstance);
    container.registerInstance(E.class, EInstance);
      

  8.   

    赫赫,采用这样设计其实就是强内聚和factory模式的一种扩展,哲学就是一个类只声明它需要的对象,而不负责取得,把获取组件的工作交给轻量容器
      

  9.   

    而轻量容器可以认为是一种具有统一形式的factory
      

  10.   

    看得不是很懂,感觉差距较大,汗ing。还是回到讨论的这个问题上来吧我自己做了测试,下面是测试结果。
    ------------------------------------------------------
    public class test {
      A a;
      B b;
      C c;
      public test() {
        a = new A(this);
        b = new B(this);
        c = new C(this);
      }
      public static void main(String args[]) {
         test t = new test();
      }}class A {
      B b;
      C c;
      public A(test t) {
        b = t.b;
        Print.print(b);
        c = t.c;
        Print.print(c);
      }
    }class B {
      A a;
      C c;
      public B(test t) {
        a = t.a;
        Print.print(a);
        c = t.c;
        Print.print(c);
      }
    }class C {
      A a;
      B b;
      public C(test t) {
        a = t.a;
        Print.print(a);
        b = t.b;
        Print.print(b);
      }
    }
    class Print{
      public static void print(Object obj){
        System.out.println(obj);
      }
    }
    ------------------------------------------------------
    结果:
    [email protected]@1a125f0smsdeal.B@181afa3
    ------------------------------------------------------
    而要是将需要调用的对象前移,就不会出现上述问题
    ------------------------------------------------------
    public class test {
      A a;
      B b;
      C c;
      D d;
      E e;
      public test() {
        d = new D();
        e = new E();
        a = new A(this);
        b = new B(this);
        c = new C(this);
      }
      public static void main(String args[]) {
         test t = new test();
      }}class A {
      D d;
      E e;
      public A(test t) {
        d = t.d;
        Print.print(d);
        e = t.e;
        Print.print(e);
      }
    }class B {
      D d;
      E e;
      public B(test t) {
        d = t.d;
        Print.print(d);
        e = t.e;
        Print.print(e);
      }}class C {
      D d;
      E e;
      public C(test t) {
        d = t.d;
        Print.print(d);
        e = t.e;
        Print.print(e);
      }}
    class D{
      public D(){}
    }
    class E{
      public E(){}
    }class Print{
      public static void print(Object obj){
        System.out.println(obj);
      }
    }------------------------------------------------------
    结果:
    [email protected]@[email protected]@[email protected]@181afa3
    ------------------------------------------------------
    结论:
    构造函数创建对象需要经过两个过程
    1:给该对象分配内存空间
    2:调用该对象的初始化函数(即调用父类缺省构造函数,然后调用自己的构造函数)
        当我们将this传入各类(如类A)构造函数的时候,由于对主类test对象t的构造只进行了第一步,所以this只代表了对象t的内存地址,所以此时b和c都是null。
        所以,如果要用到主类中其它对象时,必须得等到该对象(并非主类对象)完成创建后(即两步都完成),才能调用。
      

  11.   

    谢谢jkit指出不足,尤其谢谢raimundo给我的视野的开阔。先让大家看看这个帖子,我会在今天内结帖!再次谢谢raimundo!
      

  12.   

    to  qlampskyface(天空的样子)jkit说你的设计烂是因为在main()类里存在顺序耦合,必须按照依赖关系来决定组建的构造顺序,一旦出现循环依赖,就没有办法了,改进:
    把constructor injector改为setter injector,也就是mediator patterninterface IMediatorAware {
       void setMediator(Mediator m);
    }class A implements IMediatorAware;
    class B implements IMediatorAware;
    class C implements IMediatorAware;class Mediator {
       A a;
       B b;
       C c;
       Mediator() {
          A a = new A();
          B b = new B();
          C c = new C();
          a.setMediator(this);
          b.setMediator(this);
          c.setMediator(this);
       }
    }然后ABC在setMediator中拿数据,可以取消顺序耦合
      

  13.   

    勘误:
    把constructor injector改为setter injector,也就是mediator pattern
    =〉
    把constructor injector改为setter injector,实现mediator patternsetter injector和mediator pattern没有必然联系,这里用setting injector实现mediator pattern