In java, I might have an object a with a bunch of properties like getColor. I then might make a second object b (possibly of the same class) that behaves similarly to object a in the sense that it does the same thing as a unless one of it's values is specifically changed (and unchanged, too).
a = new Foo()
b = new FooWhichRefersToAnotherFoo(a)
a.getColor() # returns blue
b.getColor() # returns blue
a.setColor(red)
b.getColor() # returns red
b.setColor(pink)
b.getColor() # returns pink
a.getColor() # returns red (unchanged)
b.deleteChangeToColor()
b.getColor() # returns red as it is now pointing back to a
I would assume it would be some kind of tree hierarchy as if I had c pointing to b pointing to a, the object would work up the chain to the first specified value or the default value of the original unparented object.
Does a pattern like this exist similar that works well with java? I would expect the first class would be one class, and the second would be an inherited class, which keeps track of which class it's instancing, and if it's own property is not set, queries the parent object.
I figure I can do this myself where every class I make, I can create a separate class like
class Foo {
Color color = new Color("red");
Color getColor() { color }
}
class FooInstance extends Foo {
Foo parent = null;
FooInstance(Foo parent) {
this.parent = parent;
}
Color getColor() {
if (color == null) return parent.getColor();
else return color;
}
}
but wanted to make sure there wasn't some easier mechanism for that like using javabeans or something. One problem with inheritance is it exposes all the methods of the parent, while I'd like to possibly specify which ones are actually available in the child, so maybe just a separate class altogether?
I've read your post a couple of times and there are some things about it that still confuse me. For example, in your a/b/c example you talk about behaviour being the same unless a value is different. I think you need to separate the concepts of behaviour and state more clearly. Behaviour being what a class will do, state being the values of it's properties. The behaviour of a class if often dependant on the state, i.e.:
Color getColor() {
if (color == null) return parent.getColor();
else return color;
}
But they are two different things. From your examples I don't think you would need two or more different classes. You could recode Foo and FooInstance as a single class as follows:
class Foo {
Foo parent = null;
Color color;
Foo(Foo parent) {
this.parent = parent;
}
Color getColor() {
//If there is no parent we have to return color regardless.
if (parent == null) {
return color;
}
// If there is a parent we can choose which to return.
return color == null ? parent.getColor() : color;
}
void setColor(Color color) {
this.color = color;
}
}
Unless you needed different behaviour from your FooInstance, you can do what you require with a single class.
I don't know of any third party API which provides this sort of data structure. But they may exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With