Servoy Tutorial: Parasitic Inheritance

Servoy Tutorial: Parasitic Inheritance

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.

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.

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.

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.

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.

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.

Gary Dotzlaw
Gary Dotzlaw has 20+ years of experience as a professional software developer and has worked with over 100 companies throughout the USA and Canada. Gary has extensive qualifications in all facets of the project life cycle, including initial feasibility analysis, conceptual design, technical design, development, implementation and deployment. Gary is an expert Servoy Developer, with extensive large-commercial project experience, and has written numerous advanced tutorials on Servoy development.