3.6.17 Representing Interfaces in Class Diagrams

Representing Interfaces in Class Diagrams

An interface names a behavior contract that different soccer-related classes can implement. It is not an instance containing the current player's state.

Read the Code Before Drawing

These complete declarations define a contract requiring RecordTurn and a Player implementation that increments its count.

Source to translate
public interface ITrainable
{
    void RecordTurn();
}

public class Player : ITrainable
{
    public int NumberOfTurns;

    public void RecordTurn()
    {
        NumberOfTurns = NumberOfTurns + 1;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define ITrainable as a behavior contract
Require a public RecordTurn operation returning no value
Define Player as one implementation
Store a turn count in Player, not in this interface
Implement the operation by increasing that count

These words describe the class contract, not a sequence of runtime actions. Identify the types, stored members, operation inputs and results, and relationships before choosing compartments and connectors.

Map the Source to Class Boxes and Connectors
Build the Diagram in Violet
  1. Create a class diagram with an interface box ITrainable marked <<interface>> and a class box Player.
  2. In the interface's operations enter +RecordTurn() : void. Leave its attributes empty for this source.
  3. In Player enter attribute +NumberOfTurns : int and operation +RecordTurn() : void.
  4. Draw dashed realization from Player to ITrainable, with a hollow triangle at the interface. Keep the method signatures consistent across the two boxes.
  5. Save and identify the required behavior separately from Player's implementation storage. No current turn count or concrete Player instance belongs in this class view.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

ITrainable requires RecordTurn. Player realizes the interface with a public operation and its own turn-count field.
A behavior contract and one implementation
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

An ITrainable reference can refer to a Player implementation and call the contracted operation. It does not mean new ITrainable() is valid.

Verify the Drawing Against the Source
Predict and Check a Small Change