Radek Anuszewski
Radek Anuszewski

Reputation: 1910

Javascript lack of Interface - simple solution with Prototype

In Javascript, in contrast to other languages which OOP like Java, do not provide interfaces. There is some solutions on the internet which are more complex than mine, but I want to share with you my way how to resolve this problem, to get some constructive critisism from you and check if I choose the right way of thinking. I choose Answer your own question – share your knowledge, Q&A-style and you will see my answer below.

Upvotes: 2

Views: 2031

Answers (2)

HMR
HMR

Reputation: 39270

Here is an example that uses a timeout to check if the needed functions are implemented, you can implement multiple Interfaces.

Since JS is not a compile time type checked language you can't really have a good solution for this. Maybe you can have a look at mix ins (under mix ins), leave the default implementation or override.

function PersonInterface(proto,fnName){
  //after running the implements function it depends how quickly you're
  //  creating instances and how quickly you implement the functions
  setTimeout(function(){
    if(typeof proto.getSurName !== 'function'){
      throw new Error(fnName + ' has to implement getSurName');
    }
    //and others if needed
  },100);
}

function implements(fn, implements,fnName){
  implements(fn.prototype,fnName);
}

function Employer(){};
implements(Employer, PersonInterface,'Employer');
//depends how quickly you set the getSurName
//  and how quickly you will be creating Emplyer instances
//  trying to call getSurName
//  comment out the next line and you'll get 
//   Employer has to implement getSurName
Employer.prototype.getSurName=function(){};

Upvotes: 1

Radek Anuszewski
Radek Anuszewski

Reputation: 1910

My solution based on Javascript Prototype. I create a class which acts like interface by throwing Error object in methods which should be implemented, when child inherits from interface. Let's create PersonInterface:

function PersonInterface(){
}

PersonInterface.prototype.getName = function(){
    throw typeof(Error) !== 'undefined'? 
        new Error(" Interface Person: method getName() unimplemented!") 
        : " Interface Person: method getName() unimplemented!";
};
PersonInterface.prototype.getSurname = function(){
     throw typeof(Error) !== 'undefined'? 
          new Error(" Interface Person: method getSurname() unimplemented!") 
          : " Interface Person: method getSurname() unimplemented!";
};

Then let's create a Customer, which implements PersonInterface interface:

function Customer(name, surname){
   PersonInterface.call(this);
   this.__name = name;
   this.__surname = surname;
}
Customer.prototype = Object.create(PersonInterface.prototype);

Customer.prototype.getName = function(){
   return this.__name;
};

Customer.prototype.getSurname = function(){
   return this.__surname;
};

When getName() or getSurname() function are not implemented, it throws an Error object with corresponding message. I create a JSFiddle which demonstrates how it works: Javascript Interface with Prototype (click)

When you, for example, remove getSurname() method from Customer's prototype, it throws an Error - which you find in your browser console. I know that is simple solution and on the net we can find many solutions, but I think it can resolve problem when we don't need something more complex than simple interface.

Upvotes: 0

Related Questions