CSC/ECE 517 Spring 2014/ch1a 1a rt: Difference between revisions
(Removed explanation of inheritance types and revised inheritance compare & contrast.) |
No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
'''Prototype-Based Programming''' | '''Prototype-Based Programming''' | ||
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming], | In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming], [http://en.wikipedia.org/wiki/Inheritance_(programming) inheritance] is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both. Class based inheritance required objects to be defined as classes before they are created. In contrast, prototype-based inheritance consists of [http://en.wikipedia.org/wiki/Cloning_(programming) cloning] existing objects which act as prototypes.<ref>http://en.wikipedia.org/wiki/Prototype-based_programming</ref> This programming style is also known as instance-base, prototype-oriented, or class-less programming. | ||
Previous page on this topic: [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2013/ch1a_1e_pi CSC/ECE 517 Spring 2013/ch1a 1e pi] | Previous page on this topic: [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2013/ch1a_1e_pi CSC/ECE 517 Spring 2013/ch1a 1e pi] | ||
Line 10: | Line 10: | ||
===Object Creation=== | ===Object Creation=== | ||
In classical inheritance, Objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes. | |||
Classical inheritance example: | Classical inheritance example: | ||
Line 17: | Line 17: | ||
//properties | //properties | ||
} | } | ||
class Car inherits Vehicle {} | class Car inherits Vehicle {} | ||
class trucks inherits inherits Car {} | class trucks inherits inherits Car {} | ||
class medium Truck inherits Truck {} | class medium Truck inherits Truck {} | ||
In prototype based programming, a new object can be instantiated by simply creating a copy of/cloning an existing object.<ref>http://en.wikipedia.org/wiki/Cloning_(programming)</ref> | |||
Prototype based inheritance example: | Prototype based inheritance example: | ||
Line 29: | Line 29: | ||
//properties | //properties | ||
} | } | ||
Car.prototype = Vehicle.new(); | Car.prototype = Vehicle.new(); | ||
Car.make =("Chevy"); | Car.make =("Chevy"); | ||
Car. | Car.model=("Master Deluxe II"); | ||
=== | ===Delegation=== | ||
In class inheritance, methods are either defined in child classes or inherited from their parents. [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] is a feature of prototype-based languages that use pointers to find the correct method or data during runtime. The delegation pointer establishes the link between parent and child methods. This allows child objects to be different in structure from their parents and be modified without affecting the parent.<ref>http://en.wikipedia.org/wiki/Delegation_(programming)</ref> | |||
=='''Advantages'''== | |||
===Ease of creating object variety=== | |||
In prototype-based programming, new objects can be quickly cloned and defined in individual lines of code. As well, multiple inheritance is made simple with prototypes compared to class inheritance. Individual data or methods in two unrelated objects can be associated to a child without requiring complete inheritance. This allows for a high density of different prototypes that can be easily maintained. | |||
<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
= | class Bicycle{} | ||
class ElectricCar{} | |||
Scooter.prototype = Bicycle.new(); | |||
Scooter.power_source = ElectricCar.power_source; | |||
//An electric scooter is made from inheriting | |||
//data from two unrelated classes. | |||
===One-of-a-kind object support=== | |||
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] provides a framework that can easily include one-of-a-kind objects with their own behavior. Since each object has named slots, and slots can hold state or behavior, any object can have unique state or behavior. Class-based systems are designed for situations where there are many objects with the same behavior. There is no support for an object to possess its own unique behavior, and it is awkward to create a class that is guaranteed to have only one instance. Ruby suffers from neither of these disadvantages. A unique object can be customized with its own behavior, and a separate "instance" is not needed.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
===Amending objects=== | |||
Using prototype-based programming, any object can be treated as a prototype, therefore the object can be extended without having to create a new object. In class-based programming, objects are committed to the functionality provided at instantiation. If the functionality of the class-based object later needs to be extended, a new object that has the desired functionality must instantiated and transfer the state between the two objects.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
=== | ==Disadvantages== | ||
===Prototype based inheritance requires dynamic typing=== | |||
Because objects are being inherited rather than classes, it is necessary that the language support [http://en.wikipedia.org/wiki/Dynamic_typing#DYNAMIC dynamic typing]. Otherwise there is no way to determine the object's inherited properties. This increases performance overhead, as each time an object's function is executed, it must be ensured that the object is of the correct type for that function.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
=== | ===Refactoring=== | ||
In a prototype based system inheritance is built across individual lines of code instead of classes. [http://en.wikipedia.org/wiki/Code_refactoring Refactoring] tools and IDEs cannot statically analyze code in a dynamic type system to refactor code.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
===Testing for assurance=== | |||
The classical approach offers [http://en.wikipedia.org/wiki/Static_typing#STATIC static typing] where the type of an object is determined at compilation. Since all languages that allow prototype based inheritance are dynamically typed, typically more testing is required to ensure correct behavior and that objects are of the correct type for methods they are called on.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref> | |||
== | ===Convention=== | ||
Class base inheritance is more widely used, making programmer unfamiliarity a possible issue. Programmers accustomed to static typing and class inheritance can find prototype-based programming confusing and unpredictable to fix errors. | |||
=='''References'''== | =='''References'''== | ||
<references/> | <references/> | ||
Latest revision as of 00:43, 18 April 2014
Prototype-Based Programming
In object-oriented programming, inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both. Class based inheritance required objects to be defined as classes before they are created. In contrast, prototype-based inheritance consists of cloning existing objects which act as prototypes.<ref>http://en.wikipedia.org/wiki/Prototype-based_programming</ref> This programming style is also known as instance-base, prototype-oriented, or class-less programming.
Previous page on this topic: CSC/ECE 517 Spring 2013/ch1a 1e pi
Classical Inheritance vs. Prototype based inheritance
Object Creation
In classical inheritance, Objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes.
Classical inheritance example:
class Vehicle { //properties } class Car inherits Vehicle {} class trucks inherits inherits Car {} class medium Truck inherits Truck {}
In prototype based programming, a new object can be instantiated by simply creating a copy of/cloning an existing object.<ref>http://en.wikipedia.org/wiki/Cloning_(programming)</ref>
Prototype based inheritance example:
class Vehicle { //properties } Car.prototype = Vehicle.new(); Car.make =("Chevy"); Car.model=("Master Deluxe II");
Delegation
In class inheritance, methods are either defined in child classes or inherited from their parents. Delegation is a feature of prototype-based languages that use pointers to find the correct method or data during runtime. The delegation pointer establishes the link between parent and child methods. This allows child objects to be different in structure from their parents and be modified without affecting the parent.<ref>http://en.wikipedia.org/wiki/Delegation_(programming)</ref>
Advantages
Ease of creating object variety
In prototype-based programming, new objects can be quickly cloned and defined in individual lines of code. As well, multiple inheritance is made simple with prototypes compared to class inheritance. Individual data or methods in two unrelated objects can be associated to a child without requiring complete inheritance. This allows for a high density of different prototypes that can be easily maintained. <ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
class Bicycle{} class ElectricCar{} Scooter.prototype = Bicycle.new(); Scooter.power_source = ElectricCar.power_source; //An electric scooter is made from inheriting //data from two unrelated classes.
One-of-a-kind object support
Ruby provides a framework that can easily include one-of-a-kind objects with their own behavior. Since each object has named slots, and slots can hold state or behavior, any object can have unique state or behavior. Class-based systems are designed for situations where there are many objects with the same behavior. There is no support for an object to possess its own unique behavior, and it is awkward to create a class that is guaranteed to have only one instance. Ruby suffers from neither of these disadvantages. A unique object can be customized with its own behavior, and a separate "instance" is not needed.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
Amending objects
Using prototype-based programming, any object can be treated as a prototype, therefore the object can be extended without having to create a new object. In class-based programming, objects are committed to the functionality provided at instantiation. If the functionality of the class-based object later needs to be extended, a new object that has the desired functionality must instantiated and transfer the state between the two objects.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
Disadvantages
Prototype based inheritance requires dynamic typing
Because objects are being inherited rather than classes, it is necessary that the language support dynamic typing. Otherwise there is no way to determine the object's inherited properties. This increases performance overhead, as each time an object's function is executed, it must be ensured that the object is of the correct type for that function.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
Refactoring
In a prototype based system inheritance is built across individual lines of code instead of classes. Refactoring tools and IDEs cannot statically analyze code in a dynamic type system to refactor code.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
Testing for assurance
The classical approach offers static typing where the type of an object is determined at compilation. Since all languages that allow prototype based inheritance are dynamically typed, typically more testing is required to ensure correct behavior and that objects are of the correct type for methods they are called on.<ref>http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop</ref>
Convention
Class base inheritance is more widely used, making programmer unfamiliarity a possible issue. Programmers accustomed to static typing and class inheritance can find prototype-based programming confusing and unpredictable to fix errors.
References
<references/>