I'm looking for something that might be described as a "Scaffold design pattern".

I'm in the process of resurrecting an old piece of working but very buggy code that implements a Finite State Automaton in Java - you provide it with a model defined in XML and then you can throw events at it and query the resulting state or have it call back. It's far more complex than meets the eye because of composite states, complex synchbar and decision graphs, etc., etc.

The model is built in several stages: load the vertices from the XML source, link the vertices with transitions, check the model for consistency generating helpful errors and warnings. Then the model can be exercised in runtime.

Currently build-time and runtime code is found in the same class, e.g. CompositeState.java which seemed like a good idea at the time until the file size starting growing like topsy.

Is there a pattern I can use so I can build a complex object and then rip away all the build-time code like scaffolding to reveal a pristine, lean, runtime object?

The idea I have in my head is something like slicing the top off a class or something like that.

  • 1
    I'm not sure this is as much as a "pattern" as this is just plain old encapsulation. – T. Sar Mar 15 at 18:35
  • 1
    You might be interested in the book Working Effectively With Legacy Code by M. Feathers — it's about techniques you can apply to slowly restructure legacy code towards a better design — if I understood you well, you can apply these same techniques. Until you can get your hands on it, here's a summary of the key points I found online (I only skimmed through it, but seems decent). Key idea: find ways to introduce what Feathers calls a seam, and restructure in small steps behind such a seam. – Filip Milovanović Mar 15 at 19:38
  • @T.Sar Not really. For example, there's a whole bunch of code embedded in the class so that each object can read itself in from a JDOM file - pass each object an Element and it knows what to do. But JDOM/XML is out of fashion now so I'd like to add support for JSON or similar. I really don't want to add that logic to the class, instead I want to separate out the code so that each class has just the runtime logic. Does that make sense? – Keith Whittingham Mar 16 at 7:29
  • @FilipMilovanović Thx for that link. That's bedtime reading tonight! – Keith Whittingham Mar 16 at 7:31
  • @gnat No. I'm old! I know how to use design patterns but I don't have one to hand that solves my issue - an issue I've seen many times before. – Keith Whittingham Mar 16 at 7:33

Currently build-time and runtime code is found in the same class, e.g. CompositeState.java which seemed like a good idea at the time until the file size starting growing like topsy.

What you need isn't a single pattern. It's a principle. Separate use from construction. You don't have to rip away the scaffolding. Like NASAs Vehicle Assembly Building or a cars factory you use it get your vehicle and leave it behind. Few race car drivers win by dragging an assembly line around the track with them.

Metaphors aside, teaching objects how to build themselves is a popular yet doomed idea. Construction and behavior are separate concerns that cripple an object when smooshed together. It's far more flexible when the object doesn't care how it's made so long as it's invariants (think, validation of state) are respected.

If object construction is any more complicated then validating and accepting a few passed in values to become state then consider moving construction elsewhere.

Here is a host of patterns that help you do that:

  • 1
    A rule-of-thumb an old teacher of mine used: "When you start putting breakpoints on your constructor, there is a good chance it is too bloated and it is time to move its logic elsewhere." – T. Sar Mar 16 at 11:13
  • Yup. That looks like the direction I want to be headed in. I guess it will take me a few days but once I've got it implemented I'll edit this comment to outline my findings. – Keith Whittingham Mar 17 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.