rkbkgk Posted September 18, 2021 Report Share Posted September 18, 2021 Hi, I made a small example for delegation pattern. hope this is the right way to teach the delegation pattern. suggestions & corrections are welcomed...... duck delegate patern.rar Quote Link to comment
ak_nz Posted September 20, 2021 Report Share Posted September 20, 2021 Personally I don't think of delegation as a pattern, just a natural consequence of encapsulation. In your example, you've included demonstrating polymorphism and delegation. If you want to focus only on delegation, I would probably make some adjustments: Two classes - "Duck" and "QuakingFunction". Duck class contains a QuakingFunction object. It has a 'constructor' or method to set the required object for that task (dependency injection). Duck class exposes the static "Quak" method. This method internally calls QuakingFunction.Quak() method of the object (your delegation). Note - if you make the QuakingFunction.Quak() method virtual, it lets you implement new child classes in the future that Quak differently (Strategy pattern). You inject the one you want at the time. You don't need the derived Duck classes at all in order to demonstrate delegation. The Duck.Quak() method can of course be called with the child class objects so that they too can quack .. like a duck (Inheritance). Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.