Object Oriented Design – A Class Diagram Walkthrough

Making a Class Diagram with UML Notation – Explained Step by Step:

 Note: Your feedback , issues are warmly welcomed and I would be happy to assist you with any problem you might be having .Class diagrams are often used in object oriented analysis and design to show the various identified objects , their types (classes) , their data and operations and how the relate , or communicate with other objects or classes. Here are some of the standard UML conventions and a sample class diagram to get you started: 

Identify the classes in the problem domain:

 This is the first and most important step for creating the class diagram. What classes should you include and how to identify them given a problem statement or a scenario? The solution is simple and some time should be spent with the client / system engineers /your teacher in case you are making an academic project or any concerned authority having the required business knowledge.The first step however is creating a problem statement or establishing a set of requirements that the software should fulfill. E.g. you need to create a small library management system. What are some of the requirements for the system? Let’s try to make a problem statement or the scenario for your design. For the sake of simplicity considers the following simple problem Statement. “The library management system would be used by the librarian to keep track of books, library members and the borrowing activity. Not all books can be borrowed by the members. Some books may only be available to review in the library; all such books are termed as Reference books. There are 2 types of library members students and college faculty members. Faculty members can also check out research papers and magazines where as students can only checkout books. The system need to send alerts whenever a book that has to be returned within a specific date is not returned. An email is sent to the librarian and the borrower. The system maintains a catalogue having a description of each book that is available in the library” 

Ways to identify Classes:

 Do the Noun Analysis: Go through the problem statement again and again and try to figure out all the nouns that you come across. In our case some strong contenders for the classes of the library management system would be: Librarian,Book,Member,Catalogue,Student_Member,Faculty_Member,Reference_Book,Issueable_Book,Alert 

Perhaps there might be some nouns that I skipped but why??  I did that intentionally because of either of the two reasons. Either there is no data associated with them. They have no role to play in the system i.e. they don’t have any functions or actions associated with them. So by this we mean that “In order for a noun to be an object or a class it should have some attributes or member data and some actions or member functions.

 Resolving the relationships between classes:

Object oriented analysis and design is all about relationships between objects. Look for a certain type of relationships. These include generalization/inheritance/specialization, composition / containership, aggregation/ collections and associations. All of these are explained briefly below and discussed in terms of our scenario. There are some key questions associated with object oriented design over here.  How data is passed between the classes? What classes should have the objects of another class?What classes create the objects of other classes?What are some of the utility classes that the application requires? All these questions play an important role in the design. Think in terms of hierarchy for generalizations/inheritence:First of all check for hierarchy that may exist between the classes or the objects.This would model the inheritance or generaliztion or the superclass/subclass relationship.In natural language this might resolve to “type of” or “can be”. If you find such words in a problem statement there might be a case of generalization. E.g. In our scenario take the three classes BOOK , REFERENCE_BOOK and ISSUEABLE_BOOK . So a book can be a reference book or an issue able book or you can say that reference books and issue able books are the type of books. They all should ideally share many characteristics which are common to all the three classes.What I mean to say is, whether it is any book (reference or issue able) they all have ISBN number, author a language, a topic e.t.c so what is the difference. The difference is implied by the business rule that reference books cannot be issued but only reviewed. So for reference books we might add a Boolean data member called m_bReference and set it to true or something. Same is the case with Member, Student_Member and Faculty_Member. Inheritance is indicated in the class diagram with a filled black triangle pointing towards the base class and connecters connecting it to all the derived classes. In our scenario the inheritance could be shown as the figure below:

 Think of the Part-Whole Relationships:

Ok so now you know what might be the possible hierarchies in your system and what might be the common data members and the common functions. What are the differences and how the qualify to be another class. Another important type of relationship in terms of Object Oriented Analysis and Design is the “Part of” or “Part Whole” relationship. In Natural language, you might come across “has a” words to quickly identify these kind of relationships. In the list of candidate classes observe which of the objects the part of other objects is. This can also be taken as collection.To give you some examples for common scenarios. A Windows form is a collection of many controls.An automobile is composed of many parts.A shopping cart has many itemsA university is comprised of many campuses Here you want to ask a simple question to resolve the type of relationship and to know whether it is an aggregation or a composition. “Is the whole destroyed when the part is destroyed” or “It doesn’t make a difference to the Whole if the part is destroyed”. Now going through this example list. A form would still exist if any of the control is destroyed; the form serves it purpose properly. This implies an aggregation .An automobile wont function properly if any of its parts are destroyed e.g. it cannot function if the tires, steering, battery is taken out so this implies composition. A university will still exist if a campus is destroyed. So it depends upon the scenario and the business rules.

