Вопрос

I have three classes in my project. Lets call them MainWindow , ProcessUserInput, InitialUIPreparer .

MainWindow's job is just to dialog with user via buttons,text,combobox etc.. ,

ProcessUserInput will make some calculation with value which are taken from MainWindow and after calc. finishes it will send back some processed data to MainWindow,

and also InitialUIPreparer will calculate the places of some shapes which should be drawn on MainWindow, it will just pass cordinates not draw them. But it also need to get some data like window size etc..

Ok problem starts like MainWindow need to get data from each class and classes needs to get data from MainWindow . This leads circular dependency. I solve the problem by including MainWindow.h to "ProcessUserInput" and "InitialUIPreparer" . But just using forward decleration in MainWindow like "class ProcessUserInput" and "class InitialUIPreparer".

I can still continue development ofcourse. But like in my case needing forward decleration many times, indicates bad design or not, should I reconsider the design for future projects?

Это было полезно?

Решение

In general, forward-declaring a class by itself does not indicate a bad design: this happens very often when your runtime dependency hierarchy is bidirectional. However, when the dependency graph is very dense (i.e. nearly every class depends on nearly all other classes) it is a valid reason to start worrying:

Circular dependency

In your case it appears that you may benefit from separating your classes in a way consistent with the Model-View-Controller design pattern. Specifically, you may want to introduce another class, let's call it Model, that would keep all information that needs to be shared among your other classes. Each class would pull the information that it needs from the shared Model, and put the information that it produces into the model as well. This way the individual classes would be able to replace dependencies on all classes with a single dependency on the Model class:

Model dependency

Другие советы

The mere presence of forward-declarations is not, in itself, a code smell. However the reason for the need for the forward-declarations can be.

In your case you need the forward-declarations in order to construct a circular dependancy. That is your code smell. The presence of a circular dependancy is always a code smell -- which is not to say that it is always a design flaw, but it is something that should be looked at.

Especially in small programs, circular dependencies may in fact be a theoretic design flaw, but one which might not be worthwhile to fix. In your case you have two classes which communicate directly with each other. In a large, production system I would usually categorize this design as "broken; needs to be fixed." The way I would fix it would be to introduce some kind of message bus, or a sink/provider mechanism separate from both of the classes, and have each class communicate with that instead of each other. Building this system either involves expending a lot of work or a lot of money (to purchase one), and opens avenues to a whole slew of new bugs and challenges. In the case of small systems, I likely wouldn't even bother.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top