skip to Main Content

Welcome

Say hello to the toggle bar. This is an optional section you can use to display any content you'd like. Simply select a page from the theme panel and the content of the page will display here. You can even use the drag and drop builder to create this! This is a perfect place for your company mission statement, alerts, notices or anything else.

Get In Touch

Email: support@total.com
Phone: 1-800-Total-Theme
Address: Las Vegas, Nevada

Our Location

togglebar-map

Visitor Pattern

Post Series: Behavioral Design Pattern

This pattern is used to create and perform new operations onto a set of objects without changing the object structure or classes. This pattern enables loose coupling and addition of new operations without changing the existing structure.

Visitor Design Pattern C#

  • Client: This is a class that has access to the data structure objects and can instruct them to accept a Visitor to perform the appropriate operations.
  • ObjectStructure: This is class that holds all the elements which can be used by visitors.
  • Element: This is an interface that specify the Accept operation.
  • ConcreteElementA/B: These are classes which implement the Element interface and holds the real information.
  • Visitor: This is an interface that specify the Visit operations for concrete visitors.
  • ConcreteVisitorA/B: These are sub classes which implement the Visitor interface.

Implement

Example

 

https://viblo.asia/p/tim-hieu-visitor-pattern-qua-vi-du-ZabG9zedvzY6

Có thể thấy, việc thêm một Monster mới rất đơn giản, chỉ cần implement các method của Monster thôi. Trong khi, nếu thêm Human thì ta phải thay đổi cả interface Monster và cập nhật các class implement của Monster.

Chính vì vậy, tùy vào trường hợp cụ thể, xem các đối tượng thuộc loại nào hay bị thay đổi mà cài đặt Visitor Patern cho phù hợp.

When to use it?

  1. An object structure has many unrelated operations to perform on it.
  2. An object structure cannot change but you need to perform new operations on it.
  3. The operations need to perform on the concrete classes of an object structure.

Resource

http://www.dotnettricks.com/learn/designpatterns/visitor-design-pattern-c-sharp

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top