如何在Java中实现方法链接?
我想用Java实现方法链接 我怎样才能做到呢 也让我知道什么时候使用它如何在Java中实现方法链接?,java,methods,chaining,Java,Methods,Chaining,我想用Java实现方法链接 我怎样才能做到呢 也让我知道什么时候使用它 public class Dialog { public Dialog() { } public void setTitle(String title) { //Logic to set title in dialog } public void setMessage(String message) { //Logic to s
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
}
}
我想创建可以使用的方法链接,如下所示:
new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();
或者
new Dialog().setTitle("Title1").setMessage("sample message");
new Dialog().setTitle("Title1").setPositiveButton();
或者
new Dialog().setTitle("Title1").setMessage("sample message");
new Dialog().setTitle("Title1").setPositiveButton();
让您的方法返回类似以下内容的
this
:
public Dialog setMessage(String message)
{
//logic to set message
return this;
}
这样,在每次调用其中一个方法后,您将返回相同的对象,以便可以在上调用另一个方法
当您想要在对象上调用一系列方法时,这种技术非常有用:它减少了实现这一点所需的代码量,并允许您在方法链之后有一个返回值
减少显示对话框所需的代码量的示例如下:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
使用单个返回值的示例如下:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
Dennis在对您的问题的评论中提到的使用生成器模式的示例:
new DialogBuilder().setMessage("some message").setTitle("some title").build().show();
生成器模式允许您在构建对象之前为类的新实例设置所有参数(考虑具有final
字段的类或对象,在构建后为其设置值比在构建时设置值更昂贵)
在上面的示例中:setMessage(String)
,setTitle(String)
属于DialogBuilder
类,并返回调用它们的DialogBuilder
的相同实例;build()
方法属于DialogBuilder
类,但返回Dialog
对象show()
方法属于Dialog
类
额外的
这可能与您的问题无关,但它可能会帮助您和遇到此问题的其他人
这适用于大多数用例:所有不涉及继承的用例和一些涉及继承的特定用例,当派生类不添加要链接在一起的新方法时和您不想使用(不强制转换)方法链的结果作为派生类的对象时
如果您想为基类中没有方法的派生类对象建立方法链,或者希望方法链返回对象作为派生类的引用,您可以查看其答案。减少显示对话框所需的代码量的示例如下:
// Your Dialog has a method show()
// You could show a dialog like this:
new Dialog().setMessage("some message").setTitle("some title")).show();
// In another class, you have a method showDialog(Dialog)
// Thus you can do:
showDialog(new Dialog().setMessage("some message").setTitle("some title"));
package com.rsa.arraytesting;
public class ExampleJavaArray {
String age;
String name;
public ExampleJavaArray getAge() {
this.age = "25";
return this;
}
public ExampleJavaArray setName(String name) {
this.name = name;
return this;
}
public void displayValue() {
System.out.println("Name:" + name + "\n\n" + "Age:" + age);
}
}
另一类
package com.rsa.arraytesting;
public class MethodChaining {
public static void main(String[] args) {
ExampleJavaArray mExampleJavaArray = new ExampleJavaArray();
mExampleJavaArray.setName("chandru").getAge().displayValue();
}
}
只需添加一个静态生成器方法,并创建另一组setter方法。 比如说
class Model {
private Object FieldA;
private Object FieldB;
public static Model create() {
return new Model();
}
public Model withFieldA(Object value) {
setFieldA(value);
return this;
}
public Model withFieldB(Object value) {
setFieldB(value);
return this;
}
}
像这样使用它
Model m = Model.create().withFieldA("AAAA").withFieldB(1234);
如果您使用的是lombok,则可以使用
lombok.config
中的参数:
lombok.accessors.chain = true
或者对于特定的数据类,您可以声明@Accessors(chain=true)
注释:
import lombok.experimental.Accessors;
@Accessors(chain = true)
@Data
public class DataType {
private int value;
// will generate setter:
public DataType setValue(int value) {
this.value = value;
return this;
}
}
与构建器模式类似?这通常称为
fluent
或fluent
编程。您可以为对话框创建一个构建器,而不是使用fluent api的设置器。如果您这样做,那么许多类似bean的访问将不再有效,因为框架限制setter返回void。如果你不喜欢写所有的锅炉板,那就看看龙目计划吧。我已经添加了最后使用的示例,因为我错过了你问题的一部分。请注意,这些并不是方法链接和/或构建器模式的唯一用途:与大多数编码模式、算法和技术一样,通过一点创造性,可以找到旧模式(算法、技术)的新用途:)更改设置行为不是好的做法,因为有些框架不希望setter方法返回任何结果。例如,如果模型中的setter将返回一些值,则Cassandra Java驱动程序将出现错误。package com.rsa.arraytesting;公共类方法链接{public static void main(String[]args){ExampleJavaArray mExampleJavaArray=new ExampleJavaArray();mExampleJavaArray.setName(“chandru”).getAge().displayValue();}