- Advanced TypeScript Programming Projects
- Peter O'Hanlon
- 224字
- 2021-06-24 13:27:10
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.
- 樂(lè)學(xué)Windows操作系統(tǒng)
- Linux內(nèi)核完全注釋?zhuān)?0周年版·第2版)
- 蘋(píng)果電腦玩全攻略 OS X 10.8 Mountain Lion
- 精解Windows 8
- Instant Handlebars.js
- Docker+Kubernetes應(yīng)用開(kāi)發(fā)與快速上云
- Windows Server 2019 Administration Fundamentals
- Linux使用和管理指南:從云原生到可觀測(cè)性
- Application Development in iOS 7
- 計(jì)算機(jī)系統(tǒng):基于x86+Linux平臺(tái)
- Cassandra 3.x High Availability(Second Edition)
- Python UNIX和Linux系統(tǒng)管理指南
- 分布式高可用架構(gòu)之道
- Multi-Cloud for Architects
- bash shell腳本編程經(jīng)典實(shí)例(第2版)