# 📖💡 Exkurs: Objektdiagramm

## Das Objektdiagramm

Ein Objektdiagramm ist im Gegensatz zum Klassendiagramm eine Momentaufnahme eines Programms. Auch für das Objektdiagramm gibt es einen UML Standard. Nachfolgend seht ihr ein Objektdiagramm mit einem einzelnen Objekt.

![](/files/-MAWH7l-2DXIHNHyUO5T)

Quelle: <http://mbse.se-rwth.de/book1/index.php?c=chapter4-1>

## Beispiel

In diesem Beispiel gehen wir das Beispiel aus [Aufgabe 11 der Übung C# Grundlagen](/tag-1/uebung-c-grundlagen.md#aufgabe-11) durch und zeichnen Schritt für Schritt ein Objektdiagramm. Wichtig dabei, das Objektdiagramm ist immer eine **Momentaufnahme**, es ändert sich also nach jeder Zeile Code, die ausgeführt wurde.

Gegeben sind folgende Klassen:

```csharp
public class Baum
{
    int _hoehe, _breite;
    
    public Baum(int hoehe, int breite)
    {
        _hoehe = hoehe;
        _breite = breite; 
    }
    public int Hoehe
    {
        get { return _hoehe; }
    }
    public int Breite
    {
        get { return _breite; }
    }
}

public class Punkt
{
    int _x, _y;
    public Punkt(int x, int y)
    {
        _x = x;
        _y = y;
    }
    public int X
    {
        get { return _x; }
        set { _x = value; }
    }
    public int Y
    {
        get { return _y; }
        set { _y = value; }
    }
}
```

### Ausgangslage

{% tabs %}
{% tab title="Ausgangslage" %}

```csharp
string s = "abcd";
int[] a1 = { 1, 2, 3, 4 };
byte b = 56;
Punkt p = new Punkt(a1[0], 100); 
Baum[] ba = new Baum[3];
Baum baum = new Baum(80, 2);
```

![](/files/-MAWPnICirNqHPUQ2vrR)
{% endtab %}

{% tab title="Schritt für Schritt" %}

### Ausgangslage 1. Schritt

```csharp
string s = "abcd";
```

![](/files/-MAWMmhuSY_wlmcoDacE)

### Ausgangslage 2. Schritt

```csharp
int[] a1 = { 1, 2, 3, 4 };
```

![](/files/-MAWN4zTJ_2H5sJ0naWA)

### Ausgangslage 3. Schritt

```csharp
byte b = 56;
```

![](/files/-MAWNGQBdD6c9nHe7gq1)

### Ausgangslage 4. Schritt

```csharp
Punkt p = new Punkt(a1[0], 100); 
```

![](/files/-MAWNtv7ZAyudnBJ7Alu)

### Ausgangslage 5. Schritt

```csharp
Baum[] ba = new Baum[3];
```

![](/files/-MAWOk7A9Q8UkA7F4s7K)

### Ausgangslage 6. Schritt

```csharp
Baum baum = new Baum(80, 2);
```

![](/files/-MAWPQO_HTekfA74zRRX)
{% endtab %}
{% endtabs %}

###

### 1. Schritt

```csharp
a1[0] = b;
```

![](/files/-MAWRMmWztvHA410u3hS)

### 2. Schritt

```csharp
ba[1] = new Baum(2, 3); 
```

![](/files/-MAWSMl-SDkS6aDkvjQP)

### 3. Schritt

```csharp
ba[2] = baum;
```

![](/files/-MAWTBZDgcvhegGBcW_w)

{% hint style="warning" %}
Beachte, dass das Objekt **nicht kopiert** wird. Es gibt jetzt lediglich eine zweite Referenz auf dieses Objekt. Es existiert aber nur einmal im Speicher.
{% endhint %}

### 4. Schritt

```csharp
Baum neuerBaum = ba[0];
```

![](/files/-MAWUGgMm2fIuwQZXcg8)

### 5. Schritt

```csharp
a1[0] = baum.Hoehe;
```

![](/files/-MAWUwjnTZwfEu4Sdm06)

### 6. Schritt

```csharp
p.Y = baum.Hoehe;
```

![](/files/-MAWVdKP0IwSZ7f_j3Hd)

### 7. Schritt

```csharp
ba = null;
```

![](/files/-MAWW8BDBVeSNa2VGrif)

{% hint style="info" %}
Da es keine Referenz mehr auf das Baum Array gibt, wird dies irgendwann vom Garbage Collector abgeräumt und aus dem Speicher gelöscht. Dasselbe passiert dann mit dem Objekt unten rechts, da es keine Referenz mehr darauf gibt.
{% endhint %}

### 8. Schritt

```csharp
p.X = s.Length;
```

![](/files/-MAWXpmc1mUB8S0DGD9m)

So ergibt sich schlussendlich dieses Objektdiagramm:

![](/files/-MAWXxUlsRO7cXFvQqe1)

## 💡 Aufgabe

Zeichne das Objektdiagramm auf Papier nach der Ausführung des folgenden Codes:

```csharp
ba = new Baum[] { neuerBaum, baum, new Baum(50, 8) };
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://m318.ict-bz.ch/tag-1/oop-grundlagen/exkurs-objektdiagramm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
