erwinleonardy
erwinleonardy

Reputation: 486

Does a class diagram always need attributes?

So, I do understand that a class can have attribute(s) and method(s).

1) Would it be possible if a class has a method but no attribute? (this seems impossible, it seems to me that it defeats the purpose of creating the class)

2) Would it be possible if a class with attribute(s) but no method? (this seems possible)

Thanks a lot!

Upvotes: 3

Views: 10558

Answers (3)

Peter Uhnak
Peter Uhnak

Reputation: 10217

1) Yes, you could sometimes see it in utility classes

class MyUtils {
  public int add(int a, int b) {
    return a + b;
  }
}

How useful it is is another question entirely, however there's no problem in modeling it.

Another case are interfaces (sometimes represented as abstract classes), where there are no attributes (or more generally a state) allowed, only methods or method prototypes/headers.

2) Yes, this is common in anemic models, where some classes act only as data holders; as it is not a good practice to show accessor methods in the diagram, you could potentially see only attributes, and not methods; or if the attributes are public, and not needing accessors, like C++ structs.

Likewise in design models you rarely see any methods, as you are focusing on the relationship between classes, and not on their implementation behavior.

Upvotes: 5

Gangnus
Gangnus

Reputation: 24464

Classes with methods, but without fields, are often used as utility classes. For example, you have several functions that do some editations on text files. Make a class EditationsUtilities, that will have only editational methods. Some people consider that practice evil, but it is widely used. Utility classes are evil? Utility classes are evil?

Classes with fields and without methods - they were called structures. And still are - in C. For example, you have a class Point, that has only two fields: x and y. Nowadays such classes are considered as bad style, but still widely used. They say it is better if x and y will be declared as private and getters and setters methods will be made for them. What is interesting, there are people that consider getters/setters evil. http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html

In some languages there are difference between fields, attributes and properties, and some must have getters/setters. In these cases the class cannot have attributes without methods, for attribute(not field!) must have getter and/or setter.

In UML(class diagrams), when you start to think on classes, you can draw a diagram where you have only empty classes rectangles. So, visibly they won't have fields or methods. But that is temporary only. In code you won't have classes without both fields and methods.

So, use it as it is convenient to you. You can't feel yet, what way of organization is better for your SW.

Upvotes: 0

Ister
Ister

Reputation: 6318

Both class without attribute (with methods) and class without operations (with attributes) are possible and legal, however rare. Class hermetization means you should not access class attributes directly. Yet you might have one class for storing data (attributes only) and another one for managing that data (operations only). Note also that your model might not show attributes or operations (or even both) even though they exist in a class.

Upvotes: 4

Related Questions