I am currently studying an example from an online course to design a class diagram for a Library Management System.
However, I am confused with this example:
I can't really understand the purpose of creating two classes: Book and Book Item.
Couldn‘t I just insert attributes from Book Item class into the Book class and keep only a single class of the two?
This is an interesting example:
The Book refers to an book in the catalogue of the publisher or the library. It has an ISBN to uniquely identify it, a title, an author and so on.
The BookItem refers to a copy of that book that is owned by the the library and is lend to the users.
But this example requires improvements:
the model should prefer composition over inheritance., which in UML translates tonprefering a simple association over a specialization. Several BookItems may refer to the same Book, and the model does not capture this reality.
The boundaries between book and book items need to be reviewed. For example the BookFormat (only in item) may in reality influence the number of pages (in book). Moreover a publishing date seems not to be specific to a book item but general to the book. Printing date could change on each item.
Related
I'm currently studying software engineering patterns and one that we've been given to study is the "historical association pattern" It refers to the historical mapping pattern given in Fowler's "Analysis patterns" book. The example we've been given is as follows:
I've tried looking for information online on how this structure would be implemented but I've found nothing regarding historical mapping associations. What would be a code/pseudo-code example of this structure?
Short answer
This pattern consist simply of using some dates for an association, by the means of an association class.
Full explanation
The diamond in the middle is a ternary association between Employee, Date and Money. The dotted line says that the Salary is the association class and it is also associated with a Date.
The association class would be typically be implemented with tuples/composed classes of Employee, Date, Money and the Salary attributes. The way it is done can be very different: in Java you’d directly refer to the associated objects, whereas in a db you’d have a mix of ids and salary attributes. Much simpler than this impressive diagram.
There are simpler models for that!
This diagram is difficult to read (ternary association), difficult to understand (multiplicity in ternary associations are not obvious to grasp), ambiguous (i.e. is Salary associated with two dates, from the ternary association it represents and from the direct association? or is it just a graphical redundancy?).
It would be much simpler to understand if it would be refactored into a binary association, and if value objects such as Dare would be shown as attributes.
After some confusion with mixing use case and class diagrams, managed to clear some doubts and come up with the following class diagram. Felt more comfy with this than use case. Hope am not wrong. Would like some feedback on any errors and improvements on it. Thank you.
Question:
A new library has books, videos, and CDs that it loans to its users. All library material
has a unique identification number and a title. In addition, books have one or more authors,
videos have one producer and one or more actors, while CDs have one or more artists.
The library maintains one or more copies of each library item (book, video or CD). Copies of all
library material can be loaned to users. Reference-only material can only be loaned for a
maximum of two hours and can’t be removed from the library. Other material can be loaned for
up to two weeks. For every loan, the library records the user, the loan date and time, and the
return date and time. For users, the library maintains their name, address and phone number.
Draw a class diagram for the description above.
Class Diagram: link to diagram
Don't use notes for multiplicities 1..*. Edit associations or their ends instead and write it there.
Write multiplicities on ends, not in the middle of the line.
Arrows should be subscribed by names of attributes.
Libraries can have many Videos, books, CD's and so on. So, there multiplicities should be in both ends.
No arrows on the line is the same as arrows on both ends. Check it on the right sides.
Write attributes inside blocks when they are of types not present on the diagram. If they are not, put their names on the opposite ends of the associations, near arrows.
You should use some empty diamonds on the left side.
You should decide the multiplicity of the producer.
Divide Title from Copy. And maybe, from Edition.
Don't mix plural and singular - hold to some system. I use plurals only for collections, but you needn't take it, of course. Plurals in class names are senseless - all classes apart from singletones have many instances.
You don't need toconnect library to CD and Video - they are merely subsets of Books. And books already are connected. The same with three lists in the Library block.
Loan should be connected to Book.
According to standards, loan() is a constructor of the class Loan. It can't be in another class.
From the UML bible, about role:
Role: A role name explains how an object participates in the relationship.
Each object needs to hold a reference to the associated object or objects. The reference is held in an attribute value within the object. When there is only one association then there is only one attribute holding a reference.
What does this sentence mean?
Can anyone please offer an example to explain it?
Roles:A role name explains how an object participates in the relationship.
You have two classes, Professor and Book and they are associated as in the following diagram:
The role gives a description of the association between Professor and Book. In this case Professor is the writer of the associated book.
Each object needs to hold a reference to the associated object or objects. The reference is held in an attribute value within the object.
For this I will use another example with a one to one multiplicity.
The diagram shows that Query builder has a Query (and vice versa). How is this association depicted in the code?
You have a class QueryBuilder that has an attribute of type Query named query. In code:
class QueryBuilder {
Query query;
}
And you have a class Query that has an attribute of type QueryBuilder named qbuilder
In code:
class Query {
QueryBuilder qbuilder;
}
The attribute (query for class QueryBuilder and qbuilder for class Query) is the reference to the associated object
When there is only one association then there is only one attribute holding a reference
In the previous example, there was one association, so we had one attribute (field) in the class to keep the reference of the associated object.
In the following diagram Acount has two associations with BookItem.
So, in class Account we will have two fields, one field for each association.
class Account {
BookItem[] borrowed;
BookItem[] reserved;
}
Note that these associations are one to many, so the fields that we have for the associations are arrays that can keep more than one BookItems.
Here you can find a good article where I borrowed most examples for this answer.
EDIT: Explanation of the association between Author and Book.
The line that connects Author and Book in the diagram is the visualization of the association. This is a bidirectional association, which means that Author one or more Book objects (the books written by the author) but also Book has one or more Author objects (because book can have more than one author). An association with multiplicity many (one or more) is usually implemented with a collection or an array. Class Author has a field that can be a collection or array of Book. The diagram does not provide the name of this fields.
The first diagram that associates Professor with Book provides also the names of these fields. Professor has a field with the name textbook to keep its Book objects. Book has a field with the name author to keep its Author objects. The type of these fields is not provided by the diagram. The field textbook could be declared as anything from the following:
Book[] textbook;
or
Set<Book> textbook;
or
List<Book> textbook;
or
Collection<Book> textbook;
Also the visibility of the fields is not provided (could be default, private or public).
There is a very good reason why this information is omitted from the class diagram: The author of the diagram did not consider it important for the message he wants to communicate with the diagram. We must not forget that UML diagrams are used to help understanding of a system by visualizing some of its aspects. Usually we create more than one diagrams in order to provide different perspectives of a system. In most cases the most important element of information is the relationship between the classes. So the implementation details are often omitted. Note there there are a lot of implementation details regarding the Book-Author association that are omitted from these diagrams. How to force that Book will always have at least one Author or how to ensure that if a Book has an Author then also the Author has this Book are among the details that are omitted.
As a programmer of UML to Code generators, I do not recommend association names as "implicite" role-names and therefore properties dependent to the additional reading direction (in this way "Professor--Book" is the better sample than "Account--BookItem" above). For bi-directional properties the association-name would help only in one direction (and is often easier explained in prosa way by Business Analysts).
DO always set the concrete role-name at each relevant Association-End to make it clear in what way the other end sees the relationship (and can manifest 1:1 in code)!
However the association-name may become the AssociationClass name of an n:n relationship where the reading direction would be in both ways the same. Like:
Bank[0..]--Contract(AssociationClass)--[0..]Person
1.Which of the following types of written design documents do we normally use on DDD projects:
a. Requirements specifications document
b. Document explaining the the meaning of core elements
c. Document giving the bird's eye view of an application structure
d. Document explaining the meaning behind the terms used by Ubiquitous language
e. Document listing the vocabulary of Ubiquitous language
f. Informal UML diagrams
anything else?
2.Which document types should be created as standalone documents and which should be combined within a single document ( example: document containing diagrams surrounded by text )?
3.And what are Requirements specifications? A list of use cases, a list of tasks program is able to perform or combination of both?
thanks
Consider the following:
A statement of the purpose of your application in 25 words or less
A representation of your model in both code and uml
A list of features corresponding to the current or desired model
A list of constraints (business rules) on the model
Where applicable, a sequence diagram for each feature
A statement of non-functional requirements
An architectural overview for team members (including model boundaries and contexts)
Team instructions and procedures
Note: use cases or user stories can inform your list of features. However, I recommend that a feature be the unit of work.
I recommend that the initial model be created (discovered) in a modeling workshop attended by both domain experts (business) and developers. It must be led by someone proficient in domain modeling.
Business rules are constraints on the model of two types: Property and Collaboration. By way of example, business rules prevent an elevator from moving with the doors open, a perishable item being placed in a non-refrigerated bin, or a cancelled purchase being shipped.
I think Event Storming might be a good solution. A photo of the workshop should be enough. If not you can use the same artifacts into a digital document.
I find these two examples conceptually identical - yet one is a composition and the other aggregation.
In the first example, the relationship 'class (has-a) students' is a compositon.
A class contains students. A student cannot exist without a class.
There exists composition between class and students.
In the second example, the relationship 'department (has-a) professors' is an aggregation.
If the university closes, the departments will no longer exist, but
the professors in those departments will continue to exist
In my opinion the first one is plain wrong. Notice that in the comment section of this SO question #TallPaul is questioning the first example as well. I think in practice it would delete all students enrolled in a class after each semester from the system. Moreover, the students would probably have to be created by the class on its initialisation, because composition in C++ is usually implemented as private attribute (not pointer). Am I right? Is there any way the first example makes sense?
There is no absolute truth and it all depends on the system you are modeling. You can create a system where students are instances that exist only in a specific class and when the class is deleted, so are the students. This may make sense when you don't want to store student information between classes for example.
Yes, those are weak examples, a Class must have a Subject would be a much better example for Composition. The relationship between a Class and Students is Aggregation because the life-time of the two is different.
See [UML Associations in Java] for more detailed examples1