This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am writing an MVC application for making graphs. My graph elements are designed in the MVC way either. The state of a single node is distributed over view(position, selected,...) and model(label, incoming/outcoming edges). All references between a node and the other nodes, it is connected with, are kept in its model.
The problem I've encountered, is when I try to do some operations on edges connected to some node. For example I want to delete a node... When a node is to be deleted, all edges it is referring to are to be deleted too.
To delete a node from my application code I am using a reference to its controller, from which I can retrieve only edge models. But to delete the edges I must have references to their controllers too!
Should I have methods in the NodeController class returning EdgeControllers? But it is not so easy. NodeController would have to create them based on its model, but it doesn't have enough information to do that.
I was following this until you got to the point where you seem to be describing multiple "controllers". This sounds a strange use of MVC to me. Can you describe the purpose and use of these separate controllers in a bit more detail?
I'm guessing that if I were implementing this I would have one or more UIs collaborating with a single model for the whole graph and a single controller for the whole graph. The single controller also collaborates with the model to make its changes, and the single model is presumably comprised of a collection of sub models representing nodes, edges, faces, or other components of the graph.
To remove a node, the UI tells the controller, which finds the node, gets all its edges and the nodes at the ends of them, tells each end node to drop the edge in question, then drops the newly unconnected node. The UI(s) can then be updated beased on the new model.
Originally posted by Jasiek Motyka: To delete a node from my application code I am using a reference to its controller
A controller should only be responsible for interpreting user gestures. The actual deleting should happen in the model. If some of your business needs to delete a node, it shouldn't use a controller to do so. In fact, it shouldn't even know about the existence of controllers.
from which I can retrieve only edge models. But to delete the edges I must have references to their controllers too!
I think you misunderstand how MVC is supposed to work.
If a user deletes a node, the controller should simply tell the model to delete that one node. It is then the responsibility of the model to find out about the edges that need to be deleted, too - as this is business logic, not presentation logic. That is, what edges will be deleted doesn't depend on how the edges are presented (view) or how the user interacts with them (controller).
Once the model deletes an edge in the model, the model notifies all of its observers that the edge got deleted. One of the observers will be the associated view, which then takes appropriate action to update the UI.
Does that help?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus