我知道如何设计简单流畅的界面类,但是
我想知道如何写这样的东西(某些方法访问特定方法而不是全部)
MyClass cls = new MyClass()
.A()
.A1()
.A2()
.A01()
.A02()
.B()
.B1()
.C()
.C1()
.C01()
.C02()
- MyClass() 只能访问 A()、B()、C()
- A()只能访问 A1()、A2()
- A2()只能访问A01()、A02()
- B()只能访问 B1()
- C()只能访问 C1()
- C1() 只能访问 C01()、C02()
如何设计具有流畅接口(interface)的类并限制java中的访问?
最佳答案
通常你会创建一个核心类,并编写很多接口(interface),然后通过某个入口点将其作为初始接口(interface)传递给用户:
public class EntryPoint {
MyClass myClass() {
return new Implementation();
}
}
// not visible to the user
class Implementation implements MyClass, A, B, C, A1, A2, ...{
Implementation A() { ... return this; }
Implementation A1() { ... return this; }
...
}
public interface MyClass {
A A();
B B();
C C();
}
public interface A {
A1 A1();
A2 A2();
}
public interface B {
B1 B1();
}
public interface C {
C1 C1();
}
...
关于java - 如何在java中创建复杂的Fluent界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843518/