Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to design the architecture of a system that I will be implementing in C++, and I was wondering if people could think of a good approach, or critique the approach that I have designed so far.

First of all, the general problem is an image processing pipeline. It contains several stages, and the goal is to design a highly modular solution, so that any of the stages can be easily swapped out and replaced with a piece of custom code (so that the user can have a speed increase if s/he knows that a certain stage is constrained in a certain way in his or her problem).

The current thinking is something like this:

struct output; /*Contains the output values from the pipeline.*/

class input_routines{
    public:
    virtual foo stage1(...){...}
    virtual bar stage2(...){...}
    virtual qux stage3(...){...}
    ...
}

output pipeline(input_routines stages);

This would allow people to subclass input_routines and override whichever stage they wanted. That said, I've worked in systems like this before, and I find the subclassing and the default stuff tends to get messy, and can be difficult to use, so I'm not giddy about writing one myself. I was also thinking about a more STLish approach, where the different stages (there are 6 or 7) would be defaulted template parameters.

Can anyone offer a critique of the pattern above, thoughts on the template approach, or any other architecture that comes to mind?

share|improve this question
    
Are you sure you do not want to use a Lisp-family language for this type of project? –  Job Feb 16 '11 at 17:39
    
Oh, I most certainly want to use a Lisp-family language for this project, tragically, I'm adding functionality to an existing C++ imaging library. –  anjruu Feb 16 '11 at 17:49

4 Answers 4

up vote 2 down vote accepted

The design is highly dependant on what the different stages actually do. I mostly prefer pure virtual functions over non-pure virtual functions (abstract classes).

Common stages can be grouped together in abstract subclasses. By deriving from the main abstract class you still can still adjust every stage, but by deriving from a subclass you can reuse existing behavior which is already written. It tends to be less messy, as you mention for virtual methods.

If the different stages can also exist on their own (outside of the entire pipeline), also consider writing classes to seperate this behavior.

share|improve this answer

Take a look at Monads as implemented in Haskell, that may give you a good idea on how to set things up. Haskell gets this type of thing really right.

share|improve this answer

Maybe create a list of functors in a factory, which implement the stages. Pseudo-code:

functorFactory() {
  return [ foo(), bar(), baz() ]
}

Users could reimplement the factory or just manipulate the list of functors. Pseudo-code

myFactory() {
  return [ foo(), myBar() ]
}

or

myFactory() {
  return functorFactory()[2] = myBar()
}

When setup is done, you can call each functor using the result of the last.

share|improve this answer
    
+1: this gives maximum flexibility, as the user is allowed to actually add steps / remove steps if they so wish. –  Matthieu M. Feb 16 '11 at 18:02
    
That looks great, but how would I implement this in C++? I couldn't have an array of function pointers, since each function pointer would have a different type if the functions returned different things, and arrays can only hold objects of the same type. Thanks! –  anjruu Feb 16 '11 at 23:26

I would define a Intermediate type. All images would be converted into this format; each stage would take an Intermediate and return an Intermediate - not the same one, a new one.

A stage would be this:

Intermediate DoSomething(const Intermediate &i);

I usually avoid inheritance-based solutions in general unless they are a fairly obvious map onto the problem, e.g., objects in a 3D world.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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