Abstract Factory



This is the abstract factory class.
-----------------------------------------------------
public abstract class AbstractFactory {

    public static final int kROWDY = 1;
    public static final int kCEREBRAL = 2;

    private static final ElectricFactory electricFactory = new ElectricFactory();
    private static final AcusticFactory acusticFactory = new AcusticFactory();
    

    static final AbstractFactory getFactory(int crowdType) 
        throws IllegalArgumentException{
        
        switch(crowdType){
            
        case kROWDY:
            return electricFactory;
            
        case  kCEREBRAL:
            return  acusticFactory;
            
        default:
            // do nothing?
        }
        throw new IllegalArgumentException("error");
    }
    
    public abstract Guitar createGuitar();
}
-----------------------------------------------------



This is the superclass for guitar extensions. 
-----------------------------------------------------
public abstract class Guitar{
    public abstract String getType();
}
-----------------------------------------------------



Concrete class
-----------------------------------------------------
class ElectricGuitar extends Guitar{
    public String getType(){return "electic";}
}
-----------------------------------------------------



Concrete class.
-----------------------------------------------------
class AcusticGuitar extends Guitar{
    public String getType(){return "acustic";}
}

-----------------------------------------------------



Concrete factory class.
-----------------------------------------------------
class ElectricFactory extends AbstractFactory{

    public Guitar createGuitar(){
        return new ElectricGuitar();
    }

}
-----------------------------------------------------



Concrete factory class.
-----------------------------------------------------
class AcusticFactory  extends AbstractFactory{

    public Guitar createGuitar(){
        return new AcusticGuitar();
    }
}
-----------------------------------------------------



Test driver to show it all work.
-----------------------------------------------------
class Test{

    public static void main(String[] args){
        System.out.println("class: Test, method: main");

        AbstractFactory af;
        af = AbstractFactory.getFactory(AbstractFactory.kROWDY);
        
        Guitar g = af.createGuitar();
        System.out.println("guitar is of type: " +g.getType());

        af =  AbstractFactory.getFactory(AbstractFactory.kCEREBRAL);
        g = af.createGuitar();
        System.out.println("guitar is of type: " +g.getType());

    }
}

-----------------------------------------------------


Here is a tar with all the above classes in it.
abstractFactory.tar