문제

Let's say I have a visitor interface like this:

''' <remarks>Visitor Pattern</remarks>
Public Interface IVisitor
    Sub Visit(value As Type1)
    Sub Visit(value As Type2)
    Sub Visit(value As Type3)
    Sub Visit(value As Type4)
    Sub Visit(value As ...)
    ...
End Interface

Is there a way for my concrete visitors to avoid implementing Visit functions they don't need? I'd like to avoid doing stuff like this in my concrete visitors:

#Region "Methods not implemented (not needed)"

    Public Sub Visit(value As Type4) Implements IVisitor.Visit

    End Sub

    ...

I know that I can't use the Overridable keyword in Interface. So maybe I should just drop Interface for Overridable Sub in my base class?

도움이 되었습니까?

해결책

You can use a base class (may be marked by MustInherit, but not a must) which define a default implementation for all the methods. Every concrete Visitor will inherit the base class and override only the methods it needs to.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top