我有一个代码片段,如下所示。我如何重构它以使用委托?是否值得重新编写此代码以使用委托?我认为这是可以使用代表的场景我错了吗?我正在尝试学习代表并希望看到它们的使用。
public class Program
{
public static void Main(string[] args)
{
var count = Int32.Parse(Console.ReadLine());
Console.Write("Logger Type -->");
var logType = Console.ReadLine();
if (logType == "A")
{
if (count > 10)
{
LoggerTypeA.Error(count);
}
else
{
LoggerTypeA.Warning(count);
}
}
else
{
if (count > 10)
{
LoggerTypeB.Error(count);
}
else
{
LoggerTypeB.Warning(count);
}
}
Console.ReadLine();
}
}
internal static class LoggerTypeA
{
public static void Error(int count)
{
Console.WriteLine("Error {0} from Logger A", count);
}
public static void Warning(int counter)
{
Console.WriteLine("Warning {0} from Logger A", counter);
}
}
internal static class LoggerTypeB
{
public static void Error(int count)
{
Console.WriteLine("Error {0} from Logger B", count);
}
public static void Warning(int counter)
{
Console.WriteLine("Warning {0} from Logger ", counter);
}
}