1

I have to find and solve a problem containing a circular dependency where I either store opaque pointers between the objects involved or use a separate class on a higher level to realize the relationship between the objects.

So I figured a simple problem where I have two classes, bank and account should do. The bank contains a list with accounts and the account contains a pointer to its bank. Circular dependency achieved.

But there is another condition which I have to satisfy which is that I have to make sure that the classes and the relationship between them can be tested independent of eachother.

The Bank class uses functions which do stuff on accounts, for example transfer funds between them, withdraw or add funds. And the account contains similar functions which edits its variables.

Testing the account class is easy as instantiating the class and test the functions but how can I test a class which depends on another class independent from the dependency? And how do you test a relationship between two classes?

I'm having trouble finding information on circular dependencies other than that you should avoid them but in larger projects they can be hard to avoid.

4

2 回答 2

0

重要的问题是为什么账户需要知道哪个银行拥有它?很难想象在需要该信息的帐户上的操作作为涉及银行的操作不会更好地完成。例如,将资金从一个账户转移到另一个账户通常需要银行 A 从账户 AA 中取出资金,然后将其交给银行 B,然后银行 B 再将其添加到账户 BB。

也许您需要删除依赖关系,然后找出没有它什么是“不可能的”,并重新考虑您要做什么,并可能根据银行对帐户做某事而不是帐户对银行做某事来重新考虑它。

于 2012-08-17T15:09:00.370 回答
0

考虑编写精简版帐户

例如

class Account
{
   public:
      bool failDebit;
      bool Debit(unsigned int quid) { return failDebit;}
}

然后,您可以通过为这两种情况Bank设置failDebitfirst 来测试您的课程。Bank然后,如果您有一个更复杂的函数,它使用多个 函数并使用类似的方法Account测试该类的所有可能性,那么您可以使其更复杂。Bank该类Account可以非常简单,只是模仿返回值。

注意这有点简单,因为可能需要考虑各种函数调用之间的依赖关系(不是双关语)。

于 2012-08-17T15:13:22.480 回答