1

For example, there is a model of an event with certain attributes that can be adjusted by existing processes: maybe one that looks at a geo-location stamp and determines a nearby city; another that looks at the number of registrations and applies rounding; another that calculates local popularity based on attendees vs. nearby events.

The important piece is having a way to dynamically order the operations--the filters/transformations/processing tasks--so that you can pass the events to a processing object that arranges the tasks in the correct order for the configuration/situation, and then passes the models through to apply them.

I can think of some ugly ways to make this happen, but want to know if there is a SOLID pattern that would allow for better, more-testable code.

0

2 Answers 2

4

Pipe & Filter is what you're looking for.

http://msdn.microsoft.com/en-us/library/dn568100.aspx

Basically, you define a standard input object and a standard output object. In object oriented programmation, you can easily define those as interfaces. The filters take the standard input object, apply some transformations and returns a standard output object. Because every filters takes the same input and output, you can chain them together however you like. You can easily produce parallel pipes to take advantage of multithreaded processors.

You could also create a configuration object to define flows of operations; your solution would then be reusable.

3

I have seen similar things done using the Chain of Responsibility pattern. Basically, each transformation/filter is a single command, and your configuration specifies the order in which they execute. The result of each transformation command can be passed to the next along with a command context to contain other metadata that should not be in the data being operated on.

4
  • 1
    +1 - I've also seen a series of chained decorators or composites if you need a tree, though they all boil down to a chain of responsibility.
    – Telastyn
    Jul 22, 2014 at 18:11
  • What about the design of the object that actually turns the configuration into different chains of responsibility? I think that is the crux of the issue. A DI container could have one or even several different instances of a service, but it isn't really feasible to have it manage a huge list of differently ordered/composed chains.
    – Bryan Agee
    Jul 22, 2014 at 20:16
  • @BryanAgee: I've used spring for that. The chain definitions are stored in a spring configuration file that is loaded when the application starts up. Our applications have a finite number of chains defined so it's not a problem that chain definitions are only loaded once. Are you creating new types of chains on the fly? Jul 22, 2014 at 20:20
  • @bryanagee - the best approach I've seen is to just define the things in code for things that are static. Otherwise, some UI to configure the things and make them serializable.
    – Telastyn
    Jul 23, 2014 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.