0

As far as i know, we do not add an attribute like id to the classes in the class diagram. right?

Example UML class diagram

But when we create a database, or when we want to call an instance, we need its id, so why do not we have this attribute in our diagrams?

4
  • Do your objects actually have an id attribute, and is it relevant in the operations of the layer you're documenting, or is that just an implementation detail of the database persistence? You'll presumably also have a database schema, which will include all tables and fields.
    – jonrsharpe
    Nov 15, 2015 at 8:58
  • by ID, i mean a unique attribute for my instance.your question is not clear for me.
    – Jimmy
    Nov 15, 2015 at 9:01
  • Oh, for... yes, I understand what an ID is! My question is: is that attribute actually relevant to what your class diagram shows, or is it e.g. only relevant to the back-end storage layer?
    – jonrsharpe
    Nov 15, 2015 at 9:04
  • Ok, it is only relevant to the back-end storage laye
    – Jimmy
    Nov 15, 2015 at 9:07

2 Answers 2

3

You don't have the ID in your class diagram because it is not actually relevant to your class itself. It will be relevant for your database so it will be present in a database diagram. Having a different storage strategy (for example, an XML file per order) means that you will not have a primary key because a primary key is an artefact of your persistance mechanism.

2
  • Ok, maybe you are right about DB, but what about the implementation? sometimes when i am programming i need a unique id to call a certain instance in a set of objects
    – Jimmy
    Nov 15, 2015 at 10:28
  • 4
    If it has significance for your business (a customer id might be significant because it is printed on invoices), it should be present on your class diagram. If not it is probably just an 'implementation detail' of your persistence strategy and should not be present.
    – JDT
    Nov 15, 2015 at 10:39
1

The meaning of a class diagram depends on the level of abstraction at which you are modeling.

For example, you could create a class diagram of the real world. In that case, class 'Shark' obviously doesn't have an 'id', because sharks don't have ids in nature. It would maybe have attribute 'length' and operation 'eatFish'.

But maybe you are creating a class diagram to define the schema of a relational database containing a Shark table, in which each shark has a unique id. In that case, you must show 'id' as an attribute of class 'Shark'. Maybe you want to model its stored procedures as operations of the class.

In between these two extremes, you may want to model at an intermediate level of abstraction. For example, you could create a conceptual data model for a shark game, i.e. a data model that defines how the user perceives the data of the application. In that case, if the application exposes the ids to the user, then you must model this as an attribute of class Shark. But if the application uses ids only internally, then you don't.

One final example: you may want to model your source code, written in an object-oriented language. In that case, if 'id' is a member of class 'Shark' in the source code, then 'id' is an attribute in your diagram and otherwise it isn't .

To summarize, if you create a class diagram, you must define at which level of abstraction you are modeling and what you mean by the concept of a 'class'. This will give you guidance on which attributes it should have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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