The visitor design pattern is a way of separating an algorithm from an object structure it operates on. That is official definition of it. I am trying to figure out how this doesn't break encapsulation. If say for example I have different types of classes for different types of bank accounts [Saving/Fixed/Current] implementing abstract class Account, should I put the calculate interest method as an abstract method in the abstract Account class or do I send the account type to the Visitor implementation and calculate it there?
Method 1: Should the Visitor implementation be responsible for calculating interest for the different account types?
public interface IInterestVisitor
{
void GetInterest(Savings AccountType);
void GetInterest(Fixed AccountType);
void GetInterest(Current AccountType);
}
Method 2: Or should the Account class's implementers do it?
public abstract class Account
{
public abstract void AcceptCalculateInterestVisitor(IInterestVisitor iv);
public abstract int CalculateInterestAmount();
}
If I use method 1, which is the visitor implementation that implements IInterestVisitor as above, then the job of calculating interest will be delegated to the visitor class. Using this approach, if I add another account type, then I will need to modify the visitor implementation everytime a new account comes along.
However, if I leave the interest calculation bit to the abstract Account classes implementations as above in method 2, then in my opinion [correct me if I am wrong here] I am not breaking encapsulation. In addition there is less code to modify as all I do is add a new class and have the visitor implement an interface like the one below
public interface IInterestVisitor
{
void GetInterest(Account AccountType);
}
public class InterestVisitor : IInterestVisitor
{
public void GetInterest(Account AccountType)
{
int i = AccountType.CalculateInterestAmount();
Console.WriteLine(i);
}
}
As you can see, there is no modification needed for the interest visitor class using method 2. Does method 1 break encapsulation? Can method 2 still be called a visitor pattern?
Thanks for reading...
The purpose of a Visitor pattern is to define a new operation without introducing the modifications to an existing object structure. Imagine that we have a composite object which consists of components.
Patterns That Provide Forms of Encapsulation Examples: JTable, JTree, JEditorPane.
Encapsulation is not just an Object Oriented Programming Principle, it is rather a general Software Design Principle which exists even in functional programming.
The visitor pattern consists of two parts: a method called Visit() which is implemented by the visitor and is called for every element in the data structure. visitable classes providing Accept() methods that accept a visitor.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.
In code provided I don't see that you need to change an object in order to fit your needs, what Visitor
basically does, is change object state by using provided Properties
/Methods
/Fields
of object itself.
So, by me your code can fit Visitor pattern, if that was actually question, also cause patterns are guidelines and not rigid rules.
I personally would chose second way, as it much more clear and OOP oriented.
Regards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With