..
The polymorphism is a programming technique that allows the use of parts of source code, while remaining unchanged, to generate run-time behaviors.
Creating polymorphic code has a specific meaning in the object-oriented programming: it means creating a taxonomy of all classes that implement an interface.
So if, for example, my interface defines a method "getArea", each class that will implement this interface will have a method "getArea": this allows us to write polymorphic methods, methods that can change their algorithm execution Depending on the type of object that is passed as an argument.
Polymorphism in the traditional OOP languages
In Java - but the same is true for C + + and any other language, Object Oriented (OO) completed, we will then hand the case of Ruby - for example:
interface IFormaGeometrica
{
void getArea ();
}
public class Triangle implements IFormaGeometrica
{
@ Override
public int getArea ()
{
return (* this.base this.altezza) / 2;
}
}
In this case we define the interface IFormaGeometrica which states that every object that "is" a FormaGeometrica getArea will have a method - such as the Triangle class, which is a FormaGeometrica, has its own implementation of getArea, which allows us to write a program able to calculate the area of any geometric shape, whether that is now present in the code and that will be implemented in the future, without changing the original source code.
In fact, if I write a class Calculator:
public final class Calculator
{
public static void main (String [] args)
{
Collection forms <IFormaGeometrica> =
<IFormaGeometrica> new ArrayList ();
forme.add (new Triangle ());
forme.add (new Square ());
forme.add (new Pentagon ());
for (IFormaGeometrica g: form)
{
System.out.println (g.calcolaArea ());
}
}
}
This can take as input any collection of geometric shapes, provided that each object in the collection must implement the interface IFormaGeometrica, and that has essentially a method getArea.
This example in Java is purely academic: in fact, probably in the constructor of each class, we will include input measures of the sides of the geometric shape, the slant, and so on.
The objective is achieved: we wrote a class that can print to video the area of any geometric shape. I repeat: this is the code that has already been implemented (as in the case of triangle), which is being implemented in the future.
Not only: if the implementation of the calculation of the Triangle area contained a bug, I can change the class without having to rewrite the class Triangle Calculator. Maybe it was deployed on a server that needs to reboot every change: in this case not having to change the code is a major advantage. But other than that, it is always advantageous to limit the parts of code that "change" because every change brings with it potential bugs. Conversely, it is a good programming practice to always know with certainty which parts of code that remain unchanged.
What happens "behind the scenes" is that the compiler makes sure that every object in the polymorphic code of the main method of the class Calculator implements the interface. In this way, they say is an expression using the methodology of design by contract, "the contract is respected."
If not, if that is in "forms" have an object of a class that we would not implement IFormaGeometrica an error at compile time, or does not manage to run the program, because the compiler would notice the error.
The polymorphism in Ruby
But what happens in Ruby? And 'possible in Ruby, which is not compiled but interpreted language, writing polymorphic methods?
Yes, it is certainly possible, but there is a profound difference "philosophical" than OO languages and compiled, which
we could describe it. OO programming in Java and classic, to establish that an object belongs to a particular type of objects (that implements a certain interface) need to expressly object to derive from a parent class: basically you need to use the techniques of inheritance (inheriting by a class, an abstract class or an interface).
It would be like saying: To determine whether this is a duck in front of me, I take his DNA and study in the laboratory to see if it is to duck.
In Ruby you are using the "duck test" (duck test) invented by James Riley (see http://en.wikipedia.org/wiki/Duck_typing ):
if it walks like a duck and quacks like a duck, then it is a duck.
(Which incidentally is what makes each of us when he sees a duck).
What does this mean? It means that in Ruby, and more generally in object-oriented scripting languages, as well as Python and Perl, no need to specify the interface, neither explicit inheritance relationships between classes.
The interpreter simply "trust" that the programmer, the method steps when a polymorphic object that "should have" a certain way, we actually have the.
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |