我有3个班级:
function A(data){
this.data = data || [];
}
A.prototype.remove = function(index){
// remove index from this.data;
};
function B(data){
this.data = data || [];
}
B.prototype.add = function(x){
this.data.push(x);
};
B.prototype.update = function(index, x){
this.data[index] = x;
};
function C(data){
this.data = data || [];
}
C.prototype.add = function(x){
this.data.push(x);
};
var a = new A(xx);
var b = new B(xx);
var c = new C(xx);
There relations are:
if I call a.remove(i)
to remove an element, b
will add it to its data
.
if I call b.update(i, x)
, c
will add x
to its data
.
How could I design this with smallest coupling?
ps: 这只是表明情况的一个实例,请不注意初始化等。