[設計] 重構去除未使用參數的方式
大家好:
最近遇到一個問題,就是要重構某個 class 階層中,
未使用到的 function parameters,但卻不知該如何下手
才好。原本的 code 示意如下:
(程式碼以C++舉例,但實際的應用可能是其他語言,例如
不支援多重繼承的Java)
class BaseClass
{
public:
virtual void Foo(int a, int b) = 0;
};
class DerivedOne : public BaseClass
{
public:
virtual void Foo(int a, int b)
{
cout << a << b << endl;
}
};
class DerivedTwo : public BaseClass
{
public:
virtual void Foo(int a, int b)
{
// b is unreferenced
cout << a << endl;
}
};
如上所示,當我期望透過多型的方式,一視同仁的操作
實際上指向不同類別的物件時,有的子類別會用到某個
參數(例子中的b) ,但某些子類別則否,這時候該怎麼
去除這樣的 unreferenced parameters比較好呢?
如:
BaseClass *ptrA = new DerivedOne;
ptrA->Foo(0, 1);
BaseClass *ptrB = new DerivedTwo;
ptrB->Foo(0, 1); // 1 is unreferenced
不知道大家有沒有什麼比較好的想法?謝謝。
--
記憶是不由人的,它想來,才會來,
它不想走,妳怎麼也躲不過。
-朱少麟.燕子-
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.2.245
推
05/09 21:47, , 1F
05/09 21:47, 1F
→
05/09 21:47, , 2F
05/09 21:47, 2F
推
05/09 22:55, , 3F
05/09 22:55, 3F
→
05/09 22:57, , 4F
05/09 22:57, 4F
→
05/09 23:11, , 5F
05/09 23:11, 5F
→
05/09 23:13, , 6F
05/09 23:13, 6F
→
05/09 23:14, , 7F
05/09 23:14, 7F
→
05/09 23:15, , 8F
05/09 23:15, 8F
→
05/09 23:16, , 9F
05/09 23:16, 9F
→
05/09 23:33, , 10F
05/09 23:33, 10F
→
05/12 20:05, , 11F
05/12 20:05, 11F
→
05/14 00:04, , 12F
05/14 00:04, 12F
→
05/14 00:04, , 13F
05/14 00:04, 13F
→
05/14 00:05, , 14F
05/14 00:05, 14F
→
05/14 00:05, , 15F
05/14 00:05, 15F
→
05/14 09:23, , 16F
05/14 09:23, 16F
→
05/14 09:24, , 17F
05/14 09:24, 17F
→
05/14 09:24, , 18F
05/14 09:24, 18F
→
05/14 09:25, , 19F
05/14 09:25, 19F
→
05/14 09:25, , 20F
05/14 09:25, 20F
→
05/14 09:26, , 21F
05/14 09:26, 21F
→
05/14 09:26, , 22F
05/14 09:26, 22F
→
05/14 09:29, , 23F
05/14 09:29, 23F
→
05/14 09:29, , 24F
05/14 09:29, 24F
→
05/14 09:31, , 25F
05/14 09:31, 25F
→
05/14 09:33, , 26F
05/14 09:33, 26F
OOAD 近期熱門文章
PTT數位生活區 即時熱門文章