Reputation: 53916
I'm using a variant of the abstract factory pattern as defined in http://en.wikipedia.org/wiki/Abstract_factory
The pattern is coded on wikipedia like so -
/* GUIFactory example -- */
interface GUIFactory {
public Button createButton();
}
class WinFactory implements GUIFactory {
public WinButton createButton() {
return new WinButton();
}
}
class OSXFactory implements GUIFactory {
public OSXButton createButton() {
return new OSXButton();
}
}
interface Button {
public void paint();
}
class WinButton implements Button {
public void paint() {
System.out.println("I'm a WinButton");
}
}
class OSXButton implements Button {
public void paint() {
System.out.println("I'm an OSXButton");
}
}
class Application {
public Application(GUIFactory factory) {
Button button = factory.createButton();
button.paint();
}
}
public class ApplicationRunner {
public static void main(String[] args) {
new Application(createOsSpecificFactory());
}
public static GUIFactory createOsSpecificFactory() {
int sys = readFromConfigFile("OS_TYPE");
if (sys == 0) {
return new WinFactory();
} else {
return new OSXFactory();
}
}
}
I'm using it slightly differently in that I dont't need a further layer of abstraction to override paint behaviour in Button implementations.
My Code is -
//Implmentation code
MainMenuButtonFactory mainMenuButtonFactory = new SyncButtonFactory();
BitmapButtonField refreshButton = mainMenuButtonFactory.createButton();
public interface MainMenuButtonFactory {
BitmapButtonField createButton();
}
public class SyncButtonFactory implements MainMenuButtonFactory{
public BitmapButtonField createButton() {
return new BitmapButtonField(ButtonImageFactory.getResyncBitmap(),
ButtonImageFactory.getResyncBitmapHighlight()){
public boolean navigationClick(int status, int time)
{
FutureContent.future.clearContent();
ScreenController.displayNextScreen(new UpdatingScreenAnimation(Constants.MAIN_MENU_SCREEN));
return super.navigationClick(status, time);
}
};
}
}
public class JudgeButtonFactory implements MainMenuButtonFactory{
public BitmapButtonField createButton() {
BitmapButtonField judgeButton = new BitmapButtonField(ButtonImageFactory.getJudgeBitmap(),
ButtonImageFactory.getJudgeBitmapHighlight()){
public boolean navigationClick(int status, int time)
{
RatingsScreenController ratingsScreenController = new RatingsScreenController("MainMenuScreen");
Thread t = new Thread(ratingsScreenController);
t.start();
return super.navigationClick(status, time);
}
};
return judgeButton;
}
}
Is this still a valid abstract factory design pattern ?
Upvotes: 0
Views: 284
Reputation: 2672
Perhaps you just haven't shown all the code to concentrate on the paint
question you pose, but I'd expect to see other XXXButtonFactory implements MainMenuButtonFactory
classes too. And these would create variations of BitmapButtonField
's parent class/interface.
If you don't have any others, or all your MainMenuButtonFactory
derived classes just create BitmapButtonField
classes, with no variation then the Abstract Factory does not seem like the appropriate pattern. Compare this to the Facory variations that then create WinButton
/OSXButton
in the example.
In this sense, the lack of a paint
method in itself is not an issue ... but I would have expected to see evidence of some variation in the created classes. If not paint
, then what variation are you modelling that justifies the use of the Abstract Factory?
Upvotes: 1
Reputation: 5211
Yes, as long as you have an interface to create a kind of objects and provide one implementation, you are implementing an abstract factory.
But consider:
Upvotes: 2