Single-serving visitor pattern
≈ 2 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, the single-serving visitor pattern is a design pattern. 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).
In computer programming, the single-serving visitor pattern is a design pattern. 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).
Usage examples
The single-serving visitor is called through the intermediate of static methods. Without parameters, this is SingleServingVisitor::applyTo(e); , while with parameters, it is SingleServingVisitor::applyTo(e, a, b); . If implemented as a singleton: SingleServingVisitor::setA(a); SingleServingVisitor::setB(b); SingleServingVisitor::applyTo(e);
Consequences
Pros
No "zombie" objects. With a single-serving visitor, it is ensured that visitors are allocated when needed and destroyed once useless. A simpler interface than visitor. The visitor is created, used and free by the sole call of the applyTo() static method.
Cons
Repeated allocation. At each call of the applyTo() method, a single-serving visitor is created then discarded, which is time-consuming. In contrast, the singleton only performs one allocation.
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
Text derived from https://en.wikipedia.org/w/index.php?oldid=1362931335 under CC BY-SA 4.0; modifications have been made.