Interfaces in object oriented programming languages and prototype-based languages
Today we talk interfaces. Interfaces are especially important in Object Oriented Programming(OOP) and the programming languages that espouse this paradigm. One of the programming languages that employ OOP is Python.
Before we go about explaining what interfaces are, it is important to define OOP first and how it is structured. OOP consists of classes which are entities that contain attributes and methods that perform specific tasks based on what the class is meant for. Classes are used to construct objects which are instances of classes. These objects then inherit attributes and methods from their parent classes.
For example, a button class with attributes such as color, size and methods that perform actions like allowing users to download files or perform operations on databases. Now, different button objects can be constructed from the button class and although they will inherit a set of attributes and methods, they will implement them differently in differing scenarios.
Having defined classes, it is time to focus on interfaces. Interfaces define the methods and properties that classes must implement albeit, interfaces do not harbor code to construct objects. In colloquial terms, this means that interfaces tell classes what to do but not how to do it. The advantage of using interfaces is that they allow implementations of methods to be abstracted from users when creating object instances. Furthermore, interfaces make sure that classes are built according to rules that outline the methods and properties to be implemented. The Python code snippet below depicts an interface:
JavaScript(JS) unlike Python, is not an OOP language. Instead, JS is a prototype-based language. JS therefore does not implement classes and thus will technically not be able to use interfaces. Although JS is not an OOP language, it has its own implementation of OOP which instead of using classes to construct children objects, prototypes are used. The keyword 'class' used in OOP languages is also employed in JS prototyping. A prototype is a constructor function. With prototyping, object instances bearing the properties and methods of parent constructor functions can be spawned. Below is a JS code snippet depicting a constructor function and its object instance:
Although JS does not have the built-in resources to implement interfaces, there are ways to get around this and emulate interfaces. The ability to emulate interfaces is possible due to the flexibility of JS. There are three ways in which one can emulate interfaces, namely: comments, attribute checking and duck typing.
Commenting involves adding a commented out instruction in JS code for other programmers to follow to implement the specified methods in constructor functions. The drawback with this method is the lack of accountability that comes with it, that is, no checks are done to ensure that interface methods are implemented.
Attribute checking offers some accountability. All classes explicitly declare which interfaces they implement, and these declarations can be checked by objects wanting to interact with these classes. The interfaces themselves are still just comments, but you can now check an attribute to see what interfaces a class says it implements.
You will see errors if a class does not declare that it supports a required interface. You can enforce that other programmers declare these interfaces. The main drawback to this approach is that you are not ensuring that the class really does implement this interface. You only know if it says it implements it. It is very easy to create a class that declares it implements an interface and then forget to add the required method. All checks will pass, but the method will not be there, potentially causing problems in your code.
In the end, it doesn't matter whether a class declares the interfaces it supports, as long as the required methods are in place. That is where duck typing comes in. Duck typing was named after the saying, “If it walks like a duck and quacks like a duck, it's a duck.” It is a technique to determine whether an object is an instance of a class based solely on what methods it implements, but it also works great for checking whether a class implements an interface. The idea behind this approach is as such: if an object contains methods that are named the same as the methods defined in your interface, it implements that interface. Using a helper function, you can ensure that the required methods are present. Duck typing differs from the other two approaches in that it uses no comments.
While probably being the most useful of the three methods, duck typing still has some drawbacks. A class never declares which interfaces it implements, reducing the reusability of the code and not self-documenting like the other approaches. It does not check the names or numbers of arguments used in the methods or their types, only that the method has the correct name...



Comments
Post a Comment