Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

Santa Claus Pattern

$
0
0
In a previous post, I talked about the Water Drop Anti-Pattern, where the ratio of payload to data was leading to poor performances.

A way to alleviate this problem is to introduce the Santa Claus Pattern. Like Santa Claus, we will make packets. But instead of packing presents, we will pack several data messages into a bigger one. This way, the payload of sending this packet on the intranet will be insignificant compared to the useful data. Also, for a user interface, we will spare several repaints, leading to better performance. There are of course several ways to implement this pattern. A possible solution is to use a timer:

public void init() {
    executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(this::sendData, DELAY, DELAY, TimeUnit.SECONDS);
}private void sendData() {if (!queue.isEmpty()) {
        List packet = new ArrayList<>();
        queue.drainTo(packet);
        sendPAcket(packet);
    }
}

But be careful with this pattern, as you might wake up the Godzilla Anti-Pattern!


Viewing all articles
Browse latest Browse all 73

Trending Articles