Skip to main content

Calling same method on different instance (Polymorphism) [Resolved]

I have situation that 3 different instances with the same method signature are doing their job repeatedly.

interface IArgs{
    //args stuff
}

interface IExample{
    void Populate(IArgs);
}

class ExampleA : IExample
{
    void Populate(IArgs a){
        //todo
    }
}

class ExampleB : IExample
{
    void Populate(IArgs b){
        //todo
    }
}

class ExampleC : IExample
{
    void Populate(IArgs c){
        //todo
    }
}


class MainExampleClass(){
    ExampleA classA;
    ExampleB classB;
    ExampleC classC;
    //instantiation and other class stuff
    foreach(Data data in dataCollection){
        classA.Populate(data);
        classB.Populate(data);
        classC.Populate(data);
    }
}

It's worth to mention that the classes work with different args but I solve that with one dataholder class with all arguments and implementation of an interface (IArgs). So my question is: is there any good way, practice, pattern to solve this repeatedly calling of methods? The Factory pattern, if I understood, will return one instance, but here I need all 3. Any suggestions would be appreciated.


Question Credit: NSKBpro
Question Reference
Asked February 19, 2019
Posted Under: Programming
68 views
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA