可以在java中创建一个可扩展的类层次结构,其方法流畅且可以按任何顺序调用吗? (是!见下面的答案),即使对于现有的课程,如果您无法访问源代码,只要这些方法很流畅!
我正在改造现有的层次结构并希望使用工厂或至少是通用构造函数和(最终)不可变构建器模式(JB P.14)。设置字段的方法返回{ {1}} - 他们最好返回一个通用的void
- 这样我们就可以获得进行方法链接的功能(他们现在都调用T
)。
目标:
1. 避免在每个类中创建静态getFactory()方法。
2. 简单方法签名。
3. 创建一个通用的工厂方法,但会在编译时捕获问题。
4. 在出错时获取编译时错误而不是运行时错误。
根据要求,非通用代码非常简单,但不起作用。
super
public class A {
private String a = null;
protected A setA(String a){
this.a = a;
return this;//<== DESIRE THIS TO BE CHAINABLE
}
protected static A factory(){
return new A();
}
}
现在,来电者可以尝试拨打public class B extends A {
private String b = null;
protected Foo setB(String b){
this.b = b;
return this;//<== DESIRE THIS TO BE CHAINABLE
}
protected static B factory(){
return new B();
}
}
但无法编译,因为B.factory().setA("a").setB("b")//won't compile
会返回setA()
,而不是A
。您可以通过覆盖B
中的setA()
,调用B
并返回setB()
而不是B
来使其工作。为了避免委托每种方法,重点是。我只想要一个可扩展的可链接类方法组,可以按任何顺序调用它们。 A
显然有用。
答案 0 :(得分:3)
答案(令我惊讶和满意)是肯定的。我自己回答了这个question: 如果方法调用返回有问题的类的实例,则可以通过一些工作来完成此操作(请参阅下面的可链接)。如果您可以编辑顶级源代码,我还发现了更简单的方法:
在顶级班级(A)中:
protected final <T> T a(T type) {
return type
}
假设C延伸B而B延伸A。
调用:
C c = new C();
//Any order is fine and you have compile time safety and IDE assistance.
c.setA("a").a(c).setB("b").a(c).setC("c");
示例1和3是使现有类层次结构流畅并允许以任何顺序调用方法的方法,只要现有类流畅(但您无权访问或无法更改源代码) )。 WAY2是您可以访问源的示例,并希望调用尽可能简单。
完整的SSCCE:
import static java.lang.System.out;
public class AATester {
public static void main(String[] args){
//Test 1:
for(int x: new int[]{ 0, 1, 2 } ){
A w = getA(x);
//I agree this is a nasty way to do it... but you CAN do it.
Chain.a(w.setA("a1")).a(w instanceof C ? ((C) w).setC("c1") : null );
out.println(w);
}
//Test for WAY 2: Hope this wins Paul Bellora's approval
//for conciseness, ease of use and syntactic sugar.
C c = new C();
//Invoke methods in any order with compile time type safety!
c.setA("a2").a(c).setB("b2").a(c).set("C2");
out.println(w);
//Example 3, which is Example 1, but where the top level class IS known to be a "C"
//but you don't have access to the source and can't add the "a" method to the
//top level class. The method invocations don't have to be as nasty as Example 1.
c = new C();
Chain.a(c.setA("a3")).a(c.setB("b3")).a(c.setC("c3"));//Not much larger than Example 2.
out.println(w);
}
public static getA(int a){//A factory method.
A retval;//I don't like multiple returns.
switch(a){
case 0: retval = new A(); break;
case 1: retval = new B(); break;
default: retval = new C(); break;
}
return retval;
}
}
测试类A
public class A {
private String a;
protected String getA() { return a; }
//WAY 2 - where you have access to the top level source class.
protected final <T> T a(T type) { return type; }//This is awesome!
protected A setA(String a) { this.a=a; return this; }//Fluent method
@Override
public String toString() {
return "A[getA()=" + getA() + "]";
}
}
测试类B
public class B extends A {
private String b;
protected String getB() { return b; }
protected B setB(String b) { this.b=b; return this; }//Fluent method
@Override
public String toString() {
return "B[getA()=" + getA() + ", getB()=" + getB() + "]\n "
+ super.toString();
}
}
测试类C
public class C extends B {
private String c;
protected String getC() { return c; }
protected C setC(String c) { this.c=c; return this; }//Fluent method
@Override
public String toString() {
return "C [getA()=" + getA() + ", getB()=" + getB() + ", getC()="
+ getC() + "]\n " + super.toString();
}
}
连锁课
/**
* Allows chaining with any class, even one you didn't write and don't have
* access to the source code for, so long as that class is fluent.
* @author Gregory G. Bishop ggb667@gmail.com (C) 11/5/2013 all rights reserved.
*/
public final class Chain {
public static <K> _<K> a(K value) {//Note that this is static
return new _<K>(value);//So the IDE names aren't nasty
}
}
Chain的助手类。
/**
* An instance method cannot override the static method from Chain,
* which is why this class exists (i.e. to suppress IDE warnings,
* and provide fluent usage).
*
* @author Gregory G. Bishop ggb667@gmail.com (C) 11/5/2013 all rights reserved.
*/
final class _<T> {
public T a;//So we may get a return value from the final link in the chain.
protected _(T t) { this.a = t }//Required by Chain above
public <K> _<K> a(K value) {
return new _<K>(value);
}
}
输出:
A [get(A)=a]
B [get(A)=a, getB()=null]
A [getA()=a]
C [getA()=a, getB()=null, getC()=c)]
B [get(A)=a, getB()=null]
A [get(A)=a]
QED。 :)
我从未见过有人这样做过;我认为这可能是一种新的且具有潜在价值的技术。
P.S。关于“elvis like usage”,它是1或2行而不是8或更多。
Book b = null; Publisher p = null; List books = null; String id = "Elric of Melnibone"; books = Chain.a(b = findBook(id)).a(b != null ? p = b.getPublisher() : null) .a(p != null ? p.getPublishedBooks(): null).a; out.println(books==null ? null : Arrays.toString(books.toArray()));
vs:
Book b = null; Publisher p = null; List books = null; String id = "Elric of Melnibone"; b = findBook(id); Array[] books = null; if( b != null ) { p = b.getPublisher(); if(p != null) { books = p.getPublishedBooks(); } } out.println(books==null ? null : Arrays.toString(books.toArray()));
没有NPE,如果链条完成,您将获得“Melnibone的Elric”出版商出版的所有书籍(即“Ace”出版商已出版的所有书籍),如果没有,则获得无效。
答案 1 :(得分:3)
我相信有一种方法可以使用泛型...语法有点不如期望的那样干净......
这是客户端代码......
B<B> b = B.factoryB();
b.setA("a").setB("b");
A<A> ba = A.factoryA();
ba.setA("a");
顶级(真实)课程
public class A<S extends A> extends Chained<S> {
private String a = null;
protected A() {
}
public S setA(String a) {
this.a = a;
return me();
}
public static A<A> factoryA() {
return new A<A>();
}
}
示例子类
public class B<S extends B> extends A<S> {
private String b = null;
B() {
}
public S setB(String b) {
this.b = b;
return me();
}
public static B<B> factoryB() {
return new B<B>();
}
}
辅助
public abstract class Chained<S extends Chained> {
// class should be extended like:
// ... class A<S extends A> extends Chained<S>
public Chained() {
}
public final S me() {
return (S) this;
}
}
它远非完美,可以不工作(如果你真的想要)
答案 2 :(得分:0)
如果可以访问源代码,通过扩展Alan写的内容,我会添加补充类来隐藏泛型,同时允许继承和非常紧凑的语法。 BaseA和BaseB执行层次结构,而A和B确实隐藏了泛型。
BaseA
+- A
+- BaseB
+- B
public class BaseA<S extends BaseA<?>> {
private String a = null;
protected BaseA() {
}
@SuppressWarnings("unchecked")
public S setA(String a) {
this.a = a;
return (S) this;
}
}
public class A extends BaseA<A> {
public static A factoryA() {
return new A();
}
}
public class BaseB<S extends BaseB<?>> extends BaseA<S> {
private String b = null;
protected BaseB() {
}
@SuppressWarnings("unchecked")
public S setB(String b) {
this.b = b;
return (S) this;
}
}
public class B extends BaseB<B> {
public static B factoryB() {
return new B();
}
}
public class Main {
public static void main(String[] args) {
B.factoryB().setA("").setB("").setB("").setA("").setA("");
}
}
答案 3 :(得分:-1)
流畅的界面与您已有的常规命令查询方法不同。关注点分离使得分离它们成为一个好主意。
由于您有一个现有的代码层次结构:编写一个流畅的外观,为您完成脏工作。
另见Martin Fowler:领域特定语言,4.2:对解析层的需求。