简体   繁体   English

使用Eclipse进行组合的代码生成

[英]Code generation for composition using Eclipse

Effective Java , along with other sources suggest that we should consider using composition over inheritance . 有效的Java以及其他来源表明我们应该考虑使用组合而不是继承 I have often found my self achieving such composition by using the Decorator pattern and implementing forwarding methods that delegate invocations to a wrapped object. 通常使用Decorator模式并实现将调用委托给包装对象的转发方法,我经常发现自己实现了这样的组合。

However, I often find myself writing many simple forwarding methods of the type: 但是,我经常发现自己编写了许多类型的简单转发方法:

public void myMethod(String name) {
    instance.myMethod(name);
}

Is there anyway of auto-generating these forwarding methods within Eclipse (3.4.x)? 无论如何在Eclipse(3.4.x)中自动生成这些转发方法?

In the Source menu choose "Generate Delegate Methods". 在“源”菜单中,选择“生成委托方法”。 It will give you a list of methods available on any of your fields that you can have the current object forward to. 它将为您提供可用于当前对象转发的任何字段的可用方法列表。

您可以编写一个自定义模板(“模板”上的“窗口”>“首选项”>“过滤器”),该模板具有方法名称和参数名称的占位符。

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

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