I read this article on Jon Skeet’s blog where he delves into the topic of performance on object pooling & thread safety.
In the article he makes a reference to SteppedPattern:
Most of our formatting goes through something called SteppedPattern – that’s basically one multicast delegate (with lots of actions) for formatting, and a list of delegates for parsing.
He demonstrates the above-mentioned format method:
public string Format(TResult value)
{
StringBuilder builder = new StringBuilder();
// This will call all the actions in the multicast delegate.
formatActions(value, builder);
return builder.ToString();
}
I’ve never heard of the term before and can’t really find any information available online for SteppedPattern (most searched have lead me to Wikipedia’s Software design pattern page)
Does anybody have more information on SteppedPattern, specifically:
- how it is defined, and
- where it would be applied?
1
It’s just a class within Noda Time. It’s not a specific design pattern that I could name.
It’s used to build up a text parsing/formatting pattern which consists of a number of steps. When formatting a value, we basically start off with a value (e.g. a date) and an empty StringBuilder
, then apply each step in turn, supplying each step with the current builder and the date. So for example, to come up with a value of “2015-02-24” you’d have stepped pattern with five format actions:
- Append the year as a four-digit value
- Append the literal ‘-‘
- Append the month-of-year as a two-digit value
- Append the literal ‘-‘
- Append the day-of-month as a two-digit value
The parser works in the exact same manner, but it starts off with a cursor over the supplied text value (which each parse step advances) and a bucket for the parse steps to store data in (e.g. it first parses the year and remembers that, then skips over the ‘-‘, etc). Finally, it converts the bucket into a LocalDate
(or whatever).
I have pattern parsers to convert something like "yyyy-MM-dd"
into a stepped pattern (containing both parse and format actions) – the idea is that you parse the pattern specifier once, and then you can reuse the parse/format actions multiple times efficiently.
1