Follow treslines by email clicking Here!

Wednesday, August 20, 2014

Programming Design Pattern - Prototype Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the prototype design pattern in action. The prototype design pattern is a very useful programming design pattern while dealing with games and complex objects.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML Diagram of the Prototype Pattern


Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.


The example

In our example we will see, how we could use the prototype pattern while dealing with games. Assuming that the instances used there are very complex to create or require a lot of time. (performance issue)

The base cloneable

That's the core of it. Note that this class extends from cloneable. That's typical for prototype.

public abstract class Caracteristica implements Cloneable {
    protected String force,power,webStorage,life,outfit,weakPoint;
    protected String velocity,skill,specialPower,combo;
    public Caracteristica(String force, String power, String webStorage, String life,
            String outfit, String combo, String weakPoint, String velocity, String skill,
            String specialPower) {
        this.force = force;this.power = power;this.webStorage = webStorage;
        this.life = life;this.outfit = outfit;this.combo = combo;
        this.weakPoint = weakPoint;this.velocity = velocity;
        this.skill = skill;this.specialPower = specialPower;
    }
    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}

Our Heros

All heros we implement, must extend from the base class.

public class Spiderman extends Caracteristica {
    
    public Spiderman(String force, String power, String webStorage, String life, String outfit,
            String combo, String weakPoint, String velocity, String skill, String specialPower) {
        super(force, power, webStorage, life, outfit, combo, weakPoint, velocity, skill, specialPower);
    }
    public void fire(){
        System.out.println("fire sticky web");
    }
    public void showSkills(){
        System.out.println("Skill: "+ skill);
    }
    // MANY OTHER COMPLEX ACCESSES AND METHODS HERE ...
}

Types of heros available

Here we define which types of heros we will support in this fictitious game.

public enum Hero {
    BATMAN, SPIDERMAN, SUPERMAN, CAPITAN_AMARICA;
}

The Game

That's the game itself. Note that whenever we need a "complex" object of hero, we do not create it from scratch. We just clone it from the register.

public class Game {
    private static final Game INSTANCE = new Game();
    private Game() {/* SINGLETON */}
    public static Game instance() {return INSTANCE;}
    private static final Map < Hero, Caracteristica > register = new HashMap < Hero, Caracteristica > ( ) {
        private static final long serialVersionUID = 1L;
        {
            put(Hero.SPIDERMAN, new Spiderman("for├ža", 
                    "poder", "armazemDeTeia", "vida", "roupa",
                    "combo", "pontoFraco", "velocidade", 
                    "pode subir nas paredes", "poderEspecial"));
        }
    };
    public Caracteristica create(Hero hero){
       return (Caracteristica) register.get(hero).clone();
    }
}

Testing it

Finally, the usage of it.

public class Client {
    public static void main(String[] args) {
        final Spiderman spiderman = (Spiderman)Game.instance().create(Hero.SPIDERMAN);
        spiderman.fire();
        spiderman.showSkills();
    }
}

That's all! Hope you like it!

No comments:

Post a Comment