官术网_书友最值得收藏!

Understanding the visitor pattern

The visitor pattern is what is known as a behavioral pattern. The term behavioral pattern is simply a classification of a group of patterns that are concerned with the way that classes and objects communicate. What the visitor pattern gives us is the ability to separate an algorithm from the object that the algorithm works on. This sounds a lot more complicated than it really is.

One of the motivations behind us using the visitor pattern is that we want to take the common ParseElement class and apply different operations on it, depending on what the underlying markdown is, which ultimately leads to us building up the MarkdownDocument class. The idea here is that if the content the user types in is something we would represent in HTML as a paragraph, we want to add different tags to those used, for example, when the content represents a horizontal rule. The convention for the visitor pattern is that we have two interfaces, IVisitor and IVisitable. At their most basic, these interfaces look like this:

interface IVisitor {
Visit(......);
}
interface IVisitable {
Accept(IVisitor, .....);
}

The idea behind these interfaces is that the object will be visitable, so when it needs to perform the relevant operations, it accepts the visitor so that it can visit the object.

主站蜘蛛池模板: 泸溪县| 孟州市| 城市| 长汀县| 巨野县| 霍林郭勒市| 当阳市| 安阳市| 黄骅市| 梓潼县| 玉山县| 彩票| 苏州市| 正安县| 迁安市| 祁阳县| 体育| 民权县| 林周县| 谷城县| 庆安县| 昌乐县| 上林县| 平罗县| 苏尼特左旗| 博爱县| 靖安县| 石渠县| 噶尔县| 崇礼县| 馆陶县| 龙江县| 怀宁县| 嘉善县| 瑞金市| 璧山县| 西峡县| 鸡东县| 苗栗县| 土默特左旗| 门头沟区|