In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, the single-serving visitor pattern is a
design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
. Its intent is to optimise the implementation of a
visitor
A visitor, in English and Welsh law and history, is an overseer of an autonomous ecclesiastical or eleemosynary institution, often a charitable institution set up for the perpetual distribution of the founder's alms and bounty, who can inter ...
that is allocated, used only once, and then deleted (which is the case of most visitors).
Applicability
The single-serving visitor pattern should be used when visitors do not need to remain in memory. This is often the case when visiting a hierarchy of objects (such as when the
visitor pattern
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to ...
is used together with the
composite pattern In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "comp ...
) to perform a single task on it, for example counting the number of cameras in a 3D scene.
The regular
visitor pattern
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to ...
should be used when the visitor must remain in memory. This occurs when the visitor is configured with a number of parameters that must be kept in memory for a later use of the visitor (for example, for storing the rendering options of a 3D scene renderer).
However, if there should be only one instance of such a visitor in a whole program, it can be a good idea to implement it both as a single-serving visitor and as a
singleton. In doing so, it is ensured that the single-serving visitor can be called later with its parameters unchanged (in this particular case "single-serving visitor" is an abuse of language since the visitor can be used several times).
Usage examples
The single-serving visitor is called through the intermediate of static methods.
* Without parameters:
Element* elem;
SingleServingVisitor::apply_to(elem);
* With parameters:
Element* elem;
TYPE param1, param2;
SingleServingVisitor::apply_to(elem, param1, param2);
* Implementation as a singleton:
Element* elem;
TYPE param1, param2;
SingleServingVisitor::set_param1(param1);
SingleServingVisitor::set_param2(param2);
SingleServingVisitor::apply_to(elem);
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 ''apply_to'' static method.
Cons
* ''Repeated allocation''. At each call of the ''apply_to'' method, a single-serving visitor is created then discarded, which is time-consuming. In contrast, the singleton only performs one allocation.
Implementation (in C++)
Basic implementation (without parameters)
// Declaration
class Element;
class ElementA;
class ElementB;
class SingleServingVisitor;
... // Same as with the visitor pattern
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to ...
.
// Definition
class SingleServingVisitor
// Implementation
void SingleServingVisitor::apply_to(Element* elem)
Passing parameters
If the single-serving visitor has to be initialised, the parameters have to be passed through the static method:
void SingleServingVisitor::apply_to(Element* elem, TYPE param1, TYPE param2, ...)
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
// Definition
class SingleServingVisitor
// Implementation
SingleServingVisitor* SingleServingVisitor::instance_ = NULL;
SingleServingVisitor* SingleServingVisitor::get_instance()
void SingleServingVisitor::apply_to(Element* elem)
void SingleServingVisitor::set_param1(TYPE param1)
void SingleServingVisitor::set_param2(TYPE param2)
Related patterns
*
Visitor pattern
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to ...
, from which this pattern derives
*
Composite pattern In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "comp ...
: single-serving visitor is often applied to hierarchies of elements
*
Singleton pattern
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. One of the well-known "Gang of Four" design patterns, which describe how to solve recurring probl ...
{{use dmy dates, date=January 2012
Software design patterns
Articles with example C++ code