Skip to content

Instantly share code, notes, and snippets.

@dynoChris
Last active December 16, 2021 14:10
Show Gist options
  • Save dynoChris/eeedc2a66267fe61646fe15c172158c5 to your computer and use it in GitHub Desktop.
Save dynoChris/eeedc2a66267fe61646fe15c172158c5 to your computer and use it in GitHub Desktop.
Toggler Pattern
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