Reputation: 3350
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 {
Button createButton();
}
LinuxButtonFactory.java
public class LinuxButtonFactory implements ButtonFactory {
@Override
public Button createButton() {
System.out.println("creating Linux Button");
return new LinuxButton();
}
}
WinButtonFactory.java
public class WinButtonFactory implements ButtonFactory {
@Override
public Button createButton() {
System.out.println("creating window Button");
return new WinButton();
}
}
MacButtonFactory.java
public class MacButtonFactory implements ButtonFactory {
@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 ,if it doesn't what changes should be made?
Upvotes: 0
Views: 213
Reputation: 954
Maybe you can consider combination between Bridge and Abstract Factory.
Imagine that you have a need to add new graphical item in addition to Button let say ComboBox, or that you have additional platform in addition to Win/Linux/Mac.
Then pure Abstract Factory will not be flexible, therefore Bridge pattern can be used in order to separate abstraction(Button, ComboBox, ...) and platform specific implementation(Win/Linux/Mac).
Upvotes: 0
Reputation: 114837
The implementation is ok. You have abstracted the button and the factory and that's the main goal for the pattern.
On an implementation side note: the getButtonFactory
method on the factory is pretty useless, you can simply remove it an all the implementations from the real factories.
When a user calls the static method on the abstract factory, he already gets an instance of a factory and doesn't have to call a method on this factory which simply would return itself again. So drop this method :)
Upvotes: 1
Reputation: 57421
You should split the interface
public interface ButtonFactory {
ButtonFactory getButtonFactory(ButtonType bt);
Button createButton();
}
Into 2 different ones
public interface ButtonFactory {
ButtonCreator getButtonFactory(ButtonType bt);
}
and
public Button ButtonCreator {
Button createButton();
}
First one creates ButtonCreator (depending on type) and ButtonCreator keeps the type inside and creates button of the type
:Updated
Upvotes: 1