0

There are 4 classes as mentioned below -

  • class A having 2 methods f1() and f2().
  • class B having method f3().
  • class C having method f4().
  • class D having method f5().

Now the following is true about the above classes -

  1. f1() calls f3().
  2. f3() calls f4().
  3. f4() calls f5().
  4. f5() calls f2().

Is it a bad design? If yes, how can this design be improved??

ash164
  • 231
  • 3
  • 9
  • it depends on what your classes are representing and what the methods are doing. In a good design, each class knows only the classes, that it needs. In other cases, use listeners and events. – Jarlik Stepsto Dec 22 '16 at 16:39
  • These are normal service classes in a mvc pattern. My doubt is that when class A is dependent on class D indirectly, then why class A have such code which class D requires? – ash164 Dec 22 '16 at 16:50
  • why not returning a value and call the function depending on the return: f1 calls f3 and waits for the response which depends on the other calls (f3->f4->f5) and then f1 calls f5, they are both in the same class – Jarlik Stepsto Dec 22 '16 at 16:52
  • Some references on cyclic dependencies: http://stackoverflow.com/questions/37444940/spot-problems-with-circular-dependency/37445480#37445480 – jaco0646 Dec 22 '16 at 21:39

2 Answers2

0

It is case dependent, but it most cases, you should avoid such designs.

We need more information about the relationship between the classes. I think, that you could resolf this situation by using the observer pattern or using return statements.

Example:

A contains B, C, D => in that case, B, C and D should not know each other

A contains B
B contains C
C contains D
D should not know A or B. In most cases D should not even know C.

MVC Pattern: The Model contains raw data and do not need to call functions of the View or the Controller. If you need to observe the model for changes, use the obersver pattern vor this (event listener).

The View knows how to display the data and in most cases it uses the observer pattern to get notified, when the model changes.

Each Controller knows his View and cann call method of this view. The Controller also knows the model and can change the data.

M does not contain direct references to V and C V can be an observer of M and it is possible, that V knows C C can call methods of M and of V in most cases V is calling C and C is calling M.

Jarlik Stepsto
  • 1,667
  • 13
  • 18
  • Thanks Jarlik for your response. But all these classes are only services. I have got the answer in the link mentioned by jaco0646. – ash164 Dec 23 '16 at 02:26
  • In the example given by you, my case is the second case where A contains B, B contains C, C contains D and D contains A referenve. – ash164 Dec 23 '16 at 02:42
0

On the basis of the link shared by jaco0646 in the comments section, I got following solution. If anyone have different ideas, do share.

Such cyclic relationship requires constant awareness. Most cyclic dependencies can be refactored in this way:

1) In above scenario, we have classes A and D depending on each other, pick any class,

2) Identify the methods in this class that both A and D depend on i.e. in this case method f2()

3) extract these methods into a new class E,

4) make both A and D depend on E,

5) one of classes A and E will still depend on the other, but the cyclic dependency will have been eliminated.

ash164
  • 231
  • 3
  • 9