Tuesday, August 12, 2014

Programming Design Pattern - Strategy Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the strategy design pattern in action. The strategy design pattern is a very useful programming design pattern while dealing with mutable behaviors.

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 Strategy 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.


Defining the strategy

That's the part a like the most. Designing, taking decissions, doing analysis and design like you've learned in the past, but applying now in the practise. At this point, you should separate the 'inmutable' from the 'mutable' behaviors. In our case, this is the weapon behavior, that can vary acc. to the game stage we are. The imutable is the warrior which just references to the strategy.

// 1. DEFINE THE STRATEGY
public interface WeaponBehavior {
    void fire();
}
// 2. DEFINE THE WARRIOR AND USE
//    THE MUTABLE BEHAVIORS
public abstract class Warrior {
    // USE MUTABLE WEAPON BEHAVIORS
    protected WeaponBehavior weapon;
    
    public Warrior() {
        super();
        this.weapon = new DefaultWeapon();
    }
    public void setWeapon(WeaponBehavior weapon){
        this.weapon=weapon;
    }
    public void fireWeapon(){
        this.weapon.fire();
    }
    public abstract void fight();
}

The Warrior

The warrior can set weapons and fires it acc. to the used strategy. That's a very simple implementation.

// 3. CREATE THE CONCRETE WARRIOR
public class MegaMan extends Warrior {
    @Override
    public void fight() {
        fireWeapon();
        // ... outros metodos de escudo, briga etc. aqui...
    }
}

Weapons

That's the mutable behavior. The interesting part of it. Let's implement some weapon strategies.

// 4. IMPLEMENT THE WEAPON BEHAVIORS
public class Bee implements WeaponBehavior {
    @Override
    public void fire() {
        System.out.println("BEE ATACK!");
    }
}
public class Scissors implements WeaponBehavior {
    @Override
    public void fire() {
        System.out.println("SCISSORS ATACK!");
    }
}
public class DefaultWeapon implements WeaponBehavior {
    @Override
    public void fire() {
        System.out.println("DEFAULT ATTACK");
    }
}

Testing it

Our client tests it. Pay attention that we do not change the inmutable warrior, only its weapon strategy.

public class Client {
    public static void main(String[] args) {
        MegaMan megaMan = new MegaMan();
        megaMan.fight();
        // USING STRATEGIES
        // FIRST WEAPON OF TYPE SCISSORS
        megaMan.setWeapon(new Scissors());
        megaMan.fight();
        // CHANGING STRATEGIES
        // SECOND WEAPON OF THE TIPE BEE
        megaMan.setWeapon(new Bee());
        megaMan.fight();
    }
}

That's all! I hope you like it!

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†