Prototype-based programming

Prototype-based programming

Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as "class-less", "prototype-oriented" or "instance-based" programming.

The original (and most canonical) example of a prototype-based language is the programming language Self developed by David Ungar and Randall Smith. However, the classless programming style has recently grown increasingly popular, and has been adopted for the programming languages JavaScript, Cecil, NewtonScript, Io, MOO, REBOL, Lisaac and several others.

Comparison with class-based models

With class-based languages, objects come in two general types. "Classes" define the basic layout and functionality of objects, and "instances" are "usable" objects based on the patterns of a particular class. In this model, "classes" act as collections of behavior (methods) and structure that are the same for all instances, whereas "instances" carry the objects' data. The role distinction is thus primarily based on a distinction between structure and behavior on the one hand, and "state" on the other.

Advocates of prototype-based programming often argue that class-based languages encourage a model of development that focuses first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during runtime, whereas only very few class-based object-oriented systems (such as the first dynamic object-oriented system, Smalltalk) allow classes to be altered during the execution of a program.

While the vast majority of prototype-based systems are based around interpreted and dynamically typed programming languages, it is important to point out that statically typed systems based around prototypes are technically feasible. The Omega programming language discussed in "Prototype-Based Programming" [Section 2.8 (pg.177). Günther Blaschek, "Omega: Statically Typed Prototypes"] is an example of such a system, though according to Omega's website even Omega is not exclusively static but rather its "compiler may choose to use static binding where this is possible and may improve the efficiency of a program."

Object construction

In class-based languages a new instance is constructed through the class's constructor and an optional set of constructor arguments. The resulting instance is modeled on the layout and behavior dictated by the chosen class.

In prototype-based systems there are two methods of constructing new objects, through "cloning" of an existing object, and through "ex nihilo" ("from nothing") object creation. While most systems support a variety of cloning, "ex nihilo" object creation is not as prominent. [Section 1.2 (pg.17). Chistophe Dony, Jacques Malenfan, Daniel Bardou, "Classifying Prototype-based Programming Languages"]

Systems that support "ex nihilo" object creation allow new objects to be created from scratch without cloning from an existing prototype. Such systems provide a special syntax for specifying the properties and behaviors of new objects without referencing existing objects. In many prototype languages, there is often a basic "Object" prototype that carries commonly needed methods and is used as a master prototype for all other objects. One useful aspect of "ex nihilo" object creation is to ensure that a new object's slot names do not have namespace collisions with the top-level "Object" object. (In the Mozilla JavaScript implementation, one can accomplish this by setting a newly constructed object's "__proto__" property to null.)

"Cloning" refers to a process whereby a new object is constructed by copying the behavior of an existing object (its prototype). The new object then carries all the qualities of the original. From this point on, the new object can be modified. In some systems the resulting child object maintains an explicit link (via "delegation" or "resemblance") to its prototype, and changes in the prototype cause corresponding changes to be apparent in its clone. Other systems, such as the Forth-like programming language Kevo, do not propagate change from the prototype in this fashion, and instead follow a more "concatenative" model where changes in cloned objects do not automatically propagate across descendants. [Section 1.1 (pg.14). Antero Taivalsaari, "Classes vs. Prototypes: Some Philosophical and Historical Observations"]

//Example of true prototypal inheritance style in javascript.

//"ex nihilo" object creation employing the literal object notation {}.var foo = {one: 1, two: 2};//another "ex nihilo" object.var bar = {three: 3};

//Gecko and Webkit can directly manipulate the hidden __proto__ link.//IE, Opera et al may need to use the object, clone or Object.beget functions//to make a new object and set its __proto__ to the parent object and return//the newly created child object.

bar.__proto__ = foo; // bar is now the child of foo.

//If we try to access foo's properties from bar from now on, we'll succeed. alert(bar.one); //alerts 1.

//The child objects properties are also accessible.alert(bar.three); //alerts 3.

Delegation

In prototype-based languages that use "delegation", the language runtime is capable of dispatching the correct method or finding the right piece of data simply by following a series of delegation pointers (from object to its prototype) until a match is found. All that is required to establish this behavior-sharing between objects is the delegation pointer. Unlike the relationship between class and instance in class-based object-oriented languages, the relationship between the prototype and its offshoots does not require that the child object have a memory or structural similarity to the prototype beyond this link. As such, the child object can continue to be modified and amended over time without rearranging the structure of its associated prototype as in class-based systems. It is also important to note that not only data but also methods can be added or changed. For this reason, most prototype-based languages refer to both data and methods as "slots".

Concatenation

Under pure prototyping, which is also referred to as "concatenative" prototypes, and is exemplified in the Kevo language, there are no visible pointers or links to the original prototype from which an object is cloned. The prototype object is copied exactly, but given a different name (or reference). Behavior and attributes are simply duplicated as-is.

Advantages to this approach include the fact that object authors can alter the copy without worrying about side-effects across other children of the parent. A further advantage is that the computational cost of method lookup during dispatch is drastically reduced when compared to delegation, where an exhaustive search must be made of the entire delegation chain before failure to find a method or slot can be admitted.

Disadvantages to the concatenative approach include the organizational difficulty of propagating changes through the system; if a change occurs in a prototype, it is not immediately or automatically available on its clones. However, Kevo does provide additional primitives for publishing changes across sets of objects based on their similarity (so-called "family resemblances") rather than through taxonomic origin, as is typical in the delegation model.

