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

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.

主站蜘蛛池模板: 伊吾县| 德格县| 宝坻区| 兴义市| 内江市| 卓资县| 崇义县| 仙桃市| 江达县| 郧西县| 常熟市| 论坛| 盈江县| 洪雅县| 洛南县| 通江县| 江山市| 彰化市| 清涧县| 四子王旗| 泉州市| 石城县| 枣强县| 梧州市| 卓资县| 花莲县| 佛坪县| 佛教| 新巴尔虎右旗| 西平县| 宣汉县| 镇平县| 徐闻县| 丽江市| 奉新县| 汤原县| 淮南市| 会理县| 北川| 京山县| 都兰县|