In my previous entry, I was describing the Rocket Anti-Pattern, where one thread would handle too much work across several application layers, leading to communication buffer overflow and message loss.
Here is an equivalent of the application shown in the previous entry, using our new model:
public class Comm {
public void onMessage(Message m) {
Data data = transcode(m);
calculator.push(data);
}
}
}
public class Calculator {
public void push(Data data) {
executor.execute(() -> calculatePrice(data));
}
}
public void calculatePrice(Data data) {
data.price = data.unitPrice * data.quantity;
Display.show(data);
}
}
}
public class Display {
public void show(Data data) {
SwingUtilities.invokeLater(() -> table.addRow(data));
}
}
}
Each class represents a layer, and propagates the data to the next layer, which will in turn ask the layer’s dedicated thread to handle it.
To implement this model, you will have to use the Marsupilami Pattern. Wait for it!
No comments:
Post a Comment