This is not specific to any language, but I'm working with javascript so I'll use javascript.
I have a "class" SimpleObject, and another class CompoundObject. They have the similar functionality and almost similar interfaces. Take the following example (simplified and irrelevant stuff removed):
SimpleObject:
This is just a base class, and init is overridden on child classes to provide other functionalities
function SimpleObject(data){
this.data = data;
this.init();
}
SimpleObject.prototype = {
init: function(){
// Irrelevant data processing code here
},
getData: function(){
return this.data;
}
}
CompoundObject:
CompoundObject is made up of different kinds of SimpleObject. The simple objects interact with each other, and the compound object handles that internally.
function CompoundObject(simpleObjectsArray, data){
this.simpleObjectsArray = simpleObjectsArray;
this.init(data);
}
CompoundObject.prototype = {
init: function(data){
// Irrelevant data processing code here
},
getData: function(){
var array = [], i;
for(i = 0; i < this.simpleObjectsArray.length; i++){
array[array.length] = {
simpleObjectType: // some value here,
data: this.simpleObjectsArray[i].getData();
};
}
return this.data;
}
}
As you can see, they have similar structure, and kind of a similar return type for getData, the only difference is SimpleObject only returns the data, while CompoundObject returns the data + some other properties of each internal SimpleObject.
Now, the value of getData is going to be used in a lot of other classes, mostly UIs. Since they are similar, the UIs(or rather, the visuals) for them are very similar. The problem is they are similar enough to use the same visuals, but different enough to warrant different classes to handle them. I want to design them so they can just use the same UI class, but maintain decoupling.
Pardon me if the question might be a bit confusing, as I myself is confused too.
Aucun commentaire:
Enregistrer un commentaire