Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extending Behavior with the Visitor Pattern : Page 2

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.


advertisement
The Abstract Visitor Class
The examples in the upcoming listing separate out persistence and the user interface to permit us to focus on the implementation of the user pattern. The following class is an implementation of the abstract Host class:

' The abstract Host class. Namespace Visitor_VB.Patterns Public MustInherit Class Host Public MustOverride Sub Accept(ByVal visitor _ As Visitor) End Class End Namespace

And here's an implementation of an abstract Visitor class.

' The abstract Visitor class. Public MustInherit Class Visitor Public MustOverride Sub Visit(ByVal host As Job) Public MustOverride Sub Visit( _ ByVal host As [Resume]) Public MustOverride Sub Visit( _ ByVal host As Advertisement) Public MustOverride Sub Visit(ByVal host As Listing) End Class

Author's Note: In the preceding code, the Resume class is shown as [Resume] to distinguish the class name from the Resume reserved word in VB.NET.

Next, simply define your host classes as subclasses of Host and define as many kinds of Visitor classes as you'd like and code the visitor classes to perform whatever operations you'd like.

The abstract Visitor class as coded above can visit Job, Resume, Listing, and Advertisement classes, so you need to define each of those classes to inherit from Host and override the Accept method. Keeping in mind that the code here doesn't deal with persistence, Listing 1 shows a possible implementation of the Listing class. Note that this implementation used abstract classes for Host and Visitor rather than interfaces. .NET doesn't support multiple inheritance, which results in a nested inheritance hierarchy with Host as the base class which is inherited by Listing, which is in turn inherited by each of Job, Resume, and Advertisement.

The Job, Resume, and Advertisement classes shown below inherit Host indirectly by inheriting from Listing, and each has its own implementation of the Accept method and the UpdateExpirationDate method defined in Listing.


Option Explicit On Option Strict On ' If you place the classes below in separate files, ' repeat the preceding two lines in each file. Public Class Job Inherits Listing Public Sub New() MyBase.Content = "This is a job listing" End Sub Public Overrides Sub Accept( _ ByVal visitor As Visitor_VB.Patterns.Visitor) visitor.Visit(Me) End Sub Protected Overrides Sub UpdateExpirationDate() MyBase.ExpirationDate = _ MyBase.PostedDate.AddDays(30) End Sub End Class Public Class [Resume] Inherits Listing Public Sub New() MyBase.Content = "This is a resume" End Sub Public Overrides Sub Accept( _ ByVal visitor As Visitor_VB.Patterns.Visitor) visitor.Visit(Me) End Sub Protected Overrides Sub UpdateExpirationDate() MyBase.ExpirationDate = _ MyBase.PostedDate.AddDays(365) End Sub End Class Public Class Advertisement Inherits Listing Private FDaysPurchased As Integer Public Sub New() DaysPurchased = 60 MyBase.Content = "This is an advertisement" End Sub Public Overrides Sub Accept( _ ByVal visitor As Visitor_VB.Patterns.Visitor) visitor.Visit(Me) End Sub Protected Overrides Sub UpdateExpirationDate() MyBase.ExpirationDate = _ MyBase.PostedDate.AddDays(DaysPurchased) End Sub Public Property DaysPurchased() As Integer Get Return FDaysPurchased End Get Set(ByVal Value As Integer) FDaysPurchased = Value UpdateExpirationDate() End Set End Property End Class





Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap