Skip to content

Commit

Permalink
TransitionsBuilderImpl cannot be inherited from TransitionBuilderImpl…
Browse files Browse the repository at this point in the history
… because they are at the same level and should not be in a parent-child relationship. If it is inheritance, the TransitionBuilderImpl should include the TransitionsBuilderImpl responsibility, which violates the single principle
  • Loading branch information
chenggwang committed Jul 14, 2023
1 parent aa7f604 commit afe5e99
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.alibaba.cola.statemachine.builder;

import com.alibaba.cola.statemachine.State;
import com.alibaba.cola.statemachine.impl.StateHelper;
import com.alibaba.cola.statemachine.impl.TransitionType;

import java.util.Map;

/**
* Take TransitionBuilderImpl and TransitionsBuilderImpl sharing variables
* and methods to that abstract class, which acts as their parent,
* instead of having TransitionsBuilderImpl inherit from
* TransitionsBuilderImpl. I think that the multi-flow
* builder(TransitionsBuilderImpl) and single-flow
* builder(TransitionBuilderImpl) are equal and not supposed to be
* parent-child relationship, they from, when, and perform methods
* are not the same, and although it looks like just a set of loops
* but logically should not be inherited over Override.
* ( Just as there was no relationship, why should we talk to each other,
* say a we are not suitable). With the abstract class, multi-flow and single-flow
* only use to focus on their respective functions are single-flow,
* or multi-flow. Conform to a single duty.
* @author welliem
* @date 2023-07-14 12:13
*/
abstract class AbstractTransitionBuilder<S,E,C> implements From<S,E,C>,On<S,E,C>,To<S,E,C>{

final Map<S, State<S, E, C>> stateMap;

protected State<S, E, C> target;

final TransitionType transitionType;

public AbstractTransitionBuilder(Map<S, State<S, E, C>> stateMap, TransitionType transitionType) {
this.stateMap = stateMap;
this.transitionType = transitionType;
}
@Override
public To<S, E, C> to(S stateId) {
target = StateHelper.getState(stateMap, stateId);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@
* @author Frank Zhang
* @date 2020-02-07 10:20 PM
*/
class TransitionBuilderImpl<S,E,C> implements ExternalTransitionBuilder<S,E,C>, InternalTransitionBuilder<S,E,C>, From<S,E,C>, On<S,E,C>, To<S,E,C> {
class TransitionBuilderImpl<S,E,C> extends AbstractTransitionBuilder<S,E,C> implements ExternalTransitionBuilder<S,E,C>, InternalTransitionBuilder<S,E,C> {

final Map<S, State<S, E, C>> stateMap;

private State<S, E, C> source;

protected State<S, E, C> target;

private Transition<S, E, C> transition;

final TransitionType transitionType;

public TransitionBuilderImpl(Map<S, State<S, E, C>> stateMap, TransitionType transitionType) {
this.stateMap = stateMap;
this.transitionType = transitionType;
super(stateMap, transitionType);
}

@Override
Expand All @@ -38,12 +31,6 @@ public From<S, E, C> from(S stateId) {
return this;
}

@Override
public To<S, E, C> to(S stateId) {
target = StateHelper.getState(stateMap, stateId);
return this;
}

@Override
public To<S, E, C> within(S stateId) {
source = target = StateHelper.getState(stateMap, stateId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author Frank Zhang
* @date 2020-02-08 7:43 PM
*/
public class TransitionsBuilderImpl<S,E,C> extends TransitionBuilderImpl<S,E,C> implements ExternalTransitionsBuilder<S,E,C> {
public class TransitionsBuilderImpl<S,E,C> extends AbstractTransitionBuilder<S,E,C> implements ExternalTransitionsBuilder<S,E,C> {
/**
* This is for fromAmong where multiple sources can be configured to point to one target
*/
Expand Down

0 comments on commit afe5e99

Please sign in to comment.