Skip to content Skip to footer

Servoy Tutorial: Inheritance Patterns

Servoy Tutorial: Inheritance Patterns
Servoy Tutorial Photo Credit: dmjames58 via Compfight

This is an object-oriented Servoy tutorial on how to use inheritance patterns in Servoy. I use objects a lot, so I thought it would be a good idea to introduce you to a few basic inheritance patterns and how to use them with Servoy. If you have not yet read my article on “Object-Oriented Programming” in Servoy, you should read that first, as it is the foundation that this article builds upon. You will find the link in the “Related Posts” at the end of this article.

For our purposes, inheritance can be thought of as new objects that inherit the properties and methods of an initial object that is returned by a constructor.

Below are the three most common patterns for creating new objects using a constructor. The conventional patterns have been modified to accommodate Servoy’s unique needs, which handles JavaScript files differently from a conventional browser.

 1. Prototype Pattern

The prototype pattern consists of two main parts; a constructor and a prototype. The constructor contains the properties that will be unique for each new object created using the constructor. The prototype can contain properties and functions (methods) that will be shared among all objects created using the constructor; only one copy exists in memory. In other words, the properties and functions are defined once per prototype, and are shared among all the objects created with the constructor.

The first step is to define the constructor and put all the properties into it that need to hold unique values for each occurrence of a new object. We define the properties by using the JavaScript this keyword, and assign the parameters passed to the constructor as the property values. In our example, we are building a fruit constructor, and the unique properties for each fruit object will be things like type of fruit, color of fruit, rating and price. We will be able to pass in parameters to this constructor and get back a new fruit object with specific properties values, like those of an orange or apple.

// Create the basic object
function Fruit1(name, color, rating, price) { 
	this.name = name; 
	this.color = color;
	this.rating = rating;
	this.price = price;
}

The next step is to create the prototype, adding the additional properties and functions that will be shared among all the objects created with the constructor. Any references by functions to properties defined in the constructor must use the JavaScript this keyword. The functions in the prototype go in the object literal, but are written so that the name of the function is like a property. In our example, we have defined two functions on the fruit prototype, each of which access properties in the constructor and return a concatenated string.

For Servoy to see the prototype, we need to define the prototype inside of an immediately invoked function, and assign it to a variable. When Servoy reads this JavaScript file, it will see the immediately invoked function, execute it, and assign the prototype into memory. That’s all we have to do to get Servoy to recognize the prototype.

//Adding to the protoype chain
var foo1 = function(){
	Fruit1.prototype = {
		type: function () { 
			return 'I am a ' + this.color + ' ' + this.name; 
		},
		getInfo: function(){
			return 'Rating: ' + this.rating + ', Price: ' + this.price
		}
	};
}();

The final step is then to create a new object using the constructor by passing in the parameters that define our piece of fruit. The constructor creates the object, sets the properties of the object created to the values of the parameters we passed in, and returns to us the object with the prototype already attached. We can then reference the properties in the new object using the object.property syntax, or call a method on the prototype using the object.method() syntax.

// Normal prototype pattern
var oApple = new Fruit1('Apple', 'Red', 5, 0.10);

application.output("Name: " + oApple.name); 
// Name: Apple

application.output("Type of Fruit: " + oApple.type()); 
// Type of Fruit: I am a Red Apple

application.output("Details: " + oApple.getInfo()); 
// Details: Rating: 5, Price: 0.1

Its important to realize that you can override properties and methods by re-defining them on the object you created with the constructor. Objects are mutable, so you can change them at any time.

The prototype pattern works quite well in Servoy, and is highly efficient, but its not the only game in town.

 2. Revealing Module Pattern

The revealing module pattern is one of my favorites, because it is quite compact, and has less reliance on the JavaScript this keyword. It does not separate the constructor and the prototype, and hence it creates a unique instance of all the properties and functions for each object created with the constructor. Although it is not as memory efficient as the other two patterns discussed here, it is ideally suited for instances when only a small number of objects will be created from the constructor.

