- 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.
- 阿里云數字新基建系列:云原生操作系統Kubernetes
- Google系統架構解密:構建安全可靠的系統
- Learning Android Intents
- PLC控制系統應用與維護
- 深入Linux內核架構與底層原理(第2版)
- 混沌工程:復雜系統韌性實現之道
- STM32庫開發實戰指南:基于STM32F4
- Windows Server 2012網絡操作系統項目教程(第4版)
- NetDevOps入門與實踐
- iOS 10 開發指南
- 鴻蒙HarmonyOS手機應用開發實戰
- Android應用性能優化最佳實踐
- Android NDK Beginner's Guide
- Raspberry Pi Networking Cookbook
- Windows8應用開發權威指南