Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visitor Pattern and Encapsulation

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...

like image 240
user20358 Avatar asked Aug 14 '11 20:08

user20358


People also ask

What is the visitor pattern used for?

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.

Which design pattern uses encapsulation?

Patterns That Provide Forms of Encapsulation Examples: JTable, JTree, JEditorPane.

Is Encapsulation a design pattern?

Encapsulation is not just an Object Oriented Programming Principle, it is rather a general Software Design Principle which exists even in functional programming.

How does the visitor design pattern work?

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.


1 Answers

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.

like image 95
Tigran Avatar answered Oct 13 '22 00:10

Tigran