Which pattern should I use?
Hi All, Here’s the requirement; “Make this product available for ordering to clients of (category 1) or (of age over 24 and not in region A)” The above seems like a SQL query, but it really is not. Ok! maybe it is. In reality it’s a DSL that will act as a filtering engine. Here’s the thing, I have a catalogue of products that I want to make available to specific clients. Much like we see in pricing rules in e-commerce platforms I would like to give the end user capabilities of creating these rules. So the final result should be a dynamic catalogue displaying exactly what I tell it to display depending on the user. It really is like an e-commerce up sale, cross sale except that it’s the end user that creates these rules, not the actual sales. Normally I would pull together some “if else” statements and encapsulate it into a class and voila. But then I remembered that my client not only uses these kind of rules to limit what kind of product can be available to whom, I also discovered that in a different modules he uses a messaging system that are sent using exactly the same rules. So we can easily transform the above DSL into something like this: “Send a message to clients of (category 1) or (of age over 24 and not in region A)” So I decided to write something reusable that will be used on the application level rather than module level. A year ago I decided to invest more into learning design patterns, so my mind is eager to use more design patterns to solve my problems, fully aware that design patterns can be more like a thorn in the side if not used properly, however I also became aware of their capabilities. And something is telling me that Design pattern is the way to go in this case. I have been trying to find the suitable pattern for this particular problem and I think I narrowed it down to GoF Interpreter pattern. Other suggested that I use WF but I’m leaning more that the GoF pattern. Now the real problem comes; even if I know what the problem is, and what pattern I should be using to solve it, I still have no clue where to begin and how to code it! I could not find a decent example on the web to base myself on it. I am hoping that the members of this forum can point me in the right direction, since I’m fairly new to design patterns. Although I’m pretty sure that the Interpreter pattern is the suitable one, but I would be more that grateful if you think that there are better approaches. Regards.
Jacques Ghazalian, Mar 27, 2012
Great question.. As usual with patterns, there are always more than one pattern that may seem to solve a specific case (definitely each has it own intent).
Prior to applying any patterns, the first thing you can do is abstracting away things that change (which I think you already did some analysis on).
From the GoF... the intent of interpreter is
"Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language."
Take this requirement for e.g.
" I have a catalogue of products that I want to make available to specific clients. "
The above is one of your business requirement or rule which relates to how your catalog of products is constructed and may be while constructing different rules may apply..
This may indicate you need to analyze creation pattern like 'builder"....
definition Separate the construction of a complex object from its representation so that the same construction process can create different representations.
The above is just an example.. Check the intent of the pattern with the problem you are trying to solve and then nail down the closes patterns to see which fits the best...
Since the scenario is more to do with business rules, which may change based on some criteria you may also look at "Strategy" and "Decorator" as well...
But the most important is first apply the SOLID principles which you may already be doing, and then further nail down 2/3 patterns which may best solve your problem at hand...
Also, think through the overall complexity of introducing more patterns than required.
All the very best for your work.
Rajesh Pillai, Mar 30, 2012
Thanks for clarifying the layer vs pattern aspect. The many layers as shown in the original post helps low coupling and high cohesion. That helps to unit test each layer. The effect of change requests to one layer has direct or indirect impact to other layers. So, I think you have to make sure all unit tests have passed instead of just a single unit test, and correspondingly a regression test is necessary of the whole software.
May 16, 2011