I have an interface I with two methods func A and func B and a class C with the implementation of the interface, I have two users U1 and U2.
I want the functionality so that if u1 accesses class C, func A should be called and if u2 accesses class C func B should be called.
How do i implement this using OOPs ?
I have an interface I with two methods func A and func B and
Share
I will focus on what you’ve asked. Why you would want to do this and whether it’s actually a good idea with respect to OO design principles is another question.
First, your requirement would look similar to the following: (note)
Now, let’s create a
Userrole and two implementations for user 1 and user 2:Finally, instantiate your users 1 and 2 as follows:
And “access” your C as follows:
Note that this code almost completely leaves class
Cout of the game and instead focuses on the interface (I). If, however, you want your code to work specifically withC, simply replaceIwithCin the appropriate place (namely, with the parameter of methodAccess).(note:) I’ve chosen C# for the examples, but translating to the language of your choice should be easy.