Browse DevX
Sign up for e-mail newsletters from DevX


Extending Behavior with the Visitor Pattern : Page 4

One common problem involves adding or extending behavior after—sometimes long after—you've implemented a class. A similar problem requires you to tack on behavior dependent on the features of a class but that doesn't belong to that class. Both problems have the Visitor pattern written all over them—and if you implement it, you'll find that the Visitor pattern provides excellent results.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Calculating Listing Days Remaining
To complete the example, a DaysRemaining class and a strongly typed collection of DaysRemaining makes it easier to calculate the days remaining in a listing by managing date spans as shown below.

Public Class DaysRemaining Private start, [end] As DateTime Public Sub New(ByVal start As DateTime, _ ByVal [end] As DateTime) Me.start = start Me.end = [end] End Sub Public ReadOnly Property Days() As Integer Get Return [end].Subtract(start).Days End Get End Property End Class Imports System.Collections Public Class DaysRemainingCollection Inherits CollectionBase Public Sub New() End Sub Default Public Property Item( _ ByVal index As Integer) As DaysRemaining Get Return CType(List(index), DaysRemaining) End Get Set(ByVal Value As DaysRemaining) List(index) = Value End Set End Property Public Function Add(ByVal value As DaysRemaining) _ As Integer Return list.Add(value) End Function Public Function add(ByVal start As DateTime, _ ByVal [end] As DateTime) As Integer Return add(New DaysRemaining(start, [end])) End Function Public Function GetAverageDaysRemaining() _ As Integer If (List.Count <= 0) Then Return 0 Return Convert.ToInt32( _ GetSumDaysRemaining() / List.Count) End Function Public Function GetSumDaysRemaining() As Integer Dim total As Integer = 0 Dim o As DaysRemaining For Each o In Me total += o.Days Next Return total End Function End Class

The intent of the Visitor behavior pattern is to permit you to define new operations on objects without changing the implementation of those objects. Visitor uses a double dispatch mechanism so that hosts only need to define a simple Accept method that calls a Visit method on the Visitor. You can define as many visitors in the future as you need without ever changing the definition of the hosts.

You can define the Visitor pattern using either abstract classes or interfaces. If you use abstract classes then the hosts will have a common base class. If you use interfaces then hosts can have different supertypes (base classes).

Paul Kimmel is the Chief Architect and founder of Software Conceptions, Inc. Paul is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming, including "Visual Basic .NET Power Coding" (Addison-Wesley) and his upcoming book "UML DeMystified" (McGraw-Hill/Osborne). Paul is the president and co-founder of the Greater Lansing area Users Group for .NET (www.glugnet.org) and a Microsoft Visual Developer MVP.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date