//Revealing module pattern
function Fruit2(name, color, rating, price) { 
	var _name = name, 
		_color = color,
		_rating = rating,
		_price = price,
		type = function () { 
			return 'I am a ' + _color + ' ' + _name; 
		},
		getInfo = function(){
			return 'Rating: ' + _rating + ', Price: ' + _price
		};

		return {
			type: type,
			getInfo: getInfo
		};
}

What this pattern really does is it allows you to make certain properties and functions private, and only reveal public methods, like an api. Only the properties and functions specified in the return object will be public, all other inner workings of the object are hidden and not accessible externally. In our example, you can see that only the two functions are public and exposed to external access. Attempting to reference one of the private properties will result in “undefined”.

// Revealing module pattern (Works)
var oApple = new Fruit2('Apple', 'Red', 5, 0.10);

application.output("Name: " + oApple.name); 
// Name: undefined

application.output("Type of Fruit: " + oApple.type()); 
// Type of Fruit: I am a Red Apple

application.output("Details: " + oApple.getInfo()); 
// Details: Rating: 5, price: 0.1

The revealing module patterns works extremely well with Servoy and is perfect for when you are creating a small number of objects and you want to restrict what properties and functions are available externally, like in an api.

 3. Revealing Prototype Pattern

The revealing prototype pattern combines the two patterns we have examined so far. It allows us to have private and public properties and functions, as well as being highly efficient by using the prototype to share a single copy of the functions among all object created with the constructor. This is the trickiest pattern to do in Servoy, so pay close attention.

The first step is again to define the constructor. As before, it will accept the parameters and set the properties using the JavaScript this keyword. All the properties in the constructor are public and are accessible.

// Basic constructor
function Fruit3(name, color, rating, price) { 
	this.name = name; 
	this.color = color;
	this.rating = rating;
	this.price = price;
}

The next step is to define the prototype. In Servoy, we have to do this using an immediately invoked function that is assigned to the prototype, that is in turn assigned to a variable. This allows Servoy to see the variable, assign the function to the prototype, execute the function, and store the prototype into memory as the prototype of the constructor. Whew, pretty complicated, right?

// Add methods to the prototype chain
var foo4 = Fruit3.prototype = function() {
	// Private (could have more private methods)
	var type = function () { 
		return 'I am a ' + this.color + ' ' + this.name; 
	},
	getInfo = function(){
		return 'Rating: ' + this.rating + ', Price: ' + this.price
	};
	// Public members
	return {
		type: type,
		getInfo: getInfo
	};
}();

Like with the revealing module pattern discussed earlier, properties and functions can be defined in the prototype and will remain private. Only the members you choose to include as public member in the return will be excisable. All your other properties and methods that make-up the internal workings of your code are never exposed, but you still have everything in one place for easy maintenance and scalability.

// Revealing prototype pattern (Does not work)
var oApple = new Fruit3('Apple', 'Red', 5, 0.10);
application.output("Name: " + oApple.name); 
// Name: Apple

application.output("Type of Fruit: " + oApple.type()); 
// Type of Fruit: I am a Red Apple

application.output("Details: " + oApple.getInfo()); 
// Details: Rating: 5, price: 0.1

As you can see, these patterns can easily be used for object-oriented programming in Servoy. They offer many advantages over conventional “function spaghetti code” programming that can quickly become confusing, difficult to maintain, and extend. Objects offer enormous power; build once and use again and again. They are mutable and can easily be changed to suit unique variations, and they are very easy to work with. All your code is in one place, easy to maintain, easy to extend, and elegant.

What are you waiting for? Crank-up that Servoy Developer and start using OOP in your code today.

Leave a comment

OUR SERVICES

AI Integration and Agent Development

We design, prototype, and deploy AI agents that streamline operations and enhance decision-making. Our expertise includes:

  • Custom AI Agent Development: Crafting autonomous systems using tools like n8n, Flowise, and PydanticAI.
  • Workflow Automation: Reducing operational inefficiencies through scalable, automated solutions.
  • Machine Learning Integration: Building predictive models to uncover actionable insights.
  • AI Adoption Consulting: Guiding businesses through the process of integrating AI technologies to achieve their strategic goals.

