我不懂C++ 也看不太懂你的问题
但是如果一个庞大的class内部有许多需求需要改变的时候 一般会用下面作法:
原本状态:有一个物件叫做Computer
他有9种方法(method1-method9)以及5种实体变量(ivar1-ivar5)
用pseudo-code表示:
class Computer:
@attribute(public) ivar1 //为public的attribute
@attribute(public) ivar2
@attribute(public) ivar3
@attribute(private) ivar4 //为private的attribute
@attribute(private) ivar5
-method1 #public > block1 //表示method1内部使用block1的算法逻辑 以下类推
-method2 #public > block2
-method3 #public > block3
-method4 #public > block4
-method5 #public > block5
-method6 #public > block6
-method7 #public > block7
-method8 #public > block8
-method9 #public > block9
end
通常我遇到这么大的class我第一步都是在class前方先写注解
来叙述这个物件的responsiblity
现在你希望你维持同样的public interface给使用这个类别的人使用 又有改变的需求
假设method4-method6和method7-method9都有改变需求但改变动机和时机不同
作法会变成宣告两个新的Helper物件:
class HelperA:
-method4 #public > block4
-method5 #public > block5
-method6 #public > block6
end
class HelperB:
-method7 #public > block7
-method8 #public > block8
-method9 #public > block9
end
class Computer:
@attribute(public) ivar1
@attribute(public) ivar2
@attribute(public) ivar3
@attribute(priavte) ivar4
@attribute(private) ivar5
@attribute(private) HelperA instanceOfHelperA
@attribute(private) HelperB instanceOfHelperB
-method1 #public > block1
-method2 #public > block2
-method3 #public > block3
-method4 #public > instanceOfHelperA.method4
-method5 #public > instanceOfHelperA.method5
-method6 #public > instanceOfHelperA.method6
-method7 #public > instanceOfHelperB.method7
-method8 #public > instanceOfHelperB.method8
-method9 #public > instanceOfHelperB.method9
end
但是因为HelperA和HelperB都需要Computer的ivar3-ivar5的资料
于是必须多一个wrapper来当物件之间的传递:
class DataWrapper:
@attribute(public) ivar3
@attribute(public) ivar4
@attribute(public) ivar5
end
clas
@attribute(public) ivar1
@attribute(public) ivar2
@attribute(public) ivar3 > instanceOfDataWrapper.ivar3
@attribute(private) ivar4 > instanceOfDataWrapper.ivar4
@attribute(private) ivar5 > instanceOfDataWrapper.ivar5
@attribute(private) DataWrapper instanceOfDataWrapper
@attribute(private) HelperA instanceOfHelperA
@attribute(private) HelperB instanceOfHelperB
-method1 #public > block1
-method2 #public > block2
-method3 #public > block3
-method4 #public > instanceOfHelperA.method4(instanceOfDataWrapper)
-method5 #public > instanceOfHelperA.method5(instanceOfDataWrapper)
-method6 #public > instanceOfHelperA.method6(instanceOfDataWrapper)
-method7 #public > instanceOfHelperB.method7(instanceOfDataWrapper)
-method8 #public > instanceOfHelperB.method8(instanceOfDataWrapper)
-method9 #public > instanceOfHelperB.method9(instanceOfDataWrapper)
end
当然HelperA和HelperB的method所吃的参数也要改变
class HelperA
-method4(DataWrapper) #public > block4
-method5(DataWrapper) #public > block5
-method6(DataWrapper) #public > block6
end
class HelperB
-method7(DataWrapper) #public > block7
-method8(DataWrapper) #public > block8
-method9(DataWrapper) #public > block9
end
现在你就可以subclass HelperA和HelperB来解决庞大Computer要改变的问题
不知道有没有解决你的问题