Re: [請教] 請教strategy、state pattern in C++

作者: leondemon (狗狗)   2011-04-23 17:40:45
我不懂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要改變的問題
不知道有沒有解決你的問題

Links booklink

Contact Us: admin [ a t ] ucptt.com