In my node.js application, I have a queue class which has push
and pop
methods and a data
property. I have an Event
class which handles an event and pushes it on to the queue.
If I think object oriented, is the relationship between queue and event better modeled as "is a" (inheritance) or "has a" (composition)?
First approach:
var util = require('util'),
array = [];
function Q(source){
this.data = source;
}
Q.prototype.push = function(x){
this.data.push(x);
};
Q.prototype.show = function(){
console.log(this.data);
};
function Event(y){
Q.call(this, y);
}
util.inherits(Event,Q);
Event.prototype.trigger = function(bb){
//some logic
this.push(bb);
};
var x = new Event(array);
x.trigger('hi');
x.show();
Second approach :
var array = [];
function Q(source){
this.data = source;
}
Q.prototype.push = function(x){
this.data.push(x);
};
Q.prototype.show = function(){
console.log(this.data);
};
function Event(y){
this.arr = new Q(y);
}
Event.prototype.trigger = function(bb){
//some logic
this.arr.push(bb);
};
var x = new Event(array);
x.trigger('hi');
x.show();