Returning to our depicted scenario the only part whole relationship noticeable is perhaps the catalogue. A catalogue has list of books available in the system with a short description of each. The catalogue is not destroyed if a book is destroyed so I would use aggregation in the design. In UML aggregation is shown with an empty diamond where as composition is shown with a solid black diamond.

 The Uses relationship – Association:

 Many objects use the methods/functions of other classes for utility. The objects are somehow related might be statically or dynamically but there is not a hierarchy or a part whole relationship. Such relationships are called association and can be mostly identified with “makes use of” or “uses” in the problem statement. E.g. There is a class called “Pen” which exposes a method called “Draw ()”. There is also a “Shape” class having a method called“drawShape ( )” . In the implementation of “drawShape( )” it internally calls / invokes the “Draw ()” method exposed by Pen. In our scenario  A librarian uses the book and student information to issue a book.The catalogue needs a reference of the book to add or update itselfAnd probably some more: 

Here is the sample design which I created with Visio just for the sake of discussion and show how relationships can be expressed in UML in terms of object oriented design.The business rules that I assumed for this discussion were really simple just to make the understanding better.If it helped you in any way or you require some discussion in any area do supply your feedback by leaving comments for this post. Thanks.

Sample Class Diagram

       

About these ads

49 Comments

Filed under Software Design

