In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a dispatch table is a table of
pointers or
memory address
In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. ...
es to functions or
method
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
s.
Use of such a table is a common technique when implementing
late binding in
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
.
Perl implementation
The following shows one way to implement a dispatch table in
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
, using a
hash
Hash, hashes, hash mark, or hashing may refer to:
Substances
* Hash (food), a coarse mixture of ingredients
* Hash, a nickname for hashish, a cannabis product
Hash mark
*Hash mark (sports), a marking on hockey rinks and gridiron football fiel ...
to store references to code (also known as
function pointers).
# Define the table using one anonymous code-ref and one named code-ref
my %dispatch = (
"-h" => sub ,
"-g" => \&say_goodbye
);
sub say_goodbye
# Fetch the code ref from the table, and invoke it
my $sub = $dispatch;
print $sub ? $sub->() : "unknown argument\n";
Running this Perl program as
perl greet -h
will produce "hello", and running it as
perl greet -g
will produce "goodbye".
JavaScript implementation
Following is a demo of implementing dispatch table in JavaScript:
var thingsWeCanDo = ;
var doSomething = function(doWhat)
Virtual method tables
In
object-oriented
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s that support
virtual methods, the
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
will automatically create a dispatch table for each object of a
class
Class or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used differently ...
containing virtual methods. This table is called a
virtual method table
In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).
Whe ...
or ''vtable'', and every call to a virtual method is dispatched through the vtable.
See also
*
Branch table
In computer programming, a branch table or jump table is a method of transferring program control ( branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch or jump instruction ...
References
*
Diomidis Spinellis (2003). ''
Code Reading: The Open Source Perspective''. Boston, MA: Addison-Wesley. {{ISBN, 0-201-79940-5
Method (computer programming)
Articles with example Perl code