Akka actors + Cake pattern

A marriage made in heaven

(λx.x)eranga
Effectz.AI
2 min readMar 4, 2016

--

About cake pattern

Cake pattern allows to build applications as a layered components.

System = Composition of multiple components

To build a component it uses traits. Component consists with multiple layers. Because of the layered architecture of a component, it named as Cake Pattern.

I have written detailed post about cake pattern in here[cake pattern].

Scenario

In order to pass dependencies to akka actors, we can use cake pattern. Consider following scenario.

I need to send messages(share messages) to server via UDP socket. After sending messages, server sends some replies. I have used an actor to implement this functionality. Main function of this actor is send messages to server and handle server responses. Actor have a dependency for database in order to save the sending data.

I have implemented actor with companion object and wrapped with a trait in order to have cake pattern dependency.

Actors with companion object

Akkas recommended practise to implement actors is using a companion object. More information about companion object and akkas best practises are in here.

  1. Akka recommended practise
  2. Akka actor creation patterns
  3. Companion object

Following is my actor with companion object.

Following is the way to initialize this actor via companion object.

Cake pattern dependency

I have mentioned that my actor need a database dependency. In order to pass this dependency I have used cake pattern(wrap actor inside trait and pass the dependency).

Following is the database dependency

Following is the cassandra based database dependency/component

Following is the way to pass this dependency/component to actor via cake pattern.

Following is the way to initilize this actor via the database dependency component.

--

--