mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-13 00:19:13 +00:00
* docs: reformulate text, add links to further languages and paragraph about the book in central README.md (#2275) * docs: shorten text for link to payhip (#2275) * docs: reformulate german translation for Abstract Document (#2275) * Create folder for Abstract Factory * Create README.md for Abstract Document * Translate README.md for Abstract Document to German * correct typo * docs: add class diagrams for all patterns (#2275) * docs: add missing words in abstract-factory.README.md (#2275) * docs: Create German README.md for Active Object #2275 first text part * docs: finish German translation of README.md for Active Object * docs: translate actor-model into German #2295 * docs: add German translation for Acyclic Visitor #2295 * correct typo * docs: Translate Singleton into German #2295 * docs: Translate Prototype into German #2295 * Delete localization/de/Abstract Factory * insert line break * correct typos in actor-model * realignment of java code block * docs: translate Adapter Pattern into German #2295 * Update README.md change 'lizenz' to 'Lizenz' in badge * Update README.md translate 'benefits' and 'trade-offs' * remove additional bullet point after line break * correct indentation * docs: translate Dependency Injection into German #2295 * docs: translate Bridge into German #2295 * correct typos * docs: Translate Builder into German * delete english text * little changes in formulation * Update README.md change link to image * docs: remove spanish files (#2275) * docs: remove non-translated patterns (#2275) * Create README.md docs: Translate Step Builder into German #2275 * Create README.md docs: Translate Fluent Interface into German #2275 * eager is eifrig, not gierig * Translate Chain of Responsibility into German #2275
204 lines
4.8 KiB
Markdown
204 lines
4.8 KiB
Markdown
---
|
||
shortTitle: Actor Model
|
||
category: Concurrency
|
||
language: de
|
||
tag:
|
||
- Concurrency
|
||
- Messaging
|
||
- Isolation
|
||
- Asynchronous
|
||
- Distributed Systems
|
||
- Actor Model
|
||
---
|
||
|
||
## Alternativbezeichnungen
|
||
|
||
- Message-passing concurrency
|
||
- Actor-based concurrency
|
||
|
||
---
|
||
|
||
## Zweck
|
||
|
||
Das Actor-Model-Pattern ermöglicht die Konstruktion von hochparallelen fehlertoleranten verteilten Systemen,
|
||
indem es isolierte Komponenten (Akteure) verwendet, die ausschließlich über asynchronen Nachrichtenaustausch interagieren.
|
||
|
||
## Detaillierte Erklärung
|
||
|
||
---
|
||
|
||
### 📦 Reales Beispiel
|
||
|
||
Stellen Sie sich ein Kundendienstsystem vor.
|
||
- Jeder **Kundendienstmitarbeiter** ist ein **Aktor**.
|
||
- Kunden **senden Anfragen (Nachrichten)** an die Mitarbeiter.
|
||
- Jeder Mitarbeiter behandelt zu einem bestimmten Zeitpunkt genau eine Anfrage und kann diese **asynchron beantworten**,
|
||
ohne dabei anderen Mitarbeitern in die Quere zu kommen.
|
||
|
||
---
|
||
|
||
### 🧠 In einfachen Worten
|
||
|
||
> "Aktoren sind wie unabhängige Arbeiter, die keine Ressourcen teilen und nur über Nachrichten kommunizieren."
|
||
|
||
---
|
||
|
||
### 📖 Wikipedia sagt
|
||
|
||
> Das [Actor Model](https://en.wikipedia.org/wiki/Actor_model) ist ein mathematisches Modell
|
||
> für parallele Informationsverarbeitung, das "Aktoren" als universelle Ausführer von Aufgaben betrachtet.
|
||
|
||
---
|
||
|
||
### 🧹 Klassendiagramm
|
||
|
||

|
||
|
||
---
|
||
|
||
## Programmbeispiel in Java
|
||
|
||
### Actor.java
|
||
|
||
```java
|
||
public abstract class Actor implements Runnable {
|
||
|
||
@Setter
|
||
@Getter
|
||
private String actorId;
|
||
private final BlockingQueue<Message> mailbox = new LinkedBlockingQueue<>();
|
||
private volatile boolean active = true;
|
||
|
||
|
||
public void send(Message message) {
|
||
mailbox.add(message);
|
||
}
|
||
|
||
public void stop() {
|
||
active = false;
|
||
}
|
||
|
||
@Override
|
||
public void run() {
|
||
|
||
}
|
||
|
||
protected abstract void onReceive(Message message);
|
||
}
|
||
|
||
```
|
||
|
||
### Message.java
|
||
|
||
```java
|
||
|
||
@AllArgsConstructor
|
||
@Getter
|
||
@Setter
|
||
public class Message {
|
||
private final String content;
|
||
private final String senderId;
|
||
}
|
||
```
|
||
|
||
### ActorSystem.java
|
||
|
||
```java
|
||
public class ActorSystem {
|
||
public void startActor(Actor actor) {
|
||
String actorId = "actor-" + idCounter.incrementAndGet(); // Generate a new and unique ID
|
||
actor.setActorId(actorId); // assign the actor it's ID
|
||
actorRegister.put(actorId, actor); // Register and save the actor with it's ID
|
||
executor.submit(actor); // Run the actor in a thread
|
||
}
|
||
public Actor getActorById(String actorId) {
|
||
return actorRegister.get(actorId); // Find by Id
|
||
}
|
||
|
||
public void shutdown() {
|
||
executor.shutdownNow(); // Stop all threads
|
||
}
|
||
}
|
||
```
|
||
|
||
### App.java
|
||
|
||
```java
|
||
public class App {
|
||
public static void main(String[] args) {
|
||
ActorSystem system = new ActorSystem();
|
||
Actor srijan = new ExampleActor(system);
|
||
Actor ansh = new ExampleActor2(system);
|
||
|
||
system.startActor(srijan);
|
||
system.startActor(ansh);
|
||
ansh.send(new Message("Hello ansh", srijan.getActorId()));
|
||
srijan.send(new Message("Hello srijan!", ansh.getActorId()));
|
||
|
||
Thread.sleep(1000); // Give time for messages to process
|
||
|
||
srijan.stop(); // Stop the actor gracefully
|
||
ansh.stop();
|
||
system.shutdown(); // Stop the actor system
|
||
}
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## Verwendung
|
||
|
||
- Bei der Konstruktion **paralleler oder verteilter Systeme**
|
||
- Wenn **keine veränderlichen Zustände geteilt** werden sollen
|
||
- Wenn **asynchrone, nachrichtenbasierte Kommunikation** benötigt wird
|
||
- Wenn die Komponenten **isoliert und lose gekoppelt** sein sollen.
|
||
|
||
---
|
||
|
||
## Tutorials
|
||
|
||
- [Baeldung – Akka with Java](https://www.baeldung.com/java-akka)
|
||
- [Vaughn Vernon – Reactive Messaging Patterns](https://vaughnvernon.co/?p=1143)
|
||
|
||
---
|
||
|
||
## Reale Anwendungen
|
||
|
||
- [Akka Framework](https://akka.io/)
|
||
- [Concurrency in Erlang und Elixir](https://www.erlang.org/)
|
||
- [Microsoft Orleans](https://learn.microsoft.com/en-us/dotnet/orleans/)
|
||
- JVM-basierte Spiel-Engines und Simulatoren
|
||
|
||
---
|
||
|
||
## Vor- und Nachteile
|
||
|
||
### ✅ Vorteile
|
||
- Unterstützt hohes Maß an Parallelität
|
||
- Leichte Skalierbarkeit über Zahl der Threads oder Prozessoren.
|
||
- Fehlerisolation und -behebbarkeit.
|
||
- Geordnete Nachrichten in den Aktoren
|
||
|
||
### ⚠️ Nachteile
|
||
- Schwierigeres Debugging wegen asynchronen Verhaltens
|
||
- Leichte Performance-Einbußen durch die Nachrichten-Warteschlangen
|
||
- Komplexeres Design als bei einfachem Methodenaufruf
|
||
---
|
||
|
||
## Verwandte Patterns
|
||
|
||
- [Command Pattern](../command)
|
||
- [Mediator Pattern](../mediator)
|
||
- [Event-Driven Architecture](../event-driven-architecture)
|
||
- [Observer Pattern](../observer)
|
||
|
||
---
|
||
|
||
## Quellen
|
||
|
||
- *Programming Erlang*, Joe Armstrong
|
||
- *Reactive Design Patterns*, Roland Kuhn
|
||
- *The Actor Model in 10 Minutes*, [InfoQ Article](https://www.infoq.com/articles/actor-model/)
|
||
- [Akka Documentation](https://doc.akka.io/docs/akka/current/index.html)
|
||
|