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

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.

主站蜘蛛池模板: 隆林| 金堂县| 吕梁市| 元朗区| 云林县| 涿鹿县| 湾仔区| 贵州省| 钟山县| 万年县| 赣州市| 明水县| 四平市| 琼中| 靖西县| 阿拉善盟| 乐东| 浦城县| 双城市| 大方县| 揭阳市| 安泽县| 盐边县| 灵台县| 哈尔滨市| 东乌珠穆沁旗| 教育| 无为县| 玉溪市| 凤城市| 灌南县| 河南省| 马关县| 青海省| 东城区| 博兴县| 大英县| 中西区| 竹北市| 广南县| 武威市|