| Prototype-based Programming |
Website Links For Programming |
Information AboutPrototype-based Programming |
| CATEGORIES ABOUT PROTOTYPE-BASED PROGRAMMING | |
| programming paradigms | |
| object-oriented programming | |
| type theory | |
|
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 class-less programming style has recently grown increasingly popular, and has been adopted for the Programming Languages JavaScript , Squeak when using the Viewer framework to manipulate Morphic components, Cecil , NewtonScript , Io , MOO , REBOL , Kevo , 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 '' Instance s'' are "usable" objects based on the patterns of a particular class. In this model, ''classes'' act as collections of behaviour ( Methods ) and structure which 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 behaviour 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 which focus first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behaviour of some set of examples and only later worry about classifying these objects into archetypal objects which are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during Runtime , whereas only a 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. Prototype-based programming is often associated with particular schools of thought in Cognitive Psychology which emphasize Prototypes or Exemplars as key attributes of the learning process. 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'' is an example of such a system. 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 behaviour 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 scratch") object creation. While most systems support a variety of cloning, ''ex nihilo'' object creation is not as prominent. Systems which 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 behaviours of new objects without referencing existing objects. In many prototype languages, there is often a basic ''Object'' prototype which 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 an object's ''__proto__'' property to null. ''Cloning'' refers to a process whereby a new object is constructed by copying the behaviour 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 system, 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. DELEGATION In prototype-based languages which use ''delegation'' the language runtime is capable of Dispatching to 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 behaviour-sharing behaviour between objects is thus 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 ammended over time without re-arranging the structure of its associated class 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 Programming 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). This process can be compared in some ways to biological Mitosis . Methods 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 behaviour to the programming while sharing implementation and data behind-the-scenes; such an approach is indeed followed by Kevo .. 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 , Safety , Predictability and Efficiency . In regard to the first three points, classes are often seen as analogous to types (and indeed, in most statically typed object-oriented languages they serve that role) and are proposed to provide contractual guarantees to their instances and users of their instances that they will behave in a given fashion. On the last point, efficiency, the declaration of classes simplifies many Compiler optimizations that allow the development of efficient method and instance variable lookup. In the case of the Self Programming Language , much development time has been spent on the development of compilation and interpretation techniques to improve the performance of prototype-based systems versus their class-based competitors. For example, Lisaac compiler produces code almost as fast as C. Tests have been run with a MPEG-2 codec written in Lissac, copied from a C version. These tests show the Lisaac version is 1.9% slower than the C version with 37% less line of codes. Finally, perhaps the most common criticism leveled against prototype-based languages is that the community of Software Developers is not familiar with them, despite the popularity and market permeation of JavaScript , (and in point of fact, the current work on a fourth edition of the ECMAScript standard is seeking to turn JavaScript into a class-based language). Further, as prototype-based systems are relatively novel, and few and far between, Best Practices for software development using them have not become widespread. LANGUAGES
FURTHER READING |
|
|