Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| de:modul:m320_2024:learningunits:lu09:zweiseitige_mehrfachbeziehung [2026/08/30 14:05] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | de:modul:m320_2024:learningunits:lu09:zweiseitige_mehrfachbeziehung [2026/08/30 14:12] (aktuell) – ↷ Links angepasst, weil Seiten im Wiki verschoben wurden 74.7.243.195 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== LU09b - Zweiseitige Mehrfachbeziehung (n zu n) ====== | ||
| + | |||
| + | Aus der realen Welt sind Mehrfachbeziehungen bestens bekannt. | ||
| + | So kann eine Feriendestination von vielen Personen gebucht werden und umgekehrt kann eine Person viele Destinationen besuchen. | ||
| + | |||
| + | Diese Beziehung wird wie folgt im UML-Klassendiagramm dargestellt. | ||
| + | |||
| + | |||
| + | {{de: | ||
| + | |||
| + | //Abb: n:n Beziehung// | ||
| + | |||
| + | Bei einer zweiseitigen Mehrfachbeziehung enthält jedes Objekt eine Liste mit Referenzen zu den verbundenen Objekten. | ||
| + | Beim Hinzufügen ('' | ||
| + | Dabei muss verhindert werden, dass ein Endlos-Loop entsteht, wenn sie die '' | ||
| + | |||
| + | ===== Umsetzung in Python ===== | ||
| + | Für unsere Umsetzung prüfen wir zuerst, ob das Objekt in der Liste vorhanden ist. | ||
| + | Nur wenn dies der Fall ist, wird die Beziehung hinzugefügt bzw. gelöscht. | ||
| + | |||
| + | <code python> | ||
| + | class HolidayDestination: | ||
| + | def __init__(self, | ||
| + | self._location = location | ||
| + | self._people = [] | ||
| + | |||
| + | def __str__(self): | ||
| + | output = f' | ||
| + | for person in self._people: | ||
| + | output += f' {person._name},' | ||
| + | output += ' | ||
| + | return output | ||
| + | |||
| + | def add_person(self, | ||
| + | if not person in self._people: | ||
| + | self._people.append(person) | ||
| + | person.add_destination(self) | ||
| + | |||
| + | def remove_person(self, | ||
| + | if person in self._people: | ||
| + | self._people.remove(person) | ||
| + | person.remove_destination(self) | ||
| + | |||
| + | |||
| + | class Person: | ||
| + | def __init__(self, | ||
| + | self._name = name | ||
| + | self._destinations = [] | ||
| + | |||
| + | def __str__(self): | ||
| + | output = f' | ||
| + | for destination in self._destinations: | ||
| + | output += f' {destination._location},' | ||
| + | output += ' | ||
| + | return output | ||
| + | |||
| + | def add_destination(self, | ||
| + | if not destination in self._destinations: | ||
| + | self._destinations.append(destination) | ||
| + | destination.add_person(self) | ||
| + | |||
| + | def remove_destination(self, | ||
| + | if destination in self._destinations: | ||
| + | self._destinations.remove(destination) | ||
| + | destination.remove_person(self) | ||
| + | |||
| + | |||
| + | def main(): | ||
| + | # let's start by creating some people and destinations | ||
| + | maya = Person(' | ||
| + | thomas = Person(' | ||
| + | jusuf = Person(' | ||
| + | valley = HolidayDestination(' | ||
| + | atlantis = HolidayDestination(' | ||
| + | |||
| + | # now we let people visit the destinations | ||
| + | maya.add_destination(valley) | ||
| + | valley.add_person(thomas) | ||
| + | print(valley) | ||
| + | atlantis.add_person(maya) | ||
| + | jusuf.add_destination(atlantis) | ||
| + | print(atlantis) | ||
| + | print(maya, thomas, jusuf) | ||
| + | |||
| + | # try to add two duplicates | ||
| + | jusuf.add_destination(atlantis) | ||
| + | atlantis.add_person(maya) | ||
| + | print(atlantis) | ||
| + | |||
| + | # and now some people are cancelling their vacation | ||
| + | valley.remove_person(thomas) | ||
| + | print (valley) | ||
| + | jusuf.remove_destination(atlantis) | ||
| + | print (jusuf) | ||
| + | |||
| + | # finally we try to remove non-existing relationships | ||
| + | atlantis.remove_person(thomas) | ||
| + | print(atlantis) | ||
| + | jusuf.remove_destination(valley) | ||
| + | print (jusuf) | ||
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| + | |||
| + | </ | ||
| + | |||
| + | <WRAP center round tip 60%> | ||
| + | Natürlich könnte man die Methoden auch so anpassen, dass sie als Parameter einen Index, einen Key und/oder ein Objekt akzeptieren. | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | {{tag> | ||
| + | [[https:// | ||