简体   繁体   English

Java中的动态绑定

[英]Dynamic Binding in Java

I am used to doing dynamic Binding in PHP this way 我习惯用这种方式在PHP中进行动态绑定

function ($className)
{
 $obj = new $className();
 .
 .
 .
}

here $className is a String parameter and based on name of the class an object of that class is created. 这里$ className是一个String参数,并基于类的名称创建该类的对象。 So when I need to add more functionality I just add another class and everything get sorted out automatically. 因此,当我需要添加更多功能时,只需添加另一个类,所有内容都会自动整理。

Now I want to do something similar in Java. 现在,我想用Java做类似的事情。 Right now my code is 现在我的代码是

public EditPart createEditPart(EditPart context, Object model) {

AbstractGraphicalEditPart part = null;

if(model instanceof Entreprise) {
part = new EntreprisePart();
else if(model instanceof Employee)
part = new EmployeePart();
.
.
.

} }

My aim is that if I add a new Class to the package, I shouldn't have the need to modify my createEditPart function. 我的目标是,如果将新类添加到程序包中,则无需修改createEditPart函数。 How is it done in Java. 用Java如何完成。

一种选择是创建一个工厂 ,每个类都可以向其注册,然后可以调用该工厂来创建适当的对象。

One way to handle this, or at least to do the same thing you're talking about in the PHP, is reflection. 反射是处理此问题的一种方法,或者至少可以做与您在PHP中谈论的相同的事情。 You can obtain a Class object from a string specifying the fully-qualified class name, and use that class to create a new instance of itself dynamically. 您可以从指定完全合格的类名称的字符串中获取Class对象,然后使用该类动态创建其自身的新实例。

public EditPart createEditPart(String name) throws [lots of stuff] {
    return Class.forName(name).asSubclass(EditPart.class).newInstance();
}

However, this is a relatively inefficient way to use Java, and I wouldn't necessarily recommend it. 但是,这是使用Java的相对低效的方式,我不一定会推荐它。 Some other languages use reflection quite liberally (like PHP), but not so much with Java. 其他一些语言相当自由地使用反射(例如PHP),而Java则很少。 Amber's factory idea is a good one and if I were doing it, I might go with that. Amber的工厂构想是一个好主意,如果我这样做的话,我可能会同意。

If you want to create your EditPart based on the class of the model object (as in your code sample), rather than on the name of the EditPart subclass, then you can't use reflection. 如果你想创建EditPart基于类的的model对象(如您的代码示例中),而不是在名称EditPart子类,那么你可以不使用反射。 Well, you can, but you'll still need some way to tell the program which EditPart corresponds to which type of model. 可以,但是您仍然需要一些方法来告诉程序哪个EditPart对应于哪种类型的模型。 One option would be to change the type of the model parameter from Object to some interface, say EditModel , which you would write to have a method that returns an instance of the appropriate part. 一种选择是将model参数的类型从Object更改为某个接口,例如EditModel ,您将编写该接口以具有返回适当零件实例的方法。 Like so: 像这样:

public interface EditModel {
    public EditPart getEditPart();
}

Then you could just write your createEditPart as 然后,您可以将createEditPart编写为

public EditPart createEditPart(EditModel model) {
    return model.getEditPart();
}

(actually, if it's that simple you probably don't even need createEditPart at all). (实际上,如果就这么简单,您甚至根本不需要createEditPart )。 If you don't want to or can't do that, you could set up something with reflection that chooses the part class dynamically based on the name of the model class, but that really sounds unnecessarily complicated. 如果您不希望或不能做到这一点,则可以使用反射进行设置,以根据模型类的名称动态选择零件类,但这听起来确实不必要。 At that point you'd definitely be better off going with the factory pattern. 到那时,您绝对最好采用工厂模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM