私は非常に基本的な質問があります (なぜ私は正しく考えることができないのかわかりません)。
私はいくつかのポリモーフィズムをやろうとしています。
私は次のようなインターフェースを持っています:
Public interface Iplugin
{
void doSomthing(string _str);
}
このインターフェースを実装するプラグインクラスもいくつかあります
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
}
}
}
メインクラスがあり、すべてのプラグインを呼び出したい
しかし、OCP (Open-Closed Principle) を保存したいので、将来別のプラグイン クラスを追加しても、メイン クラスは変更されません。
これがメインクラス
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
}
}