These are my requirements:
The framework owns the collection of "Things". Like this:
class Framework {
void addAThing(Thing t)...
}
A "Thing" is probably a composition of other stuffs. So it must have a Stuff getStuff() method. The framework uses a Things Stuff sometimes.
interface Thing {
Stuff getStuff() ;
}
So one implementation of Thing is the simple version that gets stuff from the outside.
class Thing1 implements Thing {
private final Stuff myStuff ;
public Thing(final Stuff myStuff) { this.myStuff = checkNotNull(myStuff) ; }
public Stuff getStuff() { return this.myStuff }
}
The problem I have is that there can be many different Things, and the actual creation of Stuff is something that I want the subclasses to be able to implement. So I'll help them along with this:
abstract class ParentThing implements Thing {
private final Stuff myStuff ;
public ParentThing() {
this.myStuff = checkNotNull(createStuff());
}
public Stuff getStuff() { return this.myStuff }
protected abstract Stuff createStuff() ;
}
The problem with that is that I'm relying on subclasses to be very nice about the implementation of createStuff() since the method is called in the constructor. It shouldn't for example register "this" as a callback to some other thread in createStuff since "this"-instance might not have left the constructor when the callback occurs.
So maybe this then:
interface Thing {
void initialize() ;
Stuff getStuff() ;
}
and then I'll fix it so that framework calls initialize before it uses the Thing, like so
class Framework {
void addAThing(Thing t){ t.initialize(); ... }
}
But then I can't use final in the ParentThing class anymore, leaving me with another state of Things, "created but not initialized yet".
So maybe I actually should have
interface ThingBuilder {
Thing createAThing() ;
}
and then
class Framework {
void addAThing(ThingBuilder t)...
}
Might not be so bad with closures, but without closures/lambda it gets a bit messy on the client side.
Ideas are welcome. How to design an OO API?
void addAThing(Thing t){ t.initialize(); ... }
}
But then I can't use final in the ParentThing class anymore, leaving me with another state of Things, "created but not initialized yet".
So maybe I actually should have
interface ThingBuilder {
Thing createAThing() ;
}
and then
class Framework {
void addAThing(ThingBuilder t)...
}
Might not be so bad with closures, but without closures/lambda it gets a bit messy on the client side.
Ideas are welcome. How to design an OO API?