+ 1

Why apply method is not displaying any output

Hi I tried to implement visitor pattern as below: https://www.sololearn.com/compiler-playground/ciTEY27lLC0h What am I missing as I am not getting output of apply method . I am aware that apply method in notification class is empty, but I do have this virtual method implemented in derived class mockdrill. Is this not correct? https://code.sololearn.com/ciTEY27lLC0h/?ref=app https://code.sololearn.com/cG2nOjiPrtPo/?ref=app

16th Sep 2023, 8:38 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Réponses
+ 1
ofcourse Ketan visitor pattern do solve this kind of double dispatch issue. and you don't need to explicitly cast your pointers, but In your implementation you have a simplified version of the visitor pattern. In the visitor patt, each subclass of the element class ' in this case the employee types' has its own accept method that takes a visitor. The visitor will have specific visit methods for each employee thpe. This way the correct visit method is called based on the actual type of the employee, I made a little modifs in you code, check it and told me if I'm messtaking somewhere: https://code.sololearn.com/c46xcU4c3MHM/?ref=app
16th Sep 2023, 6:37 PM
Amine Laaboudi
Amine Laaboudi - avatar
+ 1
Hi👋, I think cuz the way you are trying to call "apply" for different emplyee types in ur "McDrill" class. when you use "this->shared_from_this()" to get a shared pointer to the derived class 'Developer, QA, ITEngg', it returns a 'shared_ptr<IEmployee>' instead of a shared_ptr<Deve..,QA>. So, the appropriate 'apply'' method in ''MockDrill'' is not being called based on the derived type. my suggestion is to use maybe dynamic casting to cast the 'IEmployee' shared pointer to the appropriate derived type before calling apply method in MockDrill : class MocDrill : public INotification { public : void apply(shared_ptr<IEmployee> ptrEmployee) { if(auto devEmployee = dynamic_pointer_cast<Developer> (ptrEmployee)) apply(devEmployee); .... private : void apply(shared_ptr<Developer> ptrEmployee) { .... } Still not sure, but this is how I see it
16th Sep 2023, 2:57 PM
Amine Laaboudi
Amine Laaboudi - avatar
+ 1
Thanks Amine Laaboudi. I got idea on what I was missing and corrected version is below taking idea from your code reference. https://www.sololearn.com/compiler-playground/cG2nOjiPrtPo I got fair idea about visitor now but I am not going to be a fan of this pattern it seems. It violates dependency inversion principle. Isn't it? My visitor (notification) depends on details (developer, it or QA) and not the interface (employee). What's benefit of such a dependency ? If I will get new class like Project Manager, visitor will force me to modify. Isn't that modifying developer only without visitor is better option? Again thanks a lot for your help to make me understand visitor pattern and have a working code.
17th Sep 2023, 6:30 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan, yes in some cases it does ''violates'' the DIP, the visitor depends on concrete elements like Developers, QA,.. instead of interfaces (e.g IEmployee), which can make it less flexible and harder to extend when new concrete element are introduced. But it will be ''helpful'' let say, when you have a well defined set of elements and well defined set of operation, when you want to separate the algorithms from the elements, and when you want to add new algorithms (visitors) without modifying the elements. Still in many cases using polymorphism directly without the visitor pattern may indeed be a simpler and more flexible solution.
17th Sep 2023, 3:48 PM
Amine Laaboudi
Amine Laaboudi - avatar
+ 1
17th Sep 2023, 5:25 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
with pleasure my friend
17th Sep 2023, 5:26 PM
Amine Laaboudi
Amine Laaboudi - avatar
0
Thanks Amine Laaboudi for the analysis. But if that is the case, isnt it a problem which visitor pattern solve ? What is the point of casting all the pointer type taking Iemployee pointer ? I am still confused whether visitor pattern i tried implementing is correct or not ?
16th Sep 2023, 4:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar