简体   繁体   中英

Code generation for composition using Eclipse

Effective Java , along with other sources suggest that we should consider using composition over inheritance . 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.

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)?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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