Skip to main content
edited body
Source Link
Gabe
  • 87.1k
  • 13
  • 144
  • 239

Implement widget factories and store them in your big array. That would be pretty close:

public interface WidgetFactory { public Widget create(); } 

and somewhere else:

public class MyClass { private static Widgetfactory[] widgetFactories = new WidgetFactory[1000]; static { widgetFactories[0] = new FancyButtonFactory(); // FancyButtonFactory implements WidgetFactory widgetFactories[0]widgetFactories[1] = new FancyTextFieldFactory(); // see above // ... } static public Widget createWidget(int index) { return widgetFactories[index].create(); } } 

This piece of code is only written to show a similiar approach without function pointers. Real life applications will use a (much) better design.

Implement widget factories and store them in your big array. That would be pretty close:

public interface WidgetFactory { public Widget create(); } 

and somewhere else:

public class MyClass { private static Widgetfactory[] widgetFactories = new WidgetFactory[1000]; static { widgetFactories[0] = new FancyButtonFactory(); // FancyButtonFactory implements WidgetFactory widgetFactories[0] = new FancyTextFieldFactory(); // see above // ... } static public Widget createWidget(int index) { return widgetFactories[index].create(); } } 

This piece of code is only written to show a similiar approach without function pointers. Real life applications will use a (much) better design.

Implement widget factories and store them in your big array. That would be pretty close:

public interface WidgetFactory { public Widget create(); } 

and somewhere else:

public class MyClass { private static Widgetfactory[] widgetFactories = new WidgetFactory[1000]; static { widgetFactories[0] = new FancyButtonFactory(); // FancyButtonFactory implements WidgetFactory widgetFactories[1] = new FancyTextFieldFactory(); // see above // ... } static public Widget createWidget(int index) { return widgetFactories[index].create(); } } 

This piece of code is only written to show a similiar approach without function pointers. Real life applications will use a (much) better design.

Source Link
Andreas Dolk
  • 114.9k
  • 20
  • 185
  • 275

Implement widget factories and store them in your big array. That would be pretty close:

public interface WidgetFactory { public Widget create(); } 

and somewhere else:

public class MyClass { private static Widgetfactory[] widgetFactories = new WidgetFactory[1000]; static { widgetFactories[0] = new FancyButtonFactory(); // FancyButtonFactory implements WidgetFactory widgetFactories[0] = new FancyTextFieldFactory(); // see above // ... } static public Widget createWidget(int index) { return widgetFactories[index].create(); } } 

This piece of code is only written to show a similiar approach without function pointers. Real life applications will use a (much) better design.