Difference between Visitor and Composite Pattern?
design-patterns
Solution
They are different patterns, but they are often used together.
The point of composite is to apply the same operation to a bunch of elements that share an interface. The point of visitor is to extend a bunch of elements with a new operation without changing their implementation nor the caller's implementation. Therefore you often see:
Composite c = new Composite();
Visitor v = new ConcreteVisitor();
c.visit(v);
This way you can keep the implementation of the composite and the classes that are in the composite static, and only vary the kind of Visitor you apply to them.
Problem
I am not seeing any difference. Looks like both Patterns are trying to compose objects. Can anyone explain the intentions behind these two patterns?