-2

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 Answers1

0

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:

candied_orange
  • 119,268