您可以使用接口来划分方法。
// you can probably come up with better names, taking your real situation into account
interface Logicable {
SubLogicOneAble logicOne(/*same input variables as logicOne*/);
SubLogicTwoAble logicTwo(/*same input variables as logicTwo*/);
}
interface SubLogicThreeAble {
int subLogicThree(/*same input variables as subLogicThree*/);
}
interface SubLogicOneAble extends SubLogicThreeAble {
int subLogicOne(/*same input variables as subLogicOne*/);
}
interface SubLogicTwoAble extends SubLogicThreeAble {
int subLogicTwo(/*same input variables as subLogicTwo*/);
}
然后,您可以让调用方创建
Example
Logicable
而不是
new Example()
logicOne
和
logicTwo
实例
当然
class Example implements SubLogicOneAble, SubLogicTwoAble, Logicable {
private Example() {}
// could also consider putting this in Logicable and calling it newLogicable (?)
public static Logicable newExample() {
return new Example();
}
Example.newExample().subLogicOne() // compiler error
逻辑一
逻辑二
SubLogicOneAble
和
SubLogicTwoAble
实例
应更改以符合接口:
public SubLogicOneAble logicOne (/*Input variables*/){
// Do logic
return this;
}
public SubLogicTwoAble logicTwo (/*Input variables*/){
// Do logic
return this;
}