Chain of Command Example

293

The idea is that a cache is often first because it’s so fast. We can, of course, write a giant master function with other functions..

One objective of the chain of command design pattern is to be able to write a bunch of functions that link together and form a chain of alternative implementations. The idea is to have alternatives that vary in their ability to compute a correct answer. If Algorithm 1 doesn’t work, try Algorithm 2. If that doesn’t work, fall back to Algorithm 3, etc.

Perhaps Algorithm 1 has a number of constraints, i.e., it’s fast, but only for a limited kind of input. Algorithm 2 may have a different set of constraints. And Algorithm 3 involves the “British Museum” algorithm.

Read more at DZone