Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy pattern in Akka

This is an continuation of my previous question How do I get around type erasure on Akka receive method

I have 10 type of events which extends from Event that I need to handle.

I want to implement business logic for each event in separate trait, because because mixing all 10 event handler functions will produce several hundreds(if not thousands) lines of code.

I don't want to create different Actor types for each event. For example:

class Event1Actor extend Actor{
  def receive ={
     case Event1(e) => //event1 Business Logic
   }
}

class Event2Actor extend Actor{
  def receive ={
     case Event2(e) => //event2 Business Logic
   }
}  

and the same Event3Actor, Event4Actor,etc....

Such code seems ugly to me, because I need to implement business Logic inside each Actor.

Implementing 10 different traits and 10 different Actor classes seems also as bad design.

I'm seeking for some kind generic solution based on design pattern, for example strategy pattern.

like image 422
danny.lesnik Avatar asked Feb 05 '15 10:02

danny.lesnik


1 Answers

case class EventOperation[T <: Event](eventType: T)

class OperationActor extends Actor {

  def receive = {
    case EventOperation(eventType) => eventType.execute
  }

}

trait Event {
 def execute //implement execute in specific event class
}

class Event1 extends Event {/*execute implemented with business logic*/}
class Event2 extends Event {/*execute implemented with business logic*/}

hope this is what you are looking for and helps, I have used this patternt to remove the redundant amount of actors wrapping all actions under a single actor executing different type of events.

like image 95
DAR Avatar answered Oct 07 '22 21:10

DAR