encyclopegia.org

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

Related articles

split-single

stub · references available

single whip law

stub · references available

single overhead camshaft

stub · references available

single address space operating system

stub · references available

single in-line package

stub · references available

First Single Album

stub · references available

single-nucleotide polymorphism

stub · references available

single track

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1362931335 · 0.5 · snapshot enwiki-001362931335

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.