dimanche 27 décembre 2015

Is there a commonly accepted design pattern for base methods implementing "early exit" functionality?

I have a class hierarchy of patterns: patterns are split into simple patterns and compound patterns, both of which have concrete implementations.

Patterns have a Match method which returns a Result, which can be a Node or an Error.

All patterns can check for a memoized result when matching. Simple patterns return an error on EOF.

Is there a pattern that allows a more simple way to reuse implemented functionality than mine? Let's say we're using a single-inheritance, single-dispatch language like C# or Java.

My approach is to implement Match at pattern level only and call a protected abstract method InnerMatch inside it. At simple pattern level, InnerMatch is implemented to handle EOF and calls protected abstract InnerInnerMatch, which is where concrete implementations define their specific functionality.

I find this approach better than adding an out bool handled parameter to Match and calling the base method explicitly in each class, but I don't like how I have to define new methods. Is there a design pattern that describes a better solution?

Aucun commentaire:

Enregistrer un commentaire