my requirement is i want to delete a Object A

A-> B-> C-

here if you want to delete A you have to delete B which is dependent on B , then If you Want to Delete B you Have to Delete C which is dependent on B and The Chain goes like this

i'm planning to Solve it using chain of responsibility design pattern , or is there any design patterns or principles that fit this scenario

  • 1
    If A contains a reference to B (and B to C), then the garbage collector will take care of deleting B and C after A has been deleted (or rather, when the last reference to A is gone). – Bart van Ingen Schenau Jan 7 at 7:58
  • Somehow this reminds me of the Disposablepattern used in the .Net world for releasing non-managed resources when an object gets out of scope. – Bernhard Hiller Jan 7 at 8:29
  • Is this what you really need? Use a finaliser to remove unmanaged resources. The finaliser should be nothing more glorious than a call to close() or similar. If the client code must cleanup the object, call that close() method, otherwise let the garbage collector do its job. – Kain0_0 Jan 8 at 8:07
  • @BartvanIngenSchenau not like that a can deleted only if b was deleted because of the dependency constraint a and b are independent objects – user3878073 Jan 9 at 1:55
  • @user3878073: You have lost me. Can you edit your question to use meaningful names and describe in what way the objects depend on each other? – Bart van Ingen Schenau Jan 9 at 7:53

Chain of responsibility would involve linking class A instance with all of its children, which would be straightforward to execute a deletion, but difficult to be setup. The only problem that this would solve is the child knowing about the parent.

Given the hierarchical nature of the problem, I would suggest you go with the Visitor pattern. In this way the deletion can be handled in an class dedicated to the task leaving your classes virtually untouched. Likewise you could apply this to other operations as well, including insertion and updating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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