1

我有几个Field实体,并Controller以这种方式与它们关联:(Field,Generic_Controller <--> Specific_Controller). 我想在Java中实现每个Specific_Controller与相应Field的关联。Generic_Controller我怎样才能做到这一点?在编译时更好。

编辑:这是一个任务的伪代码(也许会更清楚):

given X:Class and C:ServiceFor[X]
  where C:ServiceFor[X] means:
    C has method M with args(x:X) returning Any

goal:
get C:ServiceFor[X]
  where C and X are given. // X is Field class or instance
that:
  if there exits explicitly defined (C1 as ServiceFor[X]) and (C1 is child of C)
    return C1 // ==Specific_Controller
  otherwise:
    return C  // ==Generic_Controller

目前,我有实现但它不是 DRY:我应该保持控制器类与getControllerbody 同步。 这是一种可能的方法,可能会导致问题消失(如果好奇,请参阅问题的第一个版本)。

import javax.swing.*;

interface Field 
{
  Object getValue();
  /** Obtain name of field for internal usage
   */
  String getName(); 
  void setValue<T>(T value) throws InvalidValueException;
  boolean isValid<T>(T value);
  Controller getController(Class<?> genericController);
}

interface Controller {}

interface UIController extends Controller {
  /** Return GUI widget responsible to manipulating value inside FormField 
   *  instance
   * (Control logic uses setValue() ) */
  JComponent getInputControl();
}

interface UrlEncoder extends Controller {
  /** Obtain value encoded for transmition through Web.
   *
   * some fields used privately in code are cut out inside this method
   */
  String getURLEncodedValue(); 
}

class CommonUIController implements UIController { ... }

//-- MyField with specific implementations of controllers
class MyField 
    implements Field, UIController, UrlEncoder 
{
  // here methods overriden
}

//-- usage (i do it every time)
MyField mf;
UIController uiController;
if(mf instanceof UIController)
  uiController = (UIController)mf;
} else { // use default impl.
  uiController = new CommonUIController(mf);
}

我希望保持Field,Controller并声明它们之间的关系彼此分开。就像 C++ 可以做的那样:

//-- common.hpp
template<class _Controller, class _Field>
struct overriden_controller:
    public _Controller
{
  typedef _Controller controller_type;
  typedef _Controller type;
  typedef _Field      field_type;
};

struct UrlEncoder{};
struct UIController {};


//-- MyField.hpp
// Field knows nothing about controllers
struct MyField {};

// !! Here comes association
template<>
struct overriden_controller<UIController, MyField>
{
   // Overriden behavior 
};

template<>
struct overriden_controller<UrlEncoder, MyField>
{
   // Overriden behavior 
};

//-- And then use associated value
auto controller = new field_controller<UIController, MyField>::type();

你可以在这里提出什么建议?

4

1 回答 1

0

在我看来,通过将接口作为参数传递来获取类的实例有点做作,除非你用一些比特代码编织来填充方法,但是......

使用一些约定而不是配置,您可以实现如下内容:

public class AbstractField {

    public Controller getController(Class clazz){
        String name = clazz.getSimpleName();

        String pack = clazz.getPackage().getName();
        String controllerName = pack+"."+getClass().getSimpleName()+name;

        Class<?> ctrl = null;
        try {
            ctrl = Class.forName(controllerName);
            return (Controller) ctrl.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
于 2012-12-01T02:37:00.313 回答