Expert Prompt Engineering

Effective prompts unlock the full potential of AI systems. We specialize in:

  • Custom Prompt Design: Developing precise and context-aware inputs to guide AI systems effectively.
  • Advanced Prompt Patterns: Using techniques like persona-based prompts, two-shot prompting, and reflection prompts for superior outcomes.
  • Industry-Specific Solutions: Tailoring prompts to solve challenges in fields like finance, healthcare, retail, and more.

Data Solutions and Insights

Unlock the power of your data with our comprehensive solutions:

  • Data Mining and Analysis: Extracting valuable insights to drive informed business decisions.
  • Data Visualization: Creating clear and interactive dashboards for impactful storytelling.
  • Advanced SQL Expertise: Leveraging MS SQL and PostgreSQL to build robust, scalable database solutions.

AI-Powered Project Management

Managing complex AI projects from start to finish:

  • End-to-End Project Management: From initial strategy to deployment, ensuring seamless execution.
  • Cross-Functional Collaboration: Bridging technical and business teams to align project goals.
  • Strategic Planning: Delivering AI solutions that are on time, within budget, and aligned with business objectives.

Why Choose Dotzlaw Consulting?

With over 20 years of experience in AI, software development, and workflow optimization, we combine technical expertise with business insight to deliver transformative results. Let us help you unlock the full potential of AI to drive growth and innovation.

Ready to Get Started?

Let’s collaborate to achieve your goals. Contact us today to explore how AI can transform your business.


Our Skills:

[bra_graph_container]
[bra_graph Title=’Artificial Intelligence (AI) Consulting’ Percent=’95’]
[bra_graph Title=’Prompt Engineering’ Percent=’90’]
[bra_graph Title=’Workflow Automation’ Percent=’90’]
[bra_graph Title=’Python’ Percent=’90’]
[bra_graph Title=’SQL’ Percent=’85’]
[bra_graph Title=’Data Mining and Visualization’ Percent=’80’]
[bra_graph Title=’Machine Learning’ Percent=’80’]
[bra_graph Title=’Servoy’ Percent=’99’]
[/bra_graph_container]

Core AI and Consulting Skills

[bra_list style=’arrow-list’]

  • Artificial Intelligence (AI) Consulting: Delivering transformative AI solutions tailored to business challenges.
  • Machine Learning: Expertise in designing, training, and deploying machine learning models.
  • Generative AI Workflows: Optimizing workflows with generative AI technologies for scalability and efficiency.
  • Predictive Analytics: Leveraging data to forecast trends and improve decision-making.
  • Data Mining and Analysis: Uncovering actionable insights from complex datasets.

[/bra_list]

Workflow and System Modernization

[bra_list style=’arrow-list’]

  • Workflow Automation (n8n, Flowise): Automating processes with cutting-edge tools to streamline operations.
  • Legacy System Modernization: Upgrading outdated systems using modern frameworks and AI-driven approaches.
  • AI Agent Development: Prototyping and scaling AI agents from concept to production.
  • Scalable Workflow Design: Crafting workflows optimized for efficiency and growth.
  • Business Process Optimization: Transforming processes to improve productivity and reduce costs.

[/bra_list]

Technical Expertise

[bra_list style=’arrow-list’]

  • Python, Java, JavaScript Programming: Proficiency in versatile programming languages for AI and software development.
  • Expert Servoy Programmer: Advanced expertise in Servoy development for specialized client needs.
  • AI Agent Prototyping with n8n and Flowise: Rapid prototyping using state-of-the-art AI agent development tools.
  • AI Agent Scalable Production Solutions with PydanticAI: Delivering robust, production-ready AI solutions.
  • Advanced AI Prompt Engineering: Crafting high-performing prompts for generative AI applications.

[/bra_list]

[bra_button text=’Contact Us’ url=’https://dotzlaw.com/clone/contact-me’ target=’_self’ size=’medium’ style=’rounded’ color=’tealgreen’]