e01.org

Single-serving visitor pattern

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). Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Single-serving visitor pattern". 1783380899. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1362931335

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·