0

我一直坚持避免使用静态方法。但是我也一直在努力使我的代码尽可能地可读和简短。

我有一个代理列表,对于所有代理,我应该执行 5 种不同的验证。

对我来说,这些验证是如此不同,以至于我不想把它们放在一个大的验证类中。特别是如果验证将被扩展,这个类可能会变得很大。

现在在这种情况下,我可以这样编写我的验证器类:

Public Class MyValidator1
Public Shared Sub Validate(proxy As ServiceClient, year As Integer)
    Dim args = New Arguments()
    args.Year= year 
    Try
        Console.WriteLine("Test")
        Console.WriteLine("--------------")
        Dim result = proxy.GeneratReport(args)
        Console.WriteLine("No errors")
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    Finally
        Console.WriteLine()
    End Try
End Sub
End Class

这是它的实现方式:

For Each proxy In _proxies
        Console.WriteLine(proxy.Endpoint.Address.Uri.Host)
        Console.WriteLine("------------------------")
        MyValidator1.Validate(proxy.Value, jaar)
        MyValidator2.Validate(proxy.Value, jaar)
        MyValidator3.Validate(proxy.Value, jaar)
        MyValidator4.Validate(proxy.Value, jaar)
        MyValidator5.Validate(proxy.Value, jaar)
    Next

我当然可以使我的方法非静态/共享,但这需要我为每个验证器创建一个实例。

每个选择的优缺点是什么。对我来说,使用静态实现更容易阅读。

4

1 回答 1

0

1)If your methods are not dependent on InstanceVariable you can make them static, and in your case since you are looking towards making them all static, Static Class maybe of your interest.

2)If your methods are not logically grouped better to have them in seperate classes, but if its only about your class growing and becoming less managable you can consider thinking of partial class for the same.

于 2013-08-13T09:10:05.903 回答