
Servoy Tutorial Photo Credit:
Matthias Lenke via Compfight
This is an object-oriented Servoy tutorial on how to use parasitic inheritance with Servoy. Parasitic inheritance is a pattern that is courtesy of Douglas Crockford, and represents a constructor function that creates a new object by taking all the functionality from another object, augmenting the new object with additional properties and methods, returning the new object, and pretending it did all the work.
Similar to my article on multiple inheritance, we are going to use objects to represent cars at a dealership. My examples are for demonstration purposes only, and do not represent how you would build objects to handle a real-world business case, so don’t judge.
You should probably have read my prior article on object-oriented programming, as it gives a more gentle introduction to the world of object-oriented programming with Servoy.
We start off by defining the vehicle constructor function. Ideally this constructor would contain the properties that are common for all types of vehicles.
1 2 3 4 5 6 7 8 9 | function Vehicle(){ var vehicle = { name: 'unknown', make: 'unknown', color: 'unknown', mgp: 0 }; return vehicle; } |
Since cars differ from trucks and vans, we could create another constructor function for cars. Here we can add properties, and functions, that are important for cars. This car constructor has no idea that the object it will be used to create is going to fall victim to parasitism.
1 2 3 4 5 6 7 | function Car(){ var vehicle = new Vehicle(); var car = objectPlus(vehicle, { type: 'unknown' }); return car; } |
This is the objectPlus function which is used to make a copy of the vehicle object and augment it with our properties for the car. You should take this code and put it in your utils library; you will use it a lot with objects.
1 2 3 4 5 6 7 8 9 10 11 12 | function objectPlus(obj, stuff) { var n; function F(){} F.prototype = obj; n = new F(); n.uber = obj; for (var i in stuff){ n[i] = stuff[i]; } return n; } |
Now we create a new car by calling the car constructor function. The sports car constructor then uses the new car object when called, to make a copy of the car with all its inherited properties using objectPlus, storing the newly created copy in the variable called that. It then sets properties with the parameters passed in, and mutates the new object by adding additional properties, and a getInfo function. It returns the final object, pretending that it did all the work, masking car and vehicle’s contributions. This is referred to as parasitic inheritance, and it works well in Servoy. Keep in mind that the code snippet immediately below has to be kept together in the same .js file, but it does not have to be in the same .js file, or even the same module for that matter, as the code that calls the sports car constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var car = new Car(); function sportsCar (name, make, color, premiumPaint, mpg){ var that = objectPlus(car); that.name = name; that.make = make; that.color = color; that.premiumPaint = premiumPaint; that.mpg = mpg; that.getInfo = function(){ return this.color + " " + this.make + " " + this.name + " " + this.type + " - " + this.mpg + "mpg" + " - " + this.premiumPaint; }; return that; } |
Here is how to use the sports car constructor. We call the constructor with our parameters, and we get back a Corvette object that contains everything from sports car, car, and vehicle.
1 2 3 4 | var s = sportsCar('Corvette', 'Chevy', 'White', 'Pearl White', 10); application.output(s.getInfo()); //White Chevy Corvette Sports Car - 10mpg - Pearl White |
That’s all there is to using parasitic inheritance in Servoy. Hopefully this Servoy tutorial puts another tool in your tool belt of object-oriented programming patterns, one that you will find useful in your career as a Servoy developer. I encourage you to learn more about using objects in Servoy, as it will help you write more manageable, extendable and elegant code.