Created
August 21, 2018 13:57
-
-
Save NNNIC/2fc98b6709988d372041848e7e7ac928 to your computer and use it in GitHub Desktop.
State Manager Java sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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