- 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.
- Learning OpenDaylight
- Mobile-first Bootstrap
- 曝光:Linux企業運維實戰
- Docker+Kubernetes應用開發與快速上云
- macOS效率手冊
- 網絡操作系統教程:Windows Server 2016管理與配置
- Windows 7案例教程
- Linux內核觀測技術BPF
- 計算機應用基礎(Windows 7+Office 2016)
- 分布式高可用架構之道
- UI設計手繪表現從入門到精通
- OpenStack Essentials(Second Edition)
- Gradle Effective Implementations Guide(Second Edition)
- 計算機系統平臺
- OpenSolaris系統管理