Last active
March 17, 2021 15:18
-
-
Save Vinodh-thimmisetty/8b151fd72877e7d67a4d7ec635518cad to your computer and use it in GitHub Desktop.
Simple Factory design pattern example
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
package factory; | |
import java.util.Scanner; | |
/** | |
* @author thimmv | |
*/ | |
public class SimpleFactoryTest { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Choose one of the maps \n" + | |
"1 >> Erangel \n" + | |
"2 >> Miramer \n" + | |
"3 >> Sanhok \n" + | |
"4 >> Vikendi \n"); | |
int id = sc.nextInt(); | |
PUBG_MAP pubg_Map = PUBG_MAP.getMap(id); | |
System.out.println("You've choosen " + pubg_Map + " Map. Fasten your seat belts, we will take you into the world of PUBG..."); | |
Pubg pubg = PubgFactory.loadGame(pubg_Map); | |
pubg.playGame(); | |
} | |
} | |
enum PUBG_MAP { | |
ERANGEL(1), | |
MIRAMAR(2), | |
SANHOK(3), | |
VIKENDI(4); | |
private int id; | |
public int getId() { | |
return id; | |
} | |
PUBG_MAP(int id) { | |
this.id = id; | |
} | |
public static PUBG_MAP getMap(int id) { | |
PUBG_MAP[] values = PUBG_MAP.values(); | |
for (final PUBG_MAP value : values) { | |
if (id == value.getId()) { | |
return value; | |
} | |
} | |
System.out.println("In-valid selection... So loading Default Map..."); | |
return PUBG_MAP.ERANGEL; | |
} | |
} | |
interface Pubg { | |
void playGame(); | |
} | |
class ErangleMap implements Pubg { | |
ErangleMap() { | |
System.out.println("Loading..... erangle"); | |
} | |
public void playGame() { | |
System.out.println("Playing ... erangle"); | |
} | |
} | |
class MiramarMap implements Pubg { | |
MiramarMap() { | |
System.out.println("Loading..... miramar"); | |
} | |
public void playGame() { | |
System.out.println("Playing ... miramar"); | |
} | |
} | |
class SanhokMap implements Pubg { | |
SanhokMap() { | |
System.out.println("Loading..... sanhok"); | |
} | |
public void playGame() { | |
System.out.println("Playing ... sanhok"); | |
} | |
} | |
class VikendiMap implements Pubg { | |
VikendiMap() { | |
System.out.println("Loading..... vikendi"); | |
} | |
public void playGame() { | |
System.out.println("Playing ... vikendi"); | |
} | |
} | |
class PubgFactory { | |
static Pubg loadGame(PUBG_MAP pubgMap) { | |
switch (pubgMap) { | |
case MIRAMAR: | |
return new MiramarMap(); | |
case SANHOK: | |
return new SanhokMap(); | |
case VIKENDI: | |
return new VikendiMap(); | |
default: | |
return new ErangleMap(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I need help my PUBG account was hacked when you gave me back my PUBG account I gave you money plz