Java的多级合成和锅炉板减少 [英] Java multiple class compositing and boiler plate reduction

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

问题描述

我们都知道为什么Java那样/不应该有多重继承。所以这不是质疑什么已经讨论到,牛先得回家。

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

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

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'/>

将寻找方法
setAmphibious(..),setFootPrint(..)和则setModel(..)。因此,我不得不从项目运输的方法和机械类

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

这是好的,如果有只是几个特点。现在,我想所有的SmartGWT的改编成GWT UiBinder的,尤其是那些不属于GWT窗口小部件类。有很多特色项目的。

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 */
  ....
}

凡特性的通用名称的情况下存在,机器的特点,将采取precedence高油耗的,和交通运输的也不会对机器的precedence和车辆将有超过交通运输的precedence。然后,注释框架将实例化交通运输,机械和高油耗汽车作为隐藏成员和扩大在break-out保护/公共特性,由@project注释序列口授了precedence,进入实际的源$ C ​​$ C或成字节级code。 preferably成字节级code。使得setFootPrint方法在两个机和高油耗发现,只有机的将被投影

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.

问题:


  1. 请你不觉得这是有这样一个框架是一个好主意?

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

  3. 是否有一个eclipse插件,不是吗?

  4. 是否有建议或任何地方的计划,你知道这样的注解框架?

这将是太奇妙,如果注释/插件框架让我指定布尔,int或需要其他任何必须从字符串转换并执行转换/解析过我。

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.

请指教,有人。我希望我的问题的措辞不够清楚。 THX。

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

编辑:
为了避免OO爱好者跳跃到最后,我已改名为这个问题的标题。

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:

  • the wikipedia article
  • Qi4j framework
  • CGLIB's mixin support
  • this example

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

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