Java多类合成和锅炉板减量 [英] Java multiple class compositing and boiler plate reduction

查看:232
本文介绍了Java多类合成和锅炉板减量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道Java为什么不应该有多个继承。所以这并不是质疑牛奶回家之前已经讨论了什么。



这将讨论当我们希望创建一个具有两个或更多其他类的特征。



可能,我们大多数人会这样做,从三个类继承。为了简单起见,我省略了构造函数:

  class Car 
extends Vehicle
{
最终公共交通运输;
最终公开机器机;
}

所以,Car类直接继承了Vehicle类的方法和对象,但是必须明确地引用运输和机器来引用运输和机器中实例化的对象。

  Car car = new Car(); 
car.drive(); // from Vehicle
car.transport.isAmphibious(); // from Transport
car.machine.getCO2Footprint(); // from Machine

我以为这是一个好主意,直​​到遇到需要setter和getter的框架方法。例如,XML

 < Car amphibious ='false'footPrint ='1000'model ='Fordstatic999'/> 

将寻找方法
setAmphibious(..),setFootPrint(..)和则setModel(..)。因此,我必须从运输和机械类项目中计划方法

  class Car 
extends Vehicle
{
最终公共交通运输;
最终公开机器机;
public void setAmphibious(boolean b){
this.transport.setAmphibious(b);
}
public void setFootPrint(String fp){
this.machine.setFootPrint(fp);
}
}

如果只有一些特点。现在,我正在尝试将所有的SmartGWT适配到GWT UIBinder中,特别是那些不是GWT小部件的类。有很多项目的特点。



如果存在某种形式的注释框架,如下所示不会很好:

  class Car 
extends Vehicle
@projects {Transport @projects {Machine @projects Guzzler}}
{
/ *无需明确实例化运输,机器或Guzzler * /
....
}

如果存在通用特征名称,机器的特征将优先于Guzzler,而运输公司将优先于机器,而车辆将优先于运输公司。注释框架然后将Transport,Machine和Guzzler作为Car的隐藏成员进行实例化,并以@project注释序列的优先权扩展到受保护/公共特征的实际源代码或字节码中。优选地进入字节码。所以在Machine和Guzzler中都可以找到setFootPrint方法,只有机器可以预计。



问题:


  1. 你不觉得拥有这样的框架是个好主意吗?

  2. 这样的框架是否已经存在?告诉我在哪里/什么。

  3. 是否有一个eclipse插件?

  4. 有没有一个提案或计划,你知道这样一个注释框架?

如果注释/插件框架允许我指定布尔值,int或其他任何需要的值要从String转换,并为我转换/解析。



请指教,有人。我希望我的问题的措词足够清楚。 Thx。



编辑:
为了避免OO爱好者跳到结论,我已经重命名了这个问题的标题。

解决方案

也许并不完全符合您的要求,但可以看一下Mixins:




We all know why Java does/should not have multiple inheritance. So this is not questioning about what has already been debated till-cows-come-home.

This discusses what we would do when we wish to create a class that has the characteristics of two or more other classes.

Probably, most of us would do this to "inherit" from three classes. For simplicity, I left out the constructor.:

class Car
extends Vehicle
{
  final public Transport transport;
  final public Machine machine;
}

So that, Car class directly inherits methods and objects of Vehicle class, but would have to refer to transport and machine explicitly to refer to objects instantiated in Transport and Machine.

Car car = new Car();
car.drive(); // from Vehicle
car.transport.isAmphibious(); // from Transport
car.machine.getCO2Footprint(); // from Machine

I thought this was a good idea until when I encounter frameworks that require setter and getter methods. For example, the XML

<Car amphibious='false' footPrint='1000' model='Fordstatic999'/>

would look for the methods setAmphibious(..), setFootPrint(..) and setModel(..). Therefore, I have to project the methods from Transport and Machine classes

class Car
extends Vehicle
{
  final public Transport transport;
  final public Machine machine;
  public void setAmphibious(boolean b){
    this.transport.setAmphibious(b);
  }
  public void setFootPrint(String fp){
    this.machine.setFootPrint(fp);
  }
}

This is OK, if there were just a few characteristics. Right now, I am trying to adapt all of SmartGWT into GWT UIBinder, especially those classes that are not a GWT widget. There are lots of characteristics to project.

Wouldn't it be nice if there exists some form of annotation framework that is like this:

class Car
extends Vehicle
@projects {Transport @projects{Machine @projects Guzzler}}
{
  /* No need to explicitly instantiate Transport, Machine or Guzzler */
  ....
}

Where, in case of common names of characteristics exist, the characteristics of Machine would take precedence Guzzler's, and Transport's would have precedence over Machine's, and Vehicle's would have precedence over Transport's. The annotation framework would then instantiate Transport, Machine and Guzzler as hidden members of Car and expand to break-out the protected/public characteristics, in the precedence dictated by the @project annotation sequence, into actual source code or into byte-code. Preferably into byte-code. So that the setFootPrint method is found in both Machine and Guzzler, only that of Machine's would be projected.

Questions:

  1. Don't you think this is a good idea to have such a framework?
  2. Does such a framework already exist? Tell me where/what.
  3. Is there an eclipse plugin that does it?
  4. Is there a proposal or plan anywhere that you know about such an annotation framework?

It would be wonderful too, if the annotation/plugin framework lets me specify that boolean, int, or whatever else needs to be converted from String and does the conversion/parsing for me too.

Please advise, somebody. I hope wording of my question was clear enough. Thx.

Edited: To avoid OO enthusiasts jumping to conclusion, I have renamed the title of this question.

解决方案

Perhaps not exactly what you are looking for, but you can take a look at Mixins:

这篇关于Java多类合成和锅炉板减量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