What’s a good OOP way?
Now I have every object containing a reference to its container as well as a copy of its key or index to identify itself in that container. the DeleteMe() method calls the container’s Delete(key) method. I think this is bad because of the tight coupling, cyclic references and the duplicate copy of the key/index.
The reason I want to do this is because each object is represented in the UI and has a context menu generated by itself, with options like “Delete” and “Rename”.
I heard that delegates or events can be used here, but I don’t really understand the motivation behind them and if they’re just YAGNI for me.
Ideally Delete, Rename operations should not belong to Objects, these should belong only to container.
Still operations on container should be invoked like
By creating DeleteMe, Rename on the object itself, you are creating object dependent on the container, and then it cannot be used to be stored in other container like List, Dictionary, etc ( or DeleteMe will fail)
Now for the requirement that Delete Handler should delete the object from the container, you can use command pattern. For example
The above code is just outline, you can customize according to the requirements. Other actions (like Rename) can be added similarly.