New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename MutableBehavior to AbstractBehavior #25750

Closed
patriknw opened this Issue Oct 8, 2018 · 0 comments

Comments

1 participant
@patriknw
Member

patriknw commented Oct 8, 2018

Because it's intended usage is more than just capturing a mutable variable. Especially in Java the OO style is useful also for capturing (immutable) parameters and scoping methods.

An example of perfectly valid usage is to model distinct FSM states as different behavior classes, like this:

public abstract class DistributedLock {

  public static Behavior<LockCommand> behavior() {
    return new Unlocked();
  }

  private static class Unlocked extends AbstractBehavior<LockCommand> {

    @Override
    public Behaviors.Receive<LockCommand> createReceive() {
      return receiveBuilder()
        .onMessage(Lock.class, this::receiveLock)
        .build();
    }

    private Behavior<LockCommand> receiveLock(Lock lock) {
      lock.lockRequestor.tell(Granted.INSTANCE);
      return new Locked(lock.lockRequestor);
    }
  }

  private static class Locked extends AbstractBehavior<LockCommand> {
    private final ActorRef<LockStatus> owner;

    private Locked(ActorRef<LockStatus> owner) {
      this.owner = owner;
    }

    @Override
    public Behaviors.Receive<LockCommand> createReceive() {
      return receiveBuilder()
        .onMessage(Unlock.class, this::receiveUnlock)
        .onMessage(Lock.class, this::receiveLock)
        .build();
    }

    private Behavior<LockCommand> receiveUnlock(Unlock unlock) {
      if (owner.equals(unlock.owner)) {
        return new Unlocked();
      } else {
        return this;
      }
    }

    private Behavior<LockCommand> receiveLock(Lock lock) {
      lock.lockRequestor.tell(new Taken(owner));
      return this;
    }
  }

}

@patriknw patriknw added this to Backlog in Akka Typed Oct 15, 2018

patriknw added a commit that referenced this issue Oct 17, 2018

Rename MutableBehavior to AbstractBehavior, #25750
* Also cleanup javadsl Receive, which is only used from AbstractBehavior
* Clarify further in docs that the functional vs OO style is a matter of taste

@patriknw patriknw moved this from Backlog to In Progress in Akka Typed Oct 17, 2018

@patriknw patriknw self-assigned this Oct 17, 2018

@patriknw patriknw moved this from In Progress to Reviewing in Akka Typed Oct 17, 2018

patriknw added a commit that referenced this issue Oct 18, 2018

Merge pull request #25804 from akka/wip-25750-AbstractBehavior-patriknw
Rename MutableBehavior to AbstractBehavior, #25750

@patriknw patriknw added this to the 2.5.18 milestone Oct 18, 2018

@patriknw patriknw closed this Oct 18, 2018

@patriknw patriknw moved this from Reviewing to Done in Akka Typed Oct 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment