This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/manuals/tutorial/reflection.html
pmoura 36a326908c Logtalk 2.28.2 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1711 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2006-11-07 17:11:47 +00:00

91 lines
4.5 KiB
HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="application/xml+xhtml; charset=utf-8" />
<title>Logtalk tutorial: a reflective class-based system</title>
<link rel="stylesheet" href="../screen.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="../print.css" type="text/css" media="print"/>
</head>
<body>
<div class="top-left">Logtalk tutorial</div>
<div class="top-right">A reflective class-based system</div>
<div class="bottom-left"><span class="page"/></div>
<div class="bottom-right"><span class="page"/></div>
<div class="navtop"><a href="../index.html">contents</a> &gt; <a href="index.html">tutorial</a></div>
<h1>A reflective class-based system</h1>
<p>
When compiling an object, Logtalk distinguishes prototypes from instance or classes by examining the object relations. If an object instantiates and/or specializes another object, then it is compiled as an instance or class, otherwise it is compiled as a prototype. A consequence of this is that, in order to work with instance or classes, we always have to define root objects for the instantiation and specialization hierarchies (however, we are not restricted to a single hierarchy). The best solution is often to define a reflective class-based system <a href="../bibliography.html#Maes87">[Maes 87]</a>, where every class is also an object and, as such, an instance of some class.
</p>
<p>
In this example, we are going to define the basis for a reflective class-based system, based on an extension of the ideas presented in <a href="../bibliography.html#Cointe87">[Cointe 87]</a>. This extension provides, along with root objects for the instantiation and specialization hierarchies, explicit support for abstract classes <a href="../bibliography.html#Moura94">[Moura 94]</a>.
</p>
<h2>Defining the base classes<a id="classes"></a></h2>
<p>
We will start by defining three classes: <code>object</code>, <code>abstract_class</code>, and <code>class</code>. The class <code>object</code> will contain all predicates common to all objects. It will be the root of the inheritance graph:
</p>
<pre>:- object(object,
instantiates(class)).
% predicates common to all objects
:- end_object.</pre>
<p>
The class <code>abstract_class</code> specializes <code>object</code> by adding predicates common to all classes. It will be the default meta-class for abstract classes:
</p>
<pre>:- object(abstract_class,
instantiates(class),
specializes(object)).
% predicates common to all classes
:- end_object.</pre>
<p>
The class <code>class</code> specializes <code>abstract_class</code> by adding predicates common to all instantiable classes. It will be the root of the instantiation graph and the default meta-class for instantiable classes:
</p>
<pre>:- object(class,
instantiates(class),
specializes(abstract_class)).
% predicates common to all instantiable classes
:- end_object.</pre>
<p>
Note that all three objects are instances of class <code>class</code>. The instantiation and specialization relationships are chosen so that each object may use the predicates defined in itself and in the other two objects, with no danger of method lookup endless loops.
</p>
<h2>Summary<a id="summary"></a></h2>
<ul>
<li>An object that does not instantiate or specialize other objects is always compiled as a prototype.</li>
</ul>
<ul>
<li>An instance must instantiate at least one object (its class). Similarly, a class must at least specialize or instantiate other object.</li>
</ul>
<ul>
<li>The distinction between abstract classes and instantiable classes is an operational one, depending on the class inherited methods. A class is instantiable if inherits methods for creating instances. Conversely, a class is abstract if does not inherit any instance creation method.</li>
</ul>
<div class="footer">
<div class="copyright">
<span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/>
<span>Last updated on: October 26, 2006</span>
</div>
<div class="navbottom">
<span><a href="index.html">previous</a> | <a href="../glossary.html">glossary</a> | <a href="profiling.html">next</a></span><br/>
<span><a href="http://validator.w3.org/check/referer">XHTML</a> + <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a></span>
</div>
</div>
</body>
</html>