This is a language agnostic question about object oriented design, particularly the passing of messages regarding object destruction. This whole question could just as easily use the terms Parent and Child instead of Factory and Item.
Which object should be responsible for an objects tear-down process; the object itself, or the factory that created it?
Imagine a situation where you have a factory that has events: 'item_created', 'item_deleted', keeps a collection of created items, and it's created items themselves have events 'changed', 'deleted'. Things need to happen in both objects; resources the item is using need to be freed, and the item needs to be removed from the list of items within the factory.
When it comes to destroying an item, who should be responsible for that process?
Part 2
Also, in my situation I have a sibling factory (call it UI_Factory
) that watches the item factory and creates sibling items for each created item (Call them UI_Items
) as well. For object destruction (originating from the UI_Item) Which way should the messages flow?
UI_Item -> Item -> Factory -> UI_Factory ?
OR
UI_Item -> Item -> Factory
UI_Item -> UI_Factory ?