leogtzr
5/11/2017 - 5:28 AM

factory_with_reflection.java

// @leonidasgtzr
interface TV {
    void show();
}

class ColorTV implements TV {
    @Override
    public void show() {
        System.out.println("Showing content with Color ... ");
    }
}

class LedTV implements TV {
    @Override
    public void show() {
        System.out.println("Showing content with Leds ...");
    }
}

enum TVFactory_2 {
    COLOR(ColorTV.class),
    LED(LedTV.class)
    ;
    
    private final Class<? extends TV> tv;
    
    TVFactory_2(final Class<? extends TV> tv) {
        this.tv = tv;
    }
    
    public TV create() {
        TV theTv = null;
        try {
            theTv = tv.newInstance();
        } catch (final Exception ex) {}
        return theTv;
    }
    
}