We have this hypothetical reporting system where 2 entities are involved Report Generator & Report.
- Report generator would have a single behavior.
- It should take 2 inputs as integers.
- It Returns a string back as an output which is basically the report presentation.
(So, basically above 2 point forms the contract that need to be fulfilled by any report generator)
- Report should take report generator object or report generation logic in the form of function as an input.
- Give call to the report generator by passing input1 & input2.
- Get the string Output back from generator.
- Will have the functionality to write the Output to console or file system.
Whenever, I introduce a new Report Generator, neither Report entity nor existing Report Generator's, should be modified.
Take a pause
Think about the solution
Scroll down for the solution
Run the below code in Linqpad (https://www.linqpad.net/), to see it working.
Generally every one understands the theoretical aspect of Delegates and Interfaces, but when it comes to actual use of the same, beginner's lacks the understanding of when such constructs need be used and in which scenario. This problem based illustration might be of some help to understand the concept better.
Happy Coding !!