Skip to content

Instantly share code, notes, and snippets.

View chermehdi's full-sized avatar
💭
My opinions are my own.

Mehdi Cheracher chermehdi

💭
My opinions are my own.
View GitHub Profile
@chermehdi
chermehdi / MyImmutableObject.java
Created November 7, 2016 14:09
an Example for a Simple Immutable Object
public final class MyImmutableObject {
/**
* Both String and Integer Classes Are Immutable
*/
private final String ObjectName;
private final Integer ObjectId;
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
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
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){
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
public enum EnemyType {
BIGENEMY,
SMALLENEMY,
SMARTENEMY
}
public abstract class Enemy {
private EnemyType enemyType;
public Enemy(EnemyType enemyType) {
this.enemyType = enemyType;
}
// this is for subclasses to implement
protected abstract void construct();
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
public class SmallEnemy extends Enemy {
public SmallEnemy() {
super(EnemyType.SMALLENEMY);
construct();
}
@Override
protected void construct() {
public class EnemyFactory {
public static Enemy create(EnemyType enemyType) {
Enemy enemy = null;
switch (enemyType) {
case SMALLENEMY: