Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating class diagrams

From: Foundations of Programming: Object-Oriented Design

Video: Creating class diagrams

The information that you have now should be enough to let you create your first collection of classes. And the most common way to jot these out is with a UML class diagram, and we've seen these once or twice already. These are the most common diagram in Object-Oriented Design, and while they can get advanced, we're going to focus here on the most common that you'll see and use. So, we want a list of classes, and we'll create a class diagram for each, and for each class we'll have the primary attributes and primary operations. And at this point, because we're getting closer to the code, we will pay more attention to naming.

Creating class diagrams

The information that you have now should be enough to let you create your first collection of classes. And the most common way to jot these out is with a UML class diagram, and we've seen these once or twice already. These are the most common diagram in Object-Oriented Design, and while they can get advanced, we're going to focus here on the most common that you'll see and use. So, we want a list of classes, and we'll create a class diagram for each, and for each class we'll have the primary attributes and primary operations. And at this point, because we're getting closer to the code, we will pay more attention to naming.

So, our classes are named in the singular, not plural, and the standard is for an uppercase first letter, so employee, event, customer, image, product. Now, with Attributes, you won't know all of them yet, because we've been focusing on the behavior, on the operations, but you're expecting to just initially write down the obvious ones, and again, we can add more once we start programming. As I'll talk about in a moment, attributes are usually a lot easier to determine than behaviors. So we'll start to name these using whatever naming format is typical for your language.

I'm going with the common Pascal Case, lowercase first letter, uppercase each following word. And obviously this is an incomplete class diagram, we've got some basic data, a product has a name, it has an isActive flag that can be true or false, it will have a launchDate and say an itemNumber. Now, it's perfectly common just to see the names of the attributes but you can also see them written with a suggested data type after a Colon. So :String for name :Boolean for the isActive flag, :Date, :Integer, but you don't have to go to this level of detail.

Now, if your language names their data types differently, it doesn't really matter. It's all about making this readable and understandable. Say in Objective-C, I might know that I'd need to implement this name attribute as an NSString object, but I don't have a problem reading the fact that it's a string. We can also describe a default value when necessary using an equal sign after the data type. And again, these aren't needed on all of them, just the ones where it's relevant and important. Then we come down to the Operations section where we should have a good idea of what we need to write down here if we've done any work with conceptual object models or CRC cards.

We'll start to name them. I am just going to go with the same case format, lowercase first letter, and uppercase every subsequent word. If we know we need some methods to modify and retrieve attributes, we typically name them as Get and Set operations, rather than change or retrieve. And this is one of the few cases where if you know your particular language automatically generates Getter and Setter methods, this might be a little different for you. Now, as we're going to turn these into code, it's quite common to see parentheses containing any parameters.

And in this case, the only one I've put that actually has a parameter is the Boolean that's passed into setActive. But on the other side, we can also add a return type, and the way we write this in a UML class diagram is to put a Colon after the parentheses and just the return type I am expecting to come back from this operation. These classes are likely to have a lot more functionality internally, but because we've been focusing on the actual responsibilities of the objects, we're really focused first on the public visibility. What are the operations that other objects need to know about? Now, if you remember in our discussion of encapsulation, I talked about trying to hide as much of the implementation as possible and only share what was absolutely necessary to expose.

And when drawing diagrams, it's common to see signs before the attributes or methods, most common is a Plus sign or Minus sign, so I've put several Minus signs in front of the attributes here. This is referred to as controlling visibility, and Minus means these should be private to the class, not directly accessible from other objects. So I can see here that I'm saying my name attribute is private, but I do have a getName operation which will be public and marked with a Plus sign, and that returns a string.

I might say here that in the operations I've drawn out, I know that formatProductDetails is only going to be used internally within the object, so I've made that one private and the others all public. Now, there are other signs, but the Plus and Minus are the two most important. And again, the rule is to leave as much private as possible, and only make it public if you know another object has to use this attribute or method. You can also add notes to a class diagram when necessary if you want to add a bit more information. The usual format looks like this, note has the top-right corner folded down so that we don't think this box represents another class.

It's often very tempting for programmers to actually begin with these kinds of diagrams. You might think you should start here and just begin by charting out your classes as completely as you can, but there's a reason I favor the concentration on responsibilities and doing some of the work with use cases and user stories and conceptual models, and that's if you jump straight to object creation, what often happens is people focus on the data. They decide they need to build a customer object and an order object, but the way they start to be built is that the customer has customerId, customerName, an email, address, a phone, a company, the obvious attributes, same with Order, it will have orderNumber, orderDate.

And if you find yourself defining classes that are strangely devoid of any behavior at all, you might want to revisit those responsibilities, because it's true that these objects may need these attributes, but it's the wrong initial focus, and when you take the responsibility focus, do a little work with requirements, written descriptions, and conceptual models or CRC cards, your focus is on what the objects do, not just viewing them as dumb data structures. So a little later, we're going to see how to show associations between multiple classes.

But let's first see how we might transform some of these diagrams into code.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 51455 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Foundations of Programming: Object-Oriented Design.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.