Skip to main content
added 508 characters in body
Source Link
Doc Brown
  • 220.6k
  • 35
  • 410
  • 625

There are surely different approaches for different cases, but one approach which I did implement in the past by myself looks like this:

Use a table, but generate its content at run time by processing all rules one-by-one in advance, do not maintain the table manually.

So you can separate the rule processing and initialization from the actual lookup operation, which should simplify things a bit.

Lets take your support phone number example. Start by filling out the table with the most common cases - maybe one default phone number for each country, or maybe 3 (one for each payment level). Now, you apply the rules which define exceptions from the default initialization one-by-one. Finally, you can pass the table to a lookup function which can make use of it whenever it is required.

What you have to maintain are the rules, either in code, maybe in a parametrized fashion, or as some Domain Specific Language, so the users can edit them by themselves. Of course, you have to make sure the rules are applied in the correct order (which is also the case when using different approaches). Here, you usually have to start with the most "general" rules and then go gradually deeper into more specific ones. 

The resulting table should still be so small it fits into the amount of memory you are able to afford for this process. In cases the table does have only a small percentage of filled entries, you may decide to use some data structure optimized for sparse matrixes for it.

I would not call this strategy "a pattern", just an approach with lots of degrees of freedom. When you think it is applicable to your case, feel free to leave a comment how it worked for your.

There are surely different approaches for different cases, but one approach which I did implement in the past by myself looks like this:

Use a table, but generate its content at run time by processing all rules one-by-one in advance, do not maintain the table manually.

So you can separate the rule processing and initialization from the actual lookup operation, which should simplify things a bit.

Lets take your support phone number example. Start by filling out the table with the most common cases - maybe one default phone number for each country, or maybe 3 (one for each payment level). Now, you apply the rules which define exceptions from the default initialization one-by-one. Finally, you can pass the table to a lookup function which can make use of it whenever it is required.

Of course, you have to make sure the rules are applied in the correct order (which is also the case when using different approaches). The resulting table should still be so small it fits into the amount of memory you are able to afford for this process. In cases the table does have only a small percentage of filled entries, you may decide to use some data structure optimized for sparse matrixes for it.

There are surely different approaches for different cases, but one approach which I did implement in the past by myself looks like this:

Use a table, but generate its content at run time by processing all rules one-by-one in advance, do not maintain the table manually.

So you can separate the rule processing and initialization from the actual lookup operation, which should simplify things a bit.

Lets take your support phone number example. Start by filling out the table with the most common cases - maybe one default phone number for each country, or maybe 3 (one for each payment level). Now, you apply the rules which define exceptions from the default initialization one-by-one. Finally, you can pass the table to a lookup function which can make use of it whenever it is required.

What you have to maintain are the rules, either in code, maybe in a parametrized fashion, or as some Domain Specific Language, so the users can edit them by themselves. Of course, you have to make sure the rules are applied in the correct order (which is also the case when using different approaches). Here, you usually have to start with the most "general" rules and then go gradually deeper into more specific ones. 

The resulting table should still be so small it fits into the amount of memory you are able to afford for this process. In cases the table does have only a small percentage of filled entries, you may decide to use some data structure optimized for sparse matrixes for it.

I would not call this strategy "a pattern", just an approach with lots of degrees of freedom. When you think it is applicable to your case, feel free to leave a comment how it worked for your.

Source Link
Doc Brown
  • 220.6k
  • 35
  • 410
  • 625

There are surely different approaches for different cases, but one approach which I did implement in the past by myself looks like this:

Use a table, but generate its content at run time by processing all rules one-by-one in advance, do not maintain the table manually.

So you can separate the rule processing and initialization from the actual lookup operation, which should simplify things a bit.

Lets take your support phone number example. Start by filling out the table with the most common cases - maybe one default phone number for each country, or maybe 3 (one for each payment level). Now, you apply the rules which define exceptions from the default initialization one-by-one. Finally, you can pass the table to a lookup function which can make use of it whenever it is required.

Of course, you have to make sure the rules are applied in the correct order (which is also the case when using different approaches). The resulting table should still be so small it fits into the amount of memory you are able to afford for this process. In cases the table does have only a small percentage of filled entries, you may decide to use some data structure optimized for sparse matrixes for it.