Is there a Java equivalent for a pointer to a C ++ function?

Possible duplicate:
What is the closest function pointer replacement in Java?

I am writing a factory class to create a bunch of different widgets. For argument, suppose this factory can create 1000 widgets.

myWidget createWidget (widgetId)
{
    case 0:  createwidget0 ();
     .
     .
    case 1000: createwidget1000 ();
}

I do not want to write an expression of 1000 cases. I want to put all the creation procedures in an array. Use widgetId as an index to directly execute the creation procedure; it does not need to pass a comparison of 1000 conditions. So, the whole createWidget procedure can be simplified, for example,

myWidget createWidget (widgetId)
{
    myAwsomeFuncArr    createFunc;

    myWidget widget = createFunc[widgetId] ();
}

Is there any way to do this in Java?

+3
source share
4 answers

Deploy widget factories and store them in your large array. That would be pretty close:

public interface WidgetFactory {
  public Widget create();
}

- :

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();
  }
}

. () .

+8

Java . , , !

public abstract class Widget {
   protected boolean loaded;

   public Widget() {
      loaded = false;
   }

}

public class ConcreteWidgetA extends Widget {

   public ConcreteWidgetA() {
      super();
   }

   public void doSomething() {
      if (!loaded) {
         load();
         loaded = true;
      }
   }

   private void load() {

   }

}

public class ConcreteWidgetB extends Widget {

   public ConcreteWidgetB() {
      super();
   }

   public void doSomethingElse() {
      if (!loaded) {
         load();
         load = true;
      }
   }

   private void load() {

   }

}

public enum Widgets {

 CONCRETE_WIDGET_A(new ConcreteWidgetA()),
 CONCRETE_WIDGET_B(new ConcreteWidgetB());

 private Widget widget;

 public Widgets(Widget aWidget) {
  widget = aWidget;
 }

 public Widget getWidget() {
  return widget;
 }

}

public class WidgetFactory {
    //Here the money maker.
    public static Widget createWidget(Widgets aWidgetElement) {
     return aWidgetElement.getWidget();
    }
}
+7

Java .

+5

functor:

public class Functor {
interface func{
int fun(int x,int y);
String toString();
}
public static void main(String[] args) {
    func add=new func(){
        public int fun(int x, int y) {
            return x+y;
        }
        public String toString() {
            return "+";
        }
    };  
    func mult=new func(){
        public int fun(int x, int y) {  
            return x*y;
        }
        public String toString() {
            return "*";
        }
    };  
    func[] arr={add,mult};
    int i[]={10,20,30,40};
    for(int val:i)
      for(func f:arr)
        System.out.println(val+""+f+val+"="+f.fun(val,val));    

}
}

. - 1000 , 1000 . , . .

Andreas , .

+1
source

Source: https://habr.com/ru/post/1762952/


All Articles