Class Notation
In UML class diagrams, classes are represented as rectangles divided into three compartments: top for class name, middle for attributes, and bottom for methods. The class name is bold and centered. If the class is abstract, the name is italicized. The class name should be a noun or noun phrase that clearly describes the entity. Avoid abbreviations and use domain-specific terminology when appropriate.
Attributes list properties with their visibility (+ for public, - for private, # for protected, ~ for package) followed by name, colon, and type. You can also include default values and constraints. For example: -count: int = 0 indicates a private attribute named count of type int with a default value of 0. Static attributes are underlined. Derived attributes (calculated from other attributes) are shown with a '/' prefix. You can also include multiplicity, initial values, and property modifiers like {readonly} or {ordered}.
Methods follow similar notation with parameters and return types. The visibility symbol comes first, followed by method name, parameters in parentheses, colon, and return type. For example: +calculateTotal(price: number, quantity: number): number indicates a public method that takes two parameters and returns a number. Abstract methods are shown in italics. Static methods are underlined. You can also include parameter directions like 'in', 'out', or 'inout' for complex operations.
Interfaces are depicted as classes with <
The notation also supports showing class constraints and notes attached to specific elements. Constraints are shown in curly braces {constraint} and can be placed near the class or attribute they apply to. Notes are rectangles with folded corners that provide additional information. They can be connected to any element with a dashed line. Proper notation ensures clear communication of design structure and helps developers understand the intended implementation.
Relationships
Class diagrams show several relationship types that define how classes interact and depend on each other. Association is a structural relationship between classes, shown with a solid line. It indicates that objects of one class are connected to objects of another class. Associations can have names and roles to clarify the relationship's purpose. For example, a 'Customer places Order' association shows that customers are associated with orders they've placed. The role names indicate how each class participates in the relationship.
Aggregation represents a 'has-a' relationship with weaker ownership, depicted with a hollow diamond on the containing class. In aggregation, the contained object can exist independently of the container. For example, a Department has many Employees, but employees can exist without the department. The lifecycle of the contained object is not managed by the container. Aggregation is often used for conceptual relationships where the whole-part relationship is important but the parts can exist independently.
Composition is stronger aggregation with ownership and lifecycle control, shown with a filled diamond. In composition, the contained object cannot exist without the container. For example, a House contains Rooms, and rooms cannot exist without the house. When the house is destroyed, all its rooms are destroyed too. Composition implies a strong ownership relationship where the container manages the lifecycle of its parts. This is common in UI frameworks where parent components manage child component lifecycles.
Dependency indicates one class uses another temporarily, shown with a dashed line. This is the weakest relationship and represents a client-supplier relationship where one class depends on another for a specific operation. For example, a Report class might depend on a DataProcessor class to format data, but this dependency is temporary and doesn't imply a structural relationship. Dependencies can be created by method parameters, local variables, or static method calls.
Inheritance (generalization) uses a solid line with hollow arrow pointing to parent class. This represents an 'is-a' relationship where a child class inherits attributes and methods from a parent class. For example, SavingsAccount is-a BankAccount. The child class can override or extend parent behavior. Implementation of interfaces uses dashed line with hollow arrow, indicating that a class implements the interface's contract but doesn't inherit implementation. Multiple inheritance is not supported in many languages, but a class can implement multiple interfaces.
Multiplicity
Multiplicity defines how many instances of one class can be associated with instances of another class. It's a crucial constraint that clarifies business rules and implementation requirements. Common values include 1 (exactly one), 0..1 (zero or one), * (many), 1..* (one or many), and 0..* (zero or many). You can also specify exact numbers like 2..5 or ranges like 1..10.
Multiplicity is placed near the class it applies to in the relationship. The placement indicates which class's instances are being counted. For example, in a Customer 1 -- 0..* Order relationship, the 1 is near Customer (each order has exactly one customer) and 0..* is near Order (each customer can have zero or many orders). This clarifies the cardinality constraints from both perspectives.
Proper multiplicity notation prevents ambiguity in design specifications and helps developers understand constraints. For example, if a relationship has 1..* multiplicity, developers know they must implement a collection that can hold at least one item. If it's 0..*, they know the collection can be empty. These constraints directly impact data model design and validation logic.
Role names can be added near multiplicity to describe the purpose of the association from that class's perspective. For example, in a relationship between Professor and Course, the role 'teaches' near Professor and 'taught-by' near Course clarifies the relationship's semantics. Role names improve diagram readability and help stakeholders understand the domain model.
Multiplicity constraints can be more specific, like 2..4 indicating between two and four instances. These constraints must be enforced in the implementation through validation logic. When designing systems, consider the business rules that govern these constraints. For example, an order must have at least one item (1..), but a product can belong to zero or many orders (0..). Understanding multiplicity helps create accurate data models and prevents issues during implementation. It also influences database design, as multiplicity constraints translate to foreign key relationships and validation rules.
Best Practices
Effective class diagrams follow several best practices that improve readability and maintainability. Organize classes logically, placing related classes near each other. Use packages to group classes by functionality or subsystem. This helps stakeholders understand the system's structure at different levels of detail. Consider creating package diagrams to show high-level organization before diving into class details.
Use meaningful names that reflect domain concepts. Avoid technical jargon when domain terms exist. The names should be nouns or noun phrases that clearly describe what the class represents. Be consistent with naming conventions throughout the diagram. Use business language that stakeholders understand, not implementation details.
Show appropriate detail level—not every attribute needs to be included. Focus on public interfaces for external classes and key attributes for internal classes. The diagram should communicate the essential design without overwhelming viewers with implementation details. Include only attributes and methods relevant to the current design discussion.
Keep relationships unidirectional when possible to reduce coupling. Bidirectional relationships create dependencies in both directions, making changes more difficult. When bidirectional relationships are necessary, document the reasons clearly. Favor composition over inheritance to create more flexible designs.
Avoid diagram clutter by creating separate diagrams for different subsystems or use cases. A single diagram trying to show everything becomes unreadable. Instead, create high-level architecture diagrams and detailed design diagrams for specific components. Use zoom levels to show different levels of detail.
Document assumptions and constraints in notes attached to relevant elements. This provides context that might not be obvious from the diagram alone. Include business rules, performance requirements, or other considerations that influence the design. Review diagrams with stakeholders to ensure they capture requirements accurately. Developers should validate that the design is implementable and meets technical constraints. Product owners should confirm that the design supports business requirements. This collaborative review process catches issues early and ensures alignment across teams.
Practice Problems
Design a scalable Class Diagrams system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Class Diagrams to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Class Diagrams and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What does a filled diamond represent in a class diagram?
2. What multiplicity means 'zero or many'?
3. How is inheritance shown in a class diagram?
4. What is the primary purpose of Class Diagrams?
Flashcards
Question
What are the three compartments of a class in UML?
Click to reveal answer
Answer
Class name, attributes, and methods.
Question
What's the difference between aggregation and composition?
Click to reveal answer
Answer
Aggregation is weaker ownership with shared lifecycle; composition is strong ownership with controlling lifecycle.
Question
How do you show an interface in a class diagram?
Click to reveal answer
Answer
Use <<interface>> stereotype above the interface name.
Question
What is Class Diagrams?
Click to reveal answer
Answer
Class Diagrams is a key concept in system design.
Question
When to use Class Diagrams?
Click to reveal answer
Answer
Use Class Diagrams when building production systems that require reliability, scalability, and maintainability.
Revision Notes
Key Takeaways
- 1.Class diagrams show static structure of a system
- 2.Relationship types convey different semantics about class interactions
- 3.Multiplicity clarifies cardinality constraints
- 4.Best practices improve diagram readability and maintenance
- 5.Focus on essential details to avoid clutter
Interview Tips
- •Practice drawing class diagrams for common design patterns
- •Explain relationship differences with concrete examples
- •Discuss how to translate requirements into class diagrams
- •Mention tools like Lucidchart, Draw.io, or PlantUML
Cheat Sheet
Class diagrams: three compartments (name, attributes, methods); visibility (+, -, #, ~); relationships: association, aggregation, composition, dependency, inheritance, implementation; multiplicity: 1, 0..1, , 1.., 0..*