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 final class MyImmutableObject { | |
/** | |
* Both String and Integer Classes Are Immutable | |
*/ | |
private final String ObjectName; | |
private final Integer ObjectId; |
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 SimpleSingleton { | |
// this will hold the instance of the class | |
private static SimpleSingleton instance = new SimpleSingleton(); | |
// this will be our constructor it's empty for no reason you can put | |
// anything necessary to instantiate your class | |
private SimpleSingleton() {} | |
//this is the static method to return the instance of the class |
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 SimpleSingleton { | |
// this will hold the instance of the class | |
private static SimpleSingleton instance; | |
// this will be our constructor it's empty for no reason you can put | |
// anything necessary to instantiate your class | |
private SimpleSingleton() {} | |
//this is the static method to return the instance of the class |
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 SimpleSingleton { | |
// this will hold the instance of the class | |
private static SimpleSingleton instance; | |
//static block invocation is done first | |
static{ | |
try{ | |
instance = new SimpleSingleton(); | |
}catch(Exception e){ |
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.io.Serializable; | |
public class SimpleSingleton implements Serializable { | |
private static long serialVersionUID = 1L; | |
// this will hold the instance of the class | |
private static SimpleSingleton instance = new SimpleSingleton(); | |
// this will be our constructor it's empty for no reason you can put |
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 enum EnemyType { | |
BIGENEMY, | |
SMALLENEMY, | |
SMARTENEMY | |
} |
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 abstract class Enemy { | |
private EnemyType enemyType; | |
public Enemy(EnemyType enemyType) { | |
this.enemyType = enemyType; | |
} | |
// this is for subclasses to implement | |
protected abstract void construct(); |
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 SmartEnemy extends Enemy { | |
public SmartEnemy() { | |
super(EnemyType.SMARTENEMY); | |
construct(); | |
} | |
// you can do all kinds of stuff here i'm | |
// just keeping it simple and only printing a message to the console |
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 SmallEnemy extends Enemy { | |
public SmallEnemy() { | |
super(EnemyType.SMALLENEMY); | |
construct(); | |
} | |
@Override | |
protected void construct() { |
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 EnemyFactory { | |
public static Enemy create(EnemyType enemyType) { | |
Enemy enemy = null; | |
switch (enemyType) { | |
case SMALLENEMY: |