Visitor only applicable when using the Composite pattern?

For a long time I’ve tried to get my head wrapped around the visitor pattern, and somehow this thing keeps being rather fuzzy to me.

I’m currently under the impression it is only useful to apply operations on objects that implement the Composite pattern. At least as far as PHP is concerned. Is that an accurate observation, or am I missing something?

I’m in the process of reimplementing some functionality, and this includes computing a diff between two Entities. These Entities contain various value objects. Different derivatives in the type hierarchy of these Entities have different value objects. Originally the diff code was contained in the entities themselves, though this caused quite some clutter, so I’d rather move it out into dedicated service objects. The Visitor pattern sprung to mind, though I do not see how I can actually sanely apply it. Would all the value objects need to implement some EntityElement interface? That seems bad. Is this a case where the Visitor pattern indeed does not apply, or am I simply failing to see how it would be applied nicely here?

1

In a strongly-typed language, Visitor is useful because of “double-dispatch”. Two objects of different types can interact without knowing what their types are. So it solves the “handshake problem”: if I have a bunch of known types, I can have every type in the bunch shake hands with every other type. The magic it does to do that is double-dispatch: I call you, passing along important information without knowing your type, and then you call me back, adding your own information. Now everyone has contributed their part without revealing what type they are.

In a dynamically-typed language, I agree it doesn’t seem like there’s much point. There are simpler ways to traverse an object structure.

In a dynamic language, you might end up with a simplified version of Visitor that leaves out the double-dispatch.

In the case you mentioned, diff is a natural candidate for Visitor, because it has that “handshake problem”: you have a bunch of types, and you want to diff any two of them. The simplification is that each class just needs to have a diff(other) method and a diff(value) method. The trick is that value doesn’t have to be an integer — it could be a more complex type. diff(other) would just be return other.diff(my_value). That’s the double-dispatch part, which does the trick of hiding the internals of the class.

The only other pattern that comes to mind would be a “parametric polymorphism” soultion. You have a Diff class, and each class you have returns has a Diff diff() method. The Diff class contains whatever information you need to do the diff — maybe just an int — and its a common class that takes the specific types out of the equation. So it would look something like

Diff d = a.diff().apply(b.diff())

If Diff really does just contain some value, then what you’re really doing is putting the different objects on a number line, and you could just have that value as a property. E.g. a.scale() or a.rank()

doOperation() = accept(), Element = Component

The difference is in the ObjectStructure in diagram 2. The Visitor Pattern does not require the ObjectStructure to implement the << interface >> Component. (Read below)

  1. Composite Pattern

  2. Visitor Pattern

Often it is useful to let the ObjectStructure implement the Component interface in case you have a tree or other nested collections.

Sometimes you do not need an ObjectStructure to implement the Component interface. If the ObjectStructure is a simple collection, such as an array or a list, you know how to iterate over it. No need to add an accept() method to the ObjectStructure. It is the starting point of your visit.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Visitor only applicable when using the Composite pattern?

For a long time I’ve tried to get my head wrapped around the visitor pattern, and somehow this thing keeps being rather fuzzy to me.

I’m currently under the impression it is only useful to apply operations on objects that implement the Composite pattern. At least as far as PHP is concerned. Is that an accurate observation, or am I missing something?

I’m in the process of reimplementing some functionality, and this includes computing a diff between two Entities. These Entities contain various value objects. Different derivatives in the type hierarchy of these Entities have different value objects. Originally the diff code was contained in the entities themselves, though this caused quite some clutter, so I’d rather move it out into dedicated service objects. The Visitor pattern sprung to mind, though I do not see how I can actually sanely apply it. Would all the value objects need to implement some EntityElement interface? That seems bad. Is this a case where the Visitor pattern indeed does not apply, or am I simply failing to see how it would be applied nicely here?

1

In a strongly-typed language, Visitor is useful because of “double-dispatch”. Two objects of different types can interact without knowing what their types are. So it solves the “handshake problem”: if I have a bunch of known types, I can have every type in the bunch shake hands with every other type. The magic it does to do that is double-dispatch: I call you, passing along important information without knowing your type, and then you call me back, adding your own information. Now everyone has contributed their part without revealing what type they are.

In a dynamically-typed language, I agree it doesn’t seem like there’s much point. There are simpler ways to traverse an object structure.

In a dynamic language, you might end up with a simplified version of Visitor that leaves out the double-dispatch.

In the case you mentioned, diff is a natural candidate for Visitor, because it has that “handshake problem”: you have a bunch of types, and you want to diff any two of them. The simplification is that each class just needs to have a diff(other) method and a diff(value) method. The trick is that value doesn’t have to be an integer — it could be a more complex type. diff(other) would just be return other.diff(my_value). That’s the double-dispatch part, which does the trick of hiding the internals of the class.

The only other pattern that comes to mind would be a “parametric polymorphism” soultion. You have a Diff class, and each class you have returns has a Diff diff() method. The Diff class contains whatever information you need to do the diff — maybe just an int — and its a common class that takes the specific types out of the equation. So it would look something like

Diff d = a.diff().apply(b.diff())

If Diff really does just contain some value, then what you’re really doing is putting the different objects on a number line, and you could just have that value as a property. E.g. a.scale() or a.rank()

doOperation() = accept(), Element = Component

The difference is in the ObjectStructure in diagram 2. The Visitor Pattern does not require the ObjectStructure to implement the << interface >> Component. (Read below)

  1. Composite Pattern

  2. Visitor Pattern

Often it is useful to let the ObjectStructure implement the Component interface in case you have a tree or other nested collections.

Sometimes you do not need an ObjectStructure to implement the Component interface. If the ObjectStructure is a simple collection, such as an array or a list, you know how to iterate over it. No need to add an accept() method to the ObjectStructure. It is the starting point of your visit.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật