Created
November 13, 2023 04:57
-
-
Save AdamZWinter/c4b382e142aa67698ce0ef5f20ff6867 to your computer and use it in GitHub Desktop.
Singleton Object Class Java
This file contains 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 BandRepository { | |
private static BandRepository instance; | |
private List<Band> mBands; | |
public static BandRepository getInstance(Context context) { | |
if (instance == null) { | |
instance = new BandRepository(context); | |
} | |
return instance; | |
} | |
private BandRepository(Context context) { | |
mBands = new ArrayList<>(); | |
Resources res = context.getResources(); | |
String[] bands = res.getStringArray(R.array.bands); | |
String[] descriptions = res.getStringArray(R.array.descriptions); | |
for (int i = 0; i < bands.length; i++) { | |
mBands.add(new Band(i + 1, bands[i], descriptions[i])); | |
} | |
} | |
public List<Band> getBands() { | |
return mBands; | |
} | |
public Band getBand(int bandId) { | |
for (Band band : mBands) { | |
if (band.getId() == bandId) { | |
return band; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment