Last active
December 16, 2021 14:10
-
-
Save dynoChris/eeedc2a66267fe61646fe15c172158c5 to your computer and use it in GitHub Desktop.
Toggler Pattern
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 Toggler { | |
private static final int DEFAULT_MODE = 1; //count = 2: *,-,*,-,*,-,*,-... | |
//count = 1: *,*,*,*,*,*,*,*... | |
//count = 3: *,-,-,*,-,-,*,-... | |
public static final int WAIT_MODE = 2; //count = 2: -,*,*,*,*,*,*,*,*... | |
//count = 1: *,*,*,*,*,*,*,*,*... | |
//count = 3: -,-,*,*,*,*,*,*,*... | |
private int mMode = DEFAULT_MODE; | |
private final int mCount; | |
private int mLap; | |
//через count раз | |
public Toggler(int count) { | |
this.mCount = count; | |
this.mLap = count; | |
} | |
//пропустить count раз, затем всегда срабатывать | |
public Toggler(int count, int mode) { | |
mMode = mode; | |
this.mCount = count; | |
this.mLap = count; | |
if (mMode == WAIT_MODE) { | |
mLap = 1; | |
} | |
} | |
public int getLap() { | |
return mLap; | |
} | |
public boolean toggle() { | |
if (mMode == DEFAULT_MODE) { | |
if (mLap == mCount) { | |
mLap = 1; | |
return true; | |
} else { | |
mLap++; | |
return false; | |
} | |
} else if (mMode == WAIT_MODE) { | |
if (mLap == mCount) { | |
return true; | |
} else { | |
mLap++; | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment