Skip to content

Instantly share code, notes, and snippets.

@NNNIC
Created August 21, 2018 13:57
Show Gist options
  • Save NNNIC/2fc98b6709988d372041848e7e7ac928 to your computer and use it in GitHub Desktop.
Save NNNIC/2fc98b6709988d372041848e7e7ac928 to your computer and use it in GitHub Desktop.
State Manager Java sample
import java.util.function.Consumer;
public class StateManager {
Consumer<Boolean> m_curfunc;
Consumer<Boolean> m_nextfunc;
public void Goto(Consumer<Boolean> func) {
m_nextfunc = func;
}
public void Update() {
Boolean bFirst = false;
if (m_nextfunc != null) {
m_curfunc = m_nextfunc;
m_nextfunc = null;
bFirst = true;
}
if (m_curfunc!=null) {
m_curfunc.accept(bFirst);
}
}
public Boolean CheckState(Consumer<Boolean> func) {
return (m_curfunc == func);
}
}
import java.util.function.Consumer;
class Sub extends StateManager {
Consumer<Boolean> S_START=(bFirst)->{
if (bFirst) {
System.out.println("Into S_START");
}
this.Goto(this.S_0001);
};
Consumer<Boolean> S_0001=(bFirst)->{
if (bFirst) {
System.out.println("Into S_0001");
}
this.Goto(this.S_END);
};
Consumer<Boolean> S_END=(bFirst)->{
if (bFirst) {
System.out.println("Into S_END");
}
};
public void Start()
{
this.Goto(this.S_START);
}
public Boolean IsEnd()
{
return this.CheckState(this.S_END);
}
}
public class Test {
public static void main(String[] args) {
System.out.println("Hello!");
Sub sm = new Sub();
sm.Start();
while(!sm.IsEnd()){
sm.Update();
}
System.out.println("Bye!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment