dimanche 22 janvier 2017

AbstractFactory design pattern

I have created a following example . does this follows AbstractFactory design pattern ?

ButtonType.java

public enum ButtonType {
    WIN, LINUX, MAC
}

Button.java

public interface Button {
    ButtonType getButtonType();

    void actionListener();
}

LinuxButton.java

public class LinuxButton implements Button {

    @Override
    public ButtonType getButtonType() {
        return ButtonType.LINUX;
    }

    @Override
    public void actionListener() {
        System.out.println("Linux Button created");

    }

}

WinButton.java

public class WinButton implements Button {

    @Override
    public ButtonType getButtonType() {
        return ButtonType.WIN;
    }

    @Override
    public void actionListener() {
        System.out.println("Window Button Created");

    }

}

MacButton.java

public class MacButton implements Button {

    @Override
    public ButtonType getButtonType() {
        return ButtonType.MAC;
    }

    @Override
    public void actionListener() {
        System.out.println("Mac Button Created");

    }

}

ButtonFactory.java

public interface ButtonFactory {

    ButtonFactory getButtonFactory();
    Button createButton();
}

LinuxButtonFactory.java

public class LinuxButtonFactory implements ButtonFactory {

    @Override
    public ButtonFactory getButtonFactory() {
        return this;
    }

    @Override
    public Button createButton() {
        System.out.println("creating Linux Button");
        return new LinuxButton();
    }

}

WinButtonFactory.java

public class WinButtonFactory implements ButtonFactory {

    @Override
    public ButtonFactory getButtonFactory() {
        return this;
    }

    @Override
    public Button createButton() {
        System.out.println("creating window Button");
        return new WinButton();
    }

}

MacButtonFactory.java

public class MacButtonFactory implements ButtonFactory {

    @Override
    public ButtonFactory getButtonFactory() {
        return this;
    }

    @Override
    public Button createButton() {
        System.out.println("Creating MAC Button");
        return new MacButton();
    }

}

AbstractButtonFactory.java

public abstract class AbstractButtonFactory {

    public static ButtonFactory getButtonFactory(ButtonType bt) {
        ButtonFactory btnFactory = null;
        switch (bt) {
        case WIN:
            btnFactory = new WinButtonFactory();
            break;
        case LINUX:
            btnFactory = new LinuxButtonFactory();
            break;
        default:
            btnFactory = new MacButtonFactory();
        }
        return btnFactory;
    }

}

and finally the main calss

public class AbstractFactoryObjectDemo {

    public static void main(String[] args) {

        ButtonFactory factory = AbstractButtonFactory.getButtonFactory(ButtonType.LINUX);
        Button linuxButton = factory.createButton();
        System.out.println(linuxButton.getButtonType().toString());
    }

}

I want to make sure is this pattern follow AbstractFactory Design Pattern ?

Aucun commentaire:

Enregistrer un commentaire