how do i avoid a circular relationship in my class diagram

时光怂恿深爱的人放手 提交于 2019-12-10 18:22:04

问题


Hi I have a question about some circular relationships that I am facing with my database design . I read a few more similar questions but couldn't solve my problem, so here is my class diagram :

and here is the logic:

  • A document belongs to a DocumentType( invoice , order form , ..)
  • a documentField ( date , address , nameClient , ... ) belongs to a documentType ( each documentType has its proper fields
  • the FieldValue is the value of documentfield that will be saved in database it belongs to both document and documentField , the value should be saved according to the fieldType ( date , char , long , double... )

However, from a database architect perspective, this circular relation is incorrect since it can results in integrity problems:

Should you have any idea how to deal with this, please be welcomed to comment.

Thank you in advance for your help.


回答1:


Here the situation is even simpler than in your other similar question. It is clear that bottom two classes describe the abstract document structure, while the top two classes describe concrete documents.

Abstract elements should never depend on concrete ones, so just make two vertical associations unidirectional and point them towards abstract classes. This will break the circular dependency neatly.

In addition, I would further refine your model:

  • The association between Document and FieldValue should be a conposition.
  • Some lower multiplicities should be changed to 0 (instead of 1), to make your model instantiation more flexible (for example - why not permit a Document with no DocumentFields? It is clear that you will sooner or later add some fields, but you can probably create an empty Document first and save it)

UPDATE:




回答2:


I can't actually see the aim of your modeling. It seems to me you are mixing meta-elements ( DocumentType and DocumentField) with modeling elements instances of the meta-elements (Document and FieldValue).

What is the semantics of the Document-->DocumentType relationship ? is it an "isA" relationship? if so why don't you use extension/generalization (inheritance) or interface realization (implementation)?

Why do you need the inverse relationship DocumentType-->Document? if You could avoid it you wouldn't have the circular relationship.

[Update] Why don't you rename DocumentType in AbstractDocument and make it an abstract class. Then replace the association from Document to DocumentType with an extension (generalization) Finally create a 1..* association from AbstractDocument to itself

[Update2] Also consider that according to your explanation the association from Document to DocumentType has different sematnics and thus is different (another association) from the association that goes from DocumentType to Document



来源:https://stackoverflow.com/questions/23719371/how-do-i-avoid-a-circular-relationship-in-my-class-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!