samedi 11 décembre 2021

Dynamically instantiating linked object chain

I am implementing a chain of command pattern for a substring search. I can create the chain statically by writing something like this:

matchChain = new CharMatcher( new DotMatcher( new CharMatcher(null)));

where I explicitly declare what each character in the string is going to be to match the pattern Character Period Character ("c.c")

But my goal is to dynamically create this chain for any given key pattern. For example ("\`\`cccc.cc\`\`*c") would be:

matchChain = new CharMatcher( new CharMatcher( new CharMatcher( new CharMatcher( new CharMatcher(new DotMatcher( new CharMatcher( new CharMatcher( new StarMatcher( new CharMatcher(null)))))));

So the Chain class is an abstract class and each of the matcher classes extend Chain.

So how can I dynamically instantiate a chain based off of the length of the given key/pattern?

Aucun commentaire:

Enregistrer un commentaire