3 ms·
The proper way to solve the first problem is to take one of your two abstract class and push its public methods up into an interface. Hooray! Now you have mul
by Stormbringer 16y ago
The proper way to solve the first problem is to take one of your two abstract class and push its public methods up into an interface. Hooray! Now you have multiple inheritance again.
This is actually a pretty good pattern for any significant class which you expect to do subclassing on:
Take the class, promote the shared code up into an abstract class, then promote the high level methods up into an interface.
The only real drawback is that you've got to come up with three different names now for the same thing. Please for the love of all that is holy don't do the horrible thing where you shove I on the front of the name to indicate an Interface. Hungarian notation has no place in a proper OO design.
For the closure examples, I can't help but think that it would actually be simpler and cleaner just to have an external class, give it a good name, and then pass whatever info you need in the constructor. Or do chained initialisation (whatever floats your boat).
(Which is where your setters return self, so that you can do this:
x.setName(arg[0]).setFilename(arg[1]).setFlags(arg[2]);
and instead of
public void setName(String name) {
this.name = name;
}
you have
public Foo setName(String name) {
this.name = name;
return this;
}