如何在java中实现方法链接? [英] how to achieve method chaining in java?

查看:136
本文介绍了如何在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();


推荐答案

让你的方法返回这个喜欢:

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

使用Dennis在您的问题评论中提到的Builder模式的示例:

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()方法属于对话框类。

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.

额外

这可能与您的问题无关,但它可能对您和其他人有帮助遇到这个问题。

这适用于大多数用例:所有不涉及继承的用例和一些涉及继承的特殊情况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天全站免登陆