如何在Java中实现方法链? [英] How to achieve method chaining in Java?

查看:34
本文介绍了如何在Java中实现方法链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Java 中实现方法链.

I want to achieve method chaining in Java.

我怎样才能实现它?

还请告诉我何时使用它.

Also let me know when to use it.

public class Dialog {

     public Dialog() {

     }

     public void setTitle(String title) {

         //Logic to set title in dialog
     }

     public void setMessage(String message) {

         //Logic to set message
     }     

     public void setPositiveButton() {

         //Logic to send button
     }
}   

我想创建我可以使用的方法链,如下所示:

I want to create method chaining that I can use as follows:

new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();

或者喜欢

new Dialog().setTitle("Title1").setMessage("sample message");

或者喜欢

new Dialog().setTitle("Title1").setPositiveButton();

推荐答案

让你的方法返回 this 像:

Have your methods return this like:

public Dialog setMessage(String message)
{
    //logic to set message
    return this;
}

这样,在每次调用其中一个方法后,您将得到相同的对象返回,以便您可以调用另一个方法.

This way, after each call to one of the methods, you'll get the same object returned so that you can call another method on.

当您想在一个对象上调用一系列方法时,这种技术很有用:它减少了实现这一目标所需的代码量,并允许您在方法链之后有一个返回值.

This technique is useful when you want to call a series of methods on an object: it reduces the amount of code required to achieve that and allows you to have a single returned value after the chain of methods.

减少显示对话框所需代码量的示例是:

An example of reducing the amount of code required to show a dialog would be:

// Your Dialog has a method show() 
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();

使用单个返回值的示例是:

An example of using the single returned value would be:

// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));

使用丹尼斯在对您的问题的评论中提到的构建器模式的示例:

An example of using the Builder pattern that Dennis mentioned in the comment on your question:

new DialogBuilder().setMessage("some message").setTitle("some title").build().show();

构建器模式允许您在构建对象之前为类的新实例设置所有参数(考虑具有 final 字段的类或在构建后为其设置值的对象比在构造时设置它的成本更高).

The builder pattern allows you to set all parameters for a new instance of a class before the object is being built (consider classes that have final fields or objects for which setting a value after it's been built is more costly than setting it when it's constructed).

在上面的例子中:setMessage(String)setTitle(String) 属于 DialogBu​​ilder 类并返回相同的 DialogBu​​ilder 他们被调用;build() 方法属于 DialogBu​​ilder 类,但返回 show() 方法所属的 Dialog 对象到 Dialog 类.

In the example above: setMessage(String), setTitle(String) belong to the DialogBuilder class and return the same instance of DialogBuilder that they're called upon; the build() method belongs to the DialogBuilder class, but returns a Dialog object the show() method belongs to the Dialog class.

额外

这可能与您的问题无关,但可能会对您和其他遇到此问题的人有所帮助.

这适用于大多数用例:所有不涉及继承的用例和一些涉及继承的特殊情况,当派生类没有添加您想要链接在一起的新方法您对使用(不强制转换)方法链的结果作为派生对象不感兴趣.

This works well for most use cases: all use cases that don't involve inheritance and some particular cases involving inheritance when the derived class doesn't add new methods that you want to chain together and you're not interested in using (without casting) the result of the chain of methods as an object of the derived.

如果您想对基类中没有方法的派生类的对象进行方法链,或者您希望方法链将对象作为派生类的引用返回,您可以看看在这个问题的答案中.

If you want to have method chaining for objects of derived classes that don't have a method in their base class or you want the chain of methods to return the object as a reference of the derived class, you can have a look at the answers for this question.

这篇关于如何在Java中实现方法链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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