49 responses to “Object Oriented Design – A Class Diagram Walkthrough

  1. Dreamer

    Hi, I would like to thank you for this enlighting article !!! It’s fantastic, thank you a lot, you really helped me out…

  2. vaishali

    its really vry gud. thanx a lot sir it help me to solve many problems.

  3. asadsiddiqi

    Thank you : )

  4. sid

    Hello,

    Very useful article. Thanks for the post.

    Have a question about the class operation: Suppose I’ve two classes, User and Blog.

    Users posts a Blog. Here the operation ‘postBlog()’, should it belong to the User class OR to the Blog class?
    Is there a rule to decide?

    Thanks again for all the help.

    -Sid

  5. asadsiddiqi

    Good Question !! primarily there are a few bare essentials the so called rules for OO Design and many time it depends on your implementation or how you decide to implement it. I would define a method in blog class called “CreatePost(string postContent)” and a method in User called “PostToBlog(Blog b)” and call the PostToBlog() Method and pass in the Blog object which you want to Post.
    There is no right or wrong in your scenario , matter of clarity and choice !! Best of luck and feel free to discuss !! Thanks

  6. bgreddy

    the information is very useful. thank you

  7. lakshmikanth

    very useful content for beginners

  8. nodeen

    hi there..

    Very good article indeed. thanks a bunch :).
    i got a bit confuse between class diagram and ERD. can u tell me what the difference between those things?is it possible if some object mentioned in a class diagram not to be part in ERD?or all object in a class diagram is subjected to be a part of ERD?
    thanks.

  9. kiran

    thanks sir,will you please send me the package diagram for the library management system ?
    my email-id
    sachine.sachine@yahoo.com

  10. hello muhhammad sir good morning , i am a mca student i want to know details about a class diagram for library management system .it is a part of our study and it is also helpful of our assignment so i request you to please send details about this diagram .thank you for anticipation
    your’s faithful
    prasanta

  11. hello muhhammad sir good morning , i am a mca student i want to know details about a class diagram for library management system .it is a part of our study and it is also helpful of our assignment so i request you to please send details about this diagram .thank you for anticipation
    your’s faithful
    prasanta
    dt-25.11.08

  12. naveen

    Thank you sir…noun analysis really helepd me :)

  13. Anand

    Thank you sir ………….Thanks a lot because of u i m able to identify classes from problem statement

  14. gaurab maharjan

    thanks for your great assist.i was looking for this daigram.it gave me good idea for my project.will you help me how it goes with the GRASP principles or patterns for OOD.i meant how can it be related with this daigram.
    regards
    gaurab maharjan

  15. ubaid ullah

    HEllo nice article ,well i am a student od computer science and i am doing my semester project “online library management system” .
    i am doing the same thing as u had described above but in my project i had also added a “RESERING BOOK” option.
    and i am confused now that either it is a good approach or bad one. so would you please send me the whole documentation of the system you escribed above..

  16. nripesh kumar nrip

    hello! i m a student of mca 2nd year..
    although the article is best..
    but i m looking for a class diagram for librery management system for my assignment work…

  17. haseeb

    sir, i want class, collabration, dfd,seqence digram and all documentaion of book shop management

    • asadsiddiqi

      Sir, This article was aimed at helping you out on how to design a class diagram and ur application. I just put forward an example. I dont have any intentions what so ever to do your homework. Do some reading about UML and if u figure it out urself that would be good for you.

      Thank you and i hope U realize it sooner than later.

  18. Benjamin

    Great tutorial, I am going into an exam soon and its almost a dead cert that i will have to do a class diagram. The book we have been given and lecture slides are very brief in that they show you the notation but neither give you a good walkthough of how to take a piece of text and methodically convert it to a class diagram

    thank you very much

  19. azar

    Hello!
    Dear brother, that was very useful to me to my assignment. And if you can try to design a component and collaboration diagram for the same system.

  20. vathany

    hi, Great tutorial thank you very much

  21. krunnd

    vaishali :
    its really vry gud. thanx a lot sir it help me to solve many problems.

    hi how r u ?
    u r so smart

  22. Amaka

    Good one really helped me in my course work

  23. Bonface Gauti

    One of the best lecture notes so far. Keep on doing the good work. May the good Lord, Jesus Christ keep on helping you in your day to day assignments.

    Am doing a postgraduate diploma in Computer Science at Chancellor College, under University of Malawi, Zomba, Malawi, Central Africa. Let’s get in touch, be my mentor

    Thanks

  24. “What is backup? Backup is the process of making copies of the files and your documents. Additional copies are made as a precaution, because who knows, tomorrow tomorrow, will it damage your Windows and does not work.se more at http://jumadijalil.blogspot.com/2010/08/how-importance-of-backup.html

  25. sir, i want class, activity, use case,seqence digram and all documentaion of book shop management

  26. Mhuammad Shakir

    Thanks Sir
    Its very good and it will help to solve my many problem.

  27. Muhammad Shakir

    Thanks Sir
    Its very good and it will help to solve my many problem.
    thanks

  28. hi, if you know muhammad , what are the objects ,classes ,methods and attributes for the lbrary system can you briefly explain them to me and thanks .

  29. Kedar

    Hello,

    Can you please suggest any good book which discusses lot of examples about OO design by considering design patterns, objects, properties and their behavior.

    Thanks.

  30. miraj

    awesome ! this helped me out greatly ! keep posting more ..

  31. miraj

    can you explain the connection between book and alert which you have given in the above figure?

  32. i’m really appreciate this note…thankz a lot…:)

  33. this is the note i ever wanted :DD

  34. Din

    Thank you very much for this article. Really Appreciate

  35. thanks a lot its a useful article thanks…………..

  36. Shafa

    vry useful thanks a lot,

  37. thanks mate, GOD bless you for this short but brain lifting and enlightening article, will really help with my coursework.. Cheers..

  38. THELAW

    I have spent hours trying to find an article that was clear in its explination of classes and relationships.

    Well done!

  39. Dev

    Depicted scenario suggests that relationship between catalogue and books is of type “Aggregation” ; but in UML diagram relationship shown as “composition “. Which one is right in current depicted scenario ?

    Quoted from article
    “Returning to our depicted scenario the only part whole relationship noticeable is perhaps the catalogue. A catalogue has list of books available in the system with a short description of each. The catalogue is not destroyed if a book is destroyed so I would use aggregation in the design. “

    • asadsiddiqi

      Good point ! But will the catalogue destroy if all the books are destroyed ? That is the business rule that a designer should think abouot. May be you think it would not be destroyed but I think it would be destroyed.
      You have a valid point and you may use aggregation :)

      -Cheers

      • Dev

        I think , It should be destroyed ; since it is library management system and library can’t be managed without existing of a catalogue. Pl , update the attached image with your article’s depicted scenario ; so that readers can match UML diagram with given explanation .
        Thanks for a nice article .

        regards
        Dev

  40. asadsiddiqi

    Sorry for the late reply !
    I am trying to create a scenario where “Issuing a book will create an alert in the system. The librarian will get an alert when the book is not returned by the due date”. It is just a made up scenario to explain the relationships. I dont know if the real system works like that :)

    • coder

      Hi,

      Very good stuff, it would be really great if you extend this stuff to database design, in real world that would be very useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s