Single-serving visitor pattern
In computer programming, the single-serving visitor pattern is a design pattern.[src] Its intent is to optimise the implementation of a visitor that is allocated, used only once, and then deleted (which is the case of most visitors).[src]
In computer programming, the single-serving visitor pattern is a design pattern.[src] Its intent is to optimise the implementation of a visitor that is allocated, used only once, and then deleted (which is the case of most visitors).[src] ==Usage examples== The single-serving visitor is called through the intermediate of static methods.[src] Without parameters, this is SingleServingVisitor::applyTo(e); , while with parameters, it is SingleServingVisitor::applyTo(e, a, b); .[src] If implemented as a singleton: SingleServingVisitor::setA(a); SingleServingVisitor::setB(b); SingleServingVisitor::applyTo(e); ==Consequences== ===Pros=== No "zombie" objects.[src] With a single-serving visitor, it is ensured that visitors are allocated when needed and destroyed once useless.[src] A simpler interface than visitor.[src] The visitor is created, used and free by the sole call of the applyTo() static method.[src] ===Cons=== Repeated allocation.[src] At each call of the applyTo() method, a single-serving visitor is created then discarded, which is time-consuming.[src] In contrast, the singleton only performs one allocation.[src] ==Implementation== template class SingleServingVisitor { protected: SingleServingVisitor() = default; public: ~SingleServingVisitor() = default; static void applyTo(Element& e) { e.accept(SingleServingVisitor ()); } virtual void visitA(A& a) = 0; virtual void visitB(B& b) = 0; }; ===Passing parameters=== If the single-serving visitor has to be initialised, the parameters have to be passed through the static method: static void applyTo(Element& elem, const A& a, const B& b) { elem.accept(SingleServingVisitor (a, b)); } === Implementation as a singleton=== This implementation ensures: that there is at most one instance of the single-serving visitor that the visitor can be accessed later template class SingleServingVisitor { protected: A a; B b; SingleServingVisitor() = default; // Note: instance() method need not to be public static SingleServingVisitor& instance() noexcept { static SingleServingVisitor ssv; return ssv; } public: ~SingleServingVisitor() = default; static void applyTo(Element& e) noexcept { e.accept(instance()); } // static methods to access parameters static void setA(const A& a) noexcept { this->a = a; } static void setB(const B& b) noexcept { this->b = b; } virtual void visitA(A& a) = 0; virtual void visitB(B& b) = 0; }; ==Related patterns== Visitor pattern, from which this pattern derives Composite pattern: single-serving visitor is often applied to hierarchies of elements Singleton pattern[src]
14 sentences · 14 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Single-serving visitor pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/single-serving-visitor-pattern. Accessed 25 November 2026.
Chicago
"Single-serving visitor pattern." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/single-serving-visitor-pattern.
APA
encyclopegia.org. (2026). Single-serving visitor pattern. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/single-serving-visitor-pattern
Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON