Protocols enable the separation between interface and implementation: several objects can implement the same protocol and an object can implement several protocols. Protocols may contain only predicate declarations. In some languages the term <em>interface</em> is used with similar meaning. Logtalk allows predicate declarations of any scope within protocols, contrary to some languages that only allow public declarations.
We can define a new object in the same way we write Prolog code: by using a text editor. Logtalk source files may contain one or more objects, categories, or protocols. If you prefer to define each entity in its own source file, it is recommended that the file be named after the protocol. By default, all Logtalk source files use the extension <code>.lgt</code> but this is optional and can be set in the configuration files. Intermediate Prolog source files (generated by the Logtalk compiler) have, by default, a <code>.pl</code> extension. Again, this can be set to match the needs of a particular Prolog compiler in the corresponding configuration file. For example, we may define a protocol named <code>listp</code> and save it in a <code>listp.lgt</code> source file that will be compiled to a <code>listp.pl</code> Prolog file.
Protocol names must be atoms. Objects, categories and protocols share the same name space: we cannot have a protocol with the same name as an object or a category.
Protocol directives are textually encapsulated by using two Logtalk directives: <atitle="Consult reference manual"href="../refman/directives/protocol1_2.html"><code>protocol/1-2</code></a> and <atitle="Consult reference manual"href="../refman/directives/end_protocol0.html"><code>end_protocol/0</code></a>. The most simple protocol will be one that is self-contained, not depending on any other Logtalk entity:
We can find, by backtracking, all defined protocols by using the <atitle="Consult reference manual"href="../refman/builtins/current_protocol1.html"><code>current_protocol/1</code></a> built-in predicate with an uninstantiated variable:
We can create a new (dynamic) protocol in runtime by calling the Logtalk built-in predicate <atitle="Consult reference manual"href="../refman/builtins/create_protocol3.html"><code>create_protocol/3</code></a>:
The first argument, the name of the new protocol (a Prolog atom), should not match an existing entity name. The remaining two arguments correspond to the relations described in the opening protocol directive and to the protocol directives.
If we need to create a lot of (dynamic) protocols at runtime, then is best to define a metaclass or a prototype with a predicate that will call this built-in predicate in order to provide more sophisticated behavior.
Dynamic protocols can be abolished using the <atitle="Consult reference manual"href="../refman/builtins/abolish_protocol1.html"><code>abolish_protocol/1</code></a> built-in predicate:
We can define a goal to be executed as soon as a protocol is (compiled and) loaded to memory with the <atitle="Consult reference manual"href="../refman/directives/initialization1.html"><code>initialization/1</code></a> directive:
As usually happens with Prolog code, a protocol can be either static or dynamic. A protocol created during the execution of a program is always dynamic. A protocol defined in a file can be either dynamic or static. Dynamic protocols are declared by using the <atitle="Consult reference manual"href="../refman/directives/dynamic0.html"><code>dynamic/0</code></a> directive in the protocol source code:
The directive must precede any predicate directives. Please be aware that using dynamic code implies a performance hit when compared to static code. We should only use dynamic protocols when these need to be abolished during program execution.
A protocol can be documented with arbitrary user-defined information by using the <atitle="Consult reference manual"href="../refman/directives/info1.html"><code>info/1</code></a> directive:
Logtalk provides two sets of built-in predicates that enable us to query the system about the possible relationships that a protocol have with other entities.
</p>
<p>
The built-in predicates <atitle="Consult reference manual"href="../refman/builtins/extends_protocol2_3.html"><code>extends_protocol/2</code></a> and <atitle="Consult reference manual"href="../refman/builtins/extends_protocol2_3.html"><code>extends_protocol/3</code></a> return all pairs of protocols so that the first one extends the second:
To find which objects or categories implement which protocols we can call the <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> or <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> built-in predicates:
Note that, if we use an uninstantiated variable for the first argument, we will need to use the <atitle="Consult reference manual"href="../refman/builtins/current_object1.html"><code>current_object/1</code></a> or <atitle="Consult reference manual"href="../refman/builtins/current_category1.html"><code>current_category/1</code></a> built-in predicates to identify the kind of entity returned.
We can find the properties of defined protocols by calling the <atitle="Consult reference manual"href="../refman/builtins/protocol_property2.html"><code>protocol_property/2</code></a> built-in predicate:
A protocol may have the property <code>static</code>, <code>dynamic</code>, or <code>built_in</code>. Dynamic protocols can be abolished in runtime by calling the <atitle="Consult reference manual"href="../refman/builtins/abolish_protocol1.html"><code>abolish_protocol/1</code></a> built-in predicate.
To make all public predicates declared via an implemented protocol protected or to make all public and protected predicates private we prefix the protocol's name with the corresponding keyword. For instance:
Logtalk defines a built-in protocol named <code>monitoring</code> that contains declarations for the <code>before/3</code> and <code>after/3</code> public event handler predicates. See the <ahref="events.html">event-driven programming</a> section for more details.