tags:

views:

36

answers:

4

I am implement a pipeline pattern, the resource is passed on to a serial of objects that will process the resource, when one object is done using the resource it passes it on to the successor, so when and which object to release the resource?

+1  A: 

You mean with something like chain of responsability pattern (or one variant)? If yes, I guess the last processing object could release the resource:

    public void process( Resource rsrc )
    {
        writeMessage( msg );

        if ( next != null )
        {
            next.process( rsrc );
        }
        else
        {
            // release resource
        }
    }

But your question wasn't very clear to me, so I'm not sure I answered your question exactly.

EDIT

I assumed you had some rather complex scenario and your pipeline didn't return in a trivial way (e.g if it's asynchronous). But as suggested in the other answers, if you can create the resource, fire the chain of responsibility and then release the resource, it's of course the easiest way:

Resource rsrc = new Resource();
chain.process( rsrc );
rsrc.release();
ewernli
good point. and thanks. i am thinking if a certain node will process asynchronously, it need to make a copy of the resource. and in my scenario, it is more complicated, the resource will be wrapped in other resource.
Benny
+1  A: 

The caller who passes the resource to the pipe. It allocates the resource and starts processing, so it should clean it up.

If the control is never returned to the caller, you could use a terminator at the end of the pipe (but this terminator should not be responsible for anything else).

Making one the pipe objects responsible sounds like an antipattern, because it means tighter coupling and is harder to maintain.

Konrad Garus
+1  A: 

It sounds as though you're implementing what I know as the Chain of Responsibility pattern or something very similar.

In which case, I'd suggest releasing the resource from wherever you initiate the work of the chain, rather than delegating that responsibility to one of the processors within the chain.

sgreeve
+1  A: 

I'd suggest you have a look at the "Unit of Work" pattern.

In other words, make the data, objects and the resource itself part of a single object that your pipeline passes along. Make the unit of work expose a release method which will take care of rollback/release/commit all pending work (possibly distinguishing if it was due to an Abend).

This release method will be called by your last step in the pipeline (if everything goes well) or by the catch/finally part of your intermediate steps if something breaks.

p.marino
The downside to this approach is that you have to be very sure that the last processor in your chain calls release; and also that nothing else in the chain calls release. That couples the last processor in the chain fairly tightly to the unit of work being processed, and means that that processor always has to be at the end of the chain - you could never set up another chain with that processor at a different location.
sgreeve
Well, yes. But I honestly don't see the point: resources don't automagically release themselves, and if you forcefully release them before the end of your process you have a problem.Packaging the resource(s) in a Unit of Work mitigates this though, because the UoW methods can check if the resource has been released already or not, and throw exceptions in case you are attempting to release it again, or are invoking methods after the resource has been released.
p.marino