循环类实例的问题 [英] Problem of circular class instances

查看:50
本文介绍了循环类实例的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个有4个clases的类结构:A,B,C和D类。

设计如下如下:



Hi,

I have a class structure having 4 clases: Class A, B, C and D.
Design is as follows:

public class A{
    private D;
    private B;
    public A() {
        D = new D();
        B = new B();
    }
}

public class B{
    private D;
    private A;
    private C;
    public B() {
        D = new D();
        A = new A();
        C = new C();
    }
}

public class C{
    private D;
    private A;
    private B;
    public C() {
        D = new D();
        A = new A();
        B = new B();
    }
}

//Utility class
public class D{
    public D(){
    }
}





所有类都有一些方法。

我需要在A类的某些方法中引用B类。

我需要在B类的某些方法中引用A类和C类。类似于C类。 br />
我需要在A,B和C类的所有方法中引用D类。



现在我的问题有时候我得到堆栈溢出异常,因为彼此的循环类实例。

任何人都可以告诉我有没有更好的结构,我可以用于我的课程,以满足我的所有要求。

或者我可以使用一些设计模式吗?



谢谢。



All classes have some methods.
I need reference of class B in some methods of class A.
I need reference of class A and C in some methods of class B. Similarly for class C.
I need reference of class D in all methods of classes A, B and C.

Now my problem is sometimes i am getting stack overflow exception because of circular instances of classes in each other.
Can anyone tell me is there any better structure i can use for my classes so that my all requirements will be fulfilled.
Or can i use some design pattern for this?

Thanks.

推荐答案

你的问题不是'圆形参考,它是圆形结构。



要创建A,你创建一个B和一个D.

创建一个B ,你创建一个A,然后......它会创建一个B,它会创建一个A,它... ...



最终你的应用程序崩溃堆栈。 />


你做不到。



我知道你给我们的代码只是一个粗略的例子,但无论你的真实代码在做什么,都无法做到。你需要更仔细地重新考虑你的项目之间的关系,因为一旦设计成循环结构依赖,它就会失败。



我们无法解决这个问题。您:我们无法访问您的正确代码或设计。





请说出A,B和A类C正在处理3个不同的数据库实体。

因此,为了在实体B中插入记录,我需要来自实体A和C的一些信息,基本上是获取调用。为此,我创建A的实例和C类。

D类仅用于记录目的,用于A,B和C类的每种方法。



I考虑选择在B的方法中创建A和C的实例,而不是在B的构造函数中创建。它解决了我的问题,但我仍然怀疑是否有更好的方法来做到这一点。




D类首先:如果它是一个日志记录服务,那么它只有一个 - 所以要么是静态类,要么是单例更合适:每次你想要使用它时都不需要创建一个新实例。



对于其他人,你可能希望存储在类中的实例: br />
Your problem isn't circular references, it's circular construction.

To create an A, you create a B, and a D.
To create a B, you create an A, and ... It creates a B, which creates an A, which...

And eventually your app crashes the stack.

You can't do that.

I know the code you gave us is only a rough example, but whatever your real code is doing, it can't do that. You need to rethink the relationships between your items more carefully, because as soon as design in a circular construction dependency, it will fail.

We can't fix that for you: we have no access to your "proper" code or design.


"Let say class A, B and C are handling 3 different database entities.
So for inserting a record in entity B, i need some information from entities A and C, basically "Get" calls. For this, i am creating instances of A and C classes.
Class D is solely for logging purpose which is used in every method of class A,B and C.

I considered option of creating instances of A and C in B's method rather then creating in B's constructor. It is solving my problem but still i doubt is there any better way to do it."


Class D first: if it's a logging service, then there is only one of it anyway - so either a static class or a singleton sounds more appropriate: no need to create a new instance each time you want to use it.

With the others, you probably want the instances stored in the classes:
public class A
   {
    private B b;
    public A() 
        {
        ...
        }
    }
public class B
   {
    private A a;
    public B() 
        {
        ...
        }
    }

但是您不应该在那里创建实例:如果数据存在,您应该执行Get调用,或者添加单独的构造函数以通过引用传递:

But you shouldn't be creating the instances there: Your "Get" calls should do if the data exists, or add separate constructors to pass the references through:

public class A
   {
    private B b;
    public A() 
        {
        b = new B(this);
        }
    }
public class B
   {
    private A a;
    public B() 
        {
        ...
        }
    public B(A a) 
        {
        this.a = a;
        ...
        }
    }



这与数据库表本身完全相同:你需要创建行TableA之前在TableB中创建引用它们作为外键的行。



这有意义吗?


It's exactly the same problem with the DB tables themselves: you need to create the rows in TableA before you create the rows in TableB that reference them as a foreign key.

Does that make sense?


之后描述我认为这将是最好的方式:

我认为你有一个List(T)。

现在你将一个Item添加到这个List。如果此项目也是List,则首先必须添加List,然后将requirer子项添加到子列表。

当然......这看起来非常复杂 - 但它对应于OOP。也许你可以创建自己的自定义列表,为你做一部分子工作......
After your description I think this would be the best way :
I think over it all you have a List (of T).
Now you add an Item to this List. If this Item is also a List you first have to add the List and then add the requirer Sub-Items to the Sub-List.
Of course ... this Looks very much more complicated - but it corresponds to OOP. Perhaps you could create your own customized Lists which does a part of the Sub-work for you ...


这篇关于循环类实例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