Another disadvantage is that, in the most naive implementations of this model, additional memory is wasted (versus the delegation model) on each clone for the parts that have stayed the same between prototype and clone. However, it is possible to provide concatenative behavior to the programming while sharing implementation and data behind-the-scenes; such an approach is indeed followed by Kevo. [Antero Taivalsaari. Kevo, a prototype-based object-oriented programming language based on concatenation and module operations. Technical Report Report LACIR 92-02, University of Victoria, 1992.] .

An alternative quasi-solution to the problem of clones interfering with the behavior of the parent is to provide a means whereby the potential parent is flagged as being clonable or not. In MOO, this is achieved with the "f" flag. Only objects with the "f" flag can be cloned. In practice, this leads to certain objects serving as "surrogate classes"; their properties are kept constant to serve as initial values for their children. These children then tend to have the "f" flag not set.

Criticism

Advocates of class-based object models who criticize prototype-based systems often have concerns that could be seen as similar to those concerns that proponents of static type systems for programming languages have of dynamic type systems (see Datatype). Usually, such concerns involve: correctness, safety, predictability, and efficiency.

On the first three points, classes are often seen as analogous to types (in most statically typed object-oriented languages they serve that role) and are proposed to provide contractual guarantees to their instances, and to users of their instances, that they will behave in some given fashion.

On the last point, efficiency, the declaration of classes simplifies many compiler optimizations that allow developing efficient method and instance variable lookup. For the Self language, much development time was spent on developing, compiling, and interpreting techniques to improve the performance of prototype-based systems versus class-based systems. For example, the Lisaac compiler produces code almost as fast as C. Tests have been run with an MPEG-2 codec written in Lisaac, copied from a C version. These tests show the Lisaac version is 1.9% slower than the C version with 37% fewer lines of code [cite web |url=http://isaacproject.u-strasbg.fr/li_benchs.html |title=Isaac project benchmarks |accessdate=2007-07-24 ] . However, the C programming language is not a class-based object-oriented language, but rather a procedural language. A Lisaac to C++ comparison would be more appropriate.

The most common criticism made against prototype-based languages is that the community of software developers is not familiar with them, despite the popularity and market permeation of JavaScript. This knowledge level of prototype based systems seems to be changing with the proliferation of JavaScript frameworks and increases in the complex use of JavaScript as "Web 2.0" matures.

Possibly because of these reasons, work on a fourth edition of the ECMAScript standard seeks to provide JavaScript with class-based language constructs. It is worth noting that according to this specification, the prototype for an object still exists when traditional class based syntax is used.

Languages

*Actor-Based Concurrent Language (ABCL): ABCL/1, ABCL/R, ABCL/R2, ABCL/c+
*Agora
*Cecil
*Cel
*ColdC
*ECMAScript
**ActionScript (Used by Adobe Flash and Adobe Flex)
**DMDScript
**E4X
**JavaScript (first named Mocha, then LiveScript)
**JScript
* [http://www.ozonehouse.com/mark/codeworks.html Glyphic Script]
*Io
*Kevo
*Lisaac
*Logtalk
*Lua
*LPC
*MOO
*Neko
*NewtonScript
*Obliq
*Omega
*OpenLaszlo
*Perl, with the [http://search.cpan.org/dist/Class-Prototyped/lib/Class/Prototyped.pm Class::Prototyped] module
*REBOL
*Self
*Slate
*SmartFrog
*TADS
*Tcl with the snit extension.

References

Further reading

*cite book|editor=James Noble (ed.), Antero Taivalsaari (ed.), Ivan Moore (ed.)|year=1999|title=Prototype-Based Programming: Concepts, Languages and Applications|publisher=Springer-Verlag|id=ISBN 981-4021-25-3
*cite book|first=Martin|last=Abadi|authorlink=Martin Abadi|coauthors=Luca Cardelli|year=|title=A Theory of Objects|publisher=Springer-Verlag|id=ISBN 0-387-94775-2

ee also

* Class-based programming (contrast)
* Programming paradigms
* Differential inheritance
* Prototype-based language implementations


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Class-based programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computin …   Wikipedia

  • Non-English-based programming languages — are computer programming languages that, unlike better known programming languages, do not use keywords taken from, or inspired by, the English vocabulary. Contents 1 Prevalence of English based programming languages 2 International programming… …   Wikipedia

  • Prototype — A prototype is an original type, form, or instance of something serving as a typical example, basis, or standard for other things of the same category. The word derives from the Greek πρωτότυπον ( prototypon ), archetype, original , neutral of… …   Wikipedia

  • Prototype (disambiguation) — A prototype is something that is representative of a category of things. Prototype may also refer to:;Automobiles* Citroën Prototype C, range of vehicles created by Citroën from 1955 to 1956 * Citroën Prototype Y, project of replacement of the… …   Wikipedia

  • Programming paradigm — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concu …   Wikipedia

  • Programming in the large and programming in the small — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computin …   Wikipedia

  • Programming language — lists Alphabetical Categorical Chronological Generational A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that… …   Wikipedia

  • Object-oriented programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   Wikipedia

  • Prototype JavaScript Framework — Infobox Software name = Prototype JavaScript Framework developer = [http://prototypejs.org/core Prototype Core Team] latest release version = 1.6.0.3 latest release date = release date|2008|09|28 genre = JavaScript toolkit license = MIT License… …   Wikipedia

  • Multi-paradigm programming language — A multi paradigm programming language is a programming language that supports more than one programming paradigm. As Leda designer Tim Budd holds it: The idea of a multiparadigm language is to provide a framework in which programmers can work in… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”