{
  "id": "enwiki-001362931335",
  "slug": "single-serving-visitor-pattern",
  "title": "Single-serving visitor pattern",
  "qid": "",
  "lead": "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).",
  "body": "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",
  "categories": [
    "103769157",
    "24745115",
    "25181569",
    "25397562",
    "25897797",
    "26748027",
    "27395026",
    "27695885"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1362931335",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1783380899.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001362931335",
    "type": "webpage",
    "title": "Single-serving visitor pattern",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/single-serving-visitor-pattern",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          14
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          14
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001362931335",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1362931335",
    "note": "Immutable snapshot enwiki-001362931335"
  }
}