Good day!
There was a question when designing classes of controllers for representations. For example, there is the following class hierarchy:
class IController { public: virtual void add(/*...*/) = 0; virtual void edit(/*...*/) = 0; virtual ~IController() = default; }; class StandartController : public IController { public: virtual void add(/*...*/) override; virtual void edit(/*...*/) override; } It is also necessary to provide an extended version of the controller:
class ExtentedController : public IController { public: virtual void add(/*...*/) override; virtual void edit(/*...*/) override; virtual void find(/*...*/); virtual void sort(/*...*/); // ... и другие методы } How now, if there is an extended child, is it uniform to use the interface? I thought to apply the solution with dynamic_cast, but it seems to me that in this case, its application will indicate that there are any problems with the architecture. The second option was to highlight the extended class in a completely new hierarchy. But does it make sense? Most likely there are better solutions. I ask for help :-)
In addition to the main question: For the production of controllers, a factory is used that returns only a pointer to an interface (IController *), regardless of what type of object the client needs. The signature of the receiving view method is as follows: void setController(IContoller* ctrl) . Representations, in fact, are also a kind of class hierarchy (One interface - many implementations).
class IView { public: IView() = delete; IView(/*...*/); ~IView() = default; //... void setController(IContoller* ctrl); //... другие методы }; When designing, the principle of "programming at the interface level" was used, but when implementations of "not like everyone else" began to appear, problems arose with their use. After all, the whole idea is known only about the controller interface. You can, of course, make a cast to the desired type in the submission, but again, I am so embarrassed by this operation that I am still looking for another solution.
findandsortappear only in the extended controller, so they can only be used where the extended controller is expected. You should not use them where a "controller at all" is expected ... Ie I, frankly, generally do not see any problem so far. - Harryvoid setController(IContoller* ctrl);. In this case, inside the receiving method, you will have to use dynamic_cast. I read that you need to use dynamic_cast carefully and where it belongs. Therefore, there are doubts about its use here. What if you need to add another 2 or 3 extended controllers? In the place of use add 2, 3 dynamic_cast? - Stevenvoid setController(IContoller* ctrl);methodvoid setController(IContoller* ctrl);can actually take any controller? Or do some of them expectExtentedController? - yrHeTaTeJlb