Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

11
  • 9
    I like the first two solutions (+1), but I don't think your last snippet with its repetition of the methods in FooManager and in UninitialisedImpl is any better than repeating if (!initialized) throw;. Commented Apr 14, 2016 at 23:30
  • 3
    @Bergi Some people love classes too much. Although if FooManager has a lot of methods, it might be easier than potentially forgetting some of the if (!initialized) checks. But in that case you should probably prefer to break up the class. Commented Apr 15, 2016 at 0:19
  • 1
    A MaybeInitializedFoo doesn't seem better than an initialized Foo too me, but +1 for giving some options/ideas. Commented Apr 15, 2016 at 2:37
  • 7
    +1 The factory is the correct option. You can't improve the a design without changing it, so IMHO the last bit of the answer about how to half are it is not going to help the OP. Commented Apr 15, 2016 at 7:06
  • 6
    @Bergi I have found the technique presented in the last section to be tremendously useful (see also the “Replace conditionals through Polymorphism ” refactoring technique, and the State Pattern). It is easy to forget the check in one method if we use an if/else, it's much harder to forget implementing a method required by the interface. Most importantly, the MainImpl corresponds exactly to an object where the initialize method is merges with the constructor. This means the implementation of MainImpl can enjoy the stronger guarantees this affords, which simplifies the main code. … Commented Apr 15, 2016 at 7:27