gpt4 book ai didi

Java:通过关联覆盖行为

转载 作者:行者123 更新时间:2023-12-01 04:58:47 26 4
gpt4 key购买 nike

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

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

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

目前,我已经实现了,但它并不干燥:我应该与 getController 保持 Controller 类同步。 这是一种可能的方法,可能会避免问题(如果好奇,请参阅问题的第一个版本)。

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

您在这里有什么建议?

最佳答案

在我看来,通过传递接口(interface)作为参数来获取类的实例有点人为,除非您用一些位码编织来填充该方法,但是......

使用一些约定优于配置,您可以实现如下所示的内容:

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

}

关于Java:通过关联覆盖行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com