Search code examples
c#interfacepolymorphismimplementationopen-closed-principle

looking how to implemant simple polymorphism


I have verybasic question ( i dont know why i unable to think stright ).

I am trying to do some polymorphism .

I have interface look like this:

Public interface Iplugin
{

   void doSomthing(string _str);
}

I also have some plugins classes that implament this interface

public class plugin1:Iplugin
{
    void doSomthing(string _str)
    {
        if (_str=="1")
        {
           // do somthing 1
        }
     }
 }

 public class plugin2:Iplugin
{
    void doSomthing(string _str)
    {
        if (_str=="2")
        {
           // do somthing 2
        }
     }
 }

public class plugin3:Iplugin
{
    void doSomthing(string _str)
    {
        if (_str=="3")
        {
           // do somthing 3
        }
     }
 }

So I have the main class and I would like it to invoke the all plug in

BUT I would like it to save the OCP (Open-Closed Principle) so If I will add in the future another plugin class the main class will not changed.

This is the main class

public class mainApp
{
  Iplugin _context;
  mainApp()
  {
     _context= new //new what??
  }
  bool func(string str)
  {
      _context.doSomthing(str);//I would like it to invoke all the plug in and also a future plugins that I will add
  }
} 

Solution

  • Ok Thank you all for your help.

    I take all your advice and I did the following:

     namespace Plugins
    {
    
    
    
    
     public class plugin1 : Iplugin
      {
        void doSomthing(string _str)
        {
            if (_str == "1")
            {
                // do somthing 1
            }
        }
    
    
    }
    
    public class plugin2 : Iplugin
    {
        void doSomthing(string _str)
        {
            if (_str == "2")
            {
                // do somthing 2
            }
        }
    
    
    }
    
    public class plugin3 : Iplugin
    {
        void doSomthing(string _str)
        {
            if (_str == "3")
            {
                // do somthing 3
            }
        }
    
    
     }
    }
    

    SO this is name space with all the plugins

    now in the main app

    namespace Factory
    {
      public interface Iplugin
      {
    
        void doSomthing(string _str);
      }
    
    
    
    class Program
    {
        static void Main(string[] args)
        {
            string @namespace = "Plugins";
    
            var q = from t in Assembly.GetExecutingAssembly().GetTypes()
                    where t.IsClass && t.Namespace == @namespace
                    select t;
            q.ToList();
    
            List<Iplugin> myList = new List<Iplugin>();
            foreach (var item in q)
            {
                Iplugin temp=(Iplugin)Activator.CreateInstance(item);
                myList.Add(temp);// a list with all my plugins
    
            }
    
            foreach (var item in myList)
            {
               item.doSomthing("string");
    
            }
    
        }
    }
    

    }