Factory design pattern

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

[Wikipedia]

Intro

In this example we want to create a villain factory in order to generate villains in encapsulated way. The factory gets the name of the villain , and returns the new villain that we mentioned. Those factories will generate Heros and Villains. 

Villain


public interface Villain {
    void specialAbility();
    void useAbility();
}

Magician


    public class Magician implements Villain {

    @Override
    public void specialAbility() {
        System.out.println("I can use magic!");
    }

    @Override
    public void useAbility() {
        System.out.println("taste the magic!");
    }
}

Sniper


    public class Sniper implements Villain {

    @Override
    public void specialAbility() {
        System.out.println("I can use my sniper rifle!");
    }

    @Override
    public void useAbility() {
        System.out.println("sniper rifle shot!");
    }
}
    

Warrior


    public class Warrior implements Villain {

    @Override
    public void specialAbility() {
        System.out.println("I can use my special sword!");
    }

    @Override
    public void useAbility() {
        System.out.println("feel my sword power!");

    }
}

VillainFactory


   public class VillainFactory {

    //this is our factory , it's only job is to create a villain by encapsulating villain creation
    public Villain getVillain(String villainType){
        switch (villainType){
            case "warrior":
                return new Warrior();   //creates new warrior
            case "sniper":
                return new Sniper();    //creates new sniper
            case "magician":
                return new Magician();  //creates new magician
            default:
                return null;
        }
    }
}

Main


public class Main {
    /*
    We want to create a villain factory in order to generate villains in encapsulated way. 
    The factory gets the name of the villain , and returns the new villain that we mentioned.
    those factories will generate heros and villains
    */
    public static void main(String[] args) {
        VillainFactory villainFactory = new VillainFactory();

        //create a Warrior with villainFactory
        Villain villain1 = villainFactory.getVillain("warrior");
        System.out.println("Warrior was created with villainFactory!");

        //call specialAbility method
        villain1.specialAbility();
        villain1.useAbility();
        System.out.println();

        //create a Sniper with villainFactory
        Villain villain2 = villainFactory.getVillain("sniper");
        System.out.println("Sniper was created with villainFactory!");

        //call specialAbility method
        villain2.specialAbility();
        villain2.useAbility();
        System.out.println();

        //create a Magician with villainFactory
        Villain villain3 = villainFactory.getVillain("magician");
        System.out.println("Magician was created with villainFactory!");


        //call specialAbility method
        villain3.specialAbility();
        villain3.useAbility();
    }
}

Runing Example

   
Warrior was created with villainFactory!
I can use my special sword!
feel my sword power!

Sniper was created with villainFactory!
I can use my sniper rifle!
sniper rifle shot!

Magician was created with villainFactory!
I can use magic!
taste the magic!