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 accomplished by cloning existing objects which serve as prototypes for the new ones. It is also known as class-less, prototype-oriented, or instance-based programming.

The original (and canonical) example of a prototype based language is Self, but the class-less programming style has recently become increasingly popular and it has been adopted by Cecil, JavaScript, NewtonScript, Io, MOO, REBOL, and several other programming languages.

Contents

Incentive

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. Programming in such a language typically consists of designing classes, and adding functionality to create various instances that store and manipulate data.

The classes are collections of code for the objects, which are the same for all instances, whereas the instances are collections of memory holding the objects' data, which are what distinguishes them from each other (a concept known as state). This model works well with traditional compilers, which basically write code and then have that code manipulate data.

One problem with class-based languages is that programs rarely remain static during development. As systems grow, their original class stuctures often become inadequate. The classes must be extended with more code, negating some of the benefits of object orientation. For some uses, it would be better if programs could change the behaviour of the underlying classes. In most languages, the classes are however fixed at compile time and can not be changed at runtime.

Prototypes and cloning

Instead of data-containing instances and code-containing classes, prototype-based languages only have objects. A prototype system starts with at least one atomic object loaded, and new objects are created by cloning existing ones. Cloning an object creates an entirely new one that starts with the same default behaviours as its original.

Instead of having a pointer to a class, new objects contain a pointer to the object that created them. They are largely empty, and only start growing in memory when changed. This is different from class-based object-oriented languages, where each instance of a class usually sets aside a known amount of memory. Additional data can be added to any object at any point at runtime. Since objects grow as needed, anything can be added to them. In fact, every object in such a system tends to be different than every other, not only in the data themselves, but in what data are being recorded. 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".

Although prototype based languages only need to start with one object, they typically provide a selection of basic objects for convenience. In general these objects tend to have method definitions only, relying on the programmer to add the data as needed. For instance, a Point object that can record positions in two dimensions might contain methods for adding and subtracting points, but not include the x and y definitions. The programmer would instead have to add these to each new copy.

This leads to some increased flexibility. Bugs in existing object methods can be fixed by sending the new code (in the form of blocks) into the appropriate "slot". Similarly, any object can be turned into one that handles remote invocation by replacing methods in the same fashion. Class design is simplified, because the "class" definitions can be changed at any point in time, and within any application (one application can fix bugs in the objects it uses for itself, and the changes will not be visible to others).

"Pure" prototypes

Under pure prototyping, there are no pointers to the "parent". The "mother" object is copied exactly, but given a different name (or reference). It can be compared to biological mitosis. Methods and attributes are simply duplicated as-is. The drawback is that memory is consumed for each copy for the parts that are the same. The advantage is that one can alter the copy without worrying about side-effects in the other children of the parent. Pure prototyping can also simplify implementation, assuming memory is not a scarce resource.

Languages

See also

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools