L3 Class Diagrams Flashcards
What is UML?
Unified Modelling Language
is a commonly accepted standard way to visualise the design of a software system
What is the use of UML?
UML, especially class diagrams:
○ OOP design patterns are typically drawn using class diagrams
○ Easier to fit more information in less space (e.g. compared to code)
○ Fast to create and reason about
○ “Lingua franca” in the OOP design world
What are the perspectives of class diagrams
● Conceptual: focus on domain concepts, e.g. Bank, Account, ATM
● Specification: focus on the interfaces of Abstract Data Types (ADT)
● Implementation: focus on implementation details
What does an Association relationship mean in UML?
Two classes need to communicate with each other
What does an Aggregation relationship mean in UML? (Indicated by white diamond)
Implies that a class can exist without another class
What does a Composition relationship mean in UML?
Implies that a class cannot exist without another class
Classes in UML
Generalisation (inheritance)
Abstract Classes
● Abstract classes allow programmers to define methods without implementing them. All derived classes are then forced to provide implementations for these methods.
● Abstract classes and methods are italic
● Extends relationship is
used between the abstract class and its derived class (empty triangle from derived towards abstract class)
Type of generalisation(inheritance)
● There are two types of generalisation (inheritance) in OOP:
○ Interface inheritance
○ Implementation inheritance
● In Java, interface inheritance is achieved using interfaces
● Use implementation inheritance only if you want to reuse all methods (operations) of the parent class and add some extra methods to the derived class
● Otherwise do not use inheritance
● NB. a derived class in Java can inherit from only one parent class
Realisation (implements)
● In Java, realisation is achieved through interfaces
● Interfaces:
○ Contain only method signatures that describe the behaviour of a class (i.e. what behaviour a class needs to provide)
○ Class can implement multiple interfaces (i.e. a class can have multiple behaviours)
○ Class must match method signatures of all interfaces which it is implementing
Interfaces and abstract classes
● Interfaces and abstract classes are a cornerstone of OOP (key to software architecture)
● Interfaces/abstract classes:
○ Enable (dynamic) polymorphism
○ Reverse source code dependencies
○ Separate operations (important) from their concrete implementations (less important)
○ Form part of almost every software design pattern
Multiplicity
Steps to draw class diagrams
● Grammatical approach:
○ Classes and attributes are nouns:
■ Use tangible entities (things) in the application domain such as aircrafts, vehicles, servers, resources
■ Roles such as managers, customers, operators
■ Interactions such as meetings
■ Locations such as offices, companies etc.
○ Operations are verbs, i.e. actions carried out by a class
● Relationships:
○ Is the class a subclass of another? If so, the relationship is generalisation (inheritance).
○ Is the class part of another? If so, the relationship is aggregation or composition.
○ Does the class interact with another? If so, the relationship is association.
Grammatical approach example
Grammatical approach example