緣起,看到一遍文章,題材取自於《C++沉思錄》,綜合利用好幾種設計模式,並且結合所謂的面向對象的技巧,然後洋洋自得,並且在最後,還反問:“有更好的解決方案嗎?”。本座暗歎,又一個設計模式的毒害者。以下,就提出另一種解決方案。
首先,先聲明一點,現實中,一張圖片,可以給予添加一層又一層片框,也可以和其他的圖片組合在一塊,或橫,或豎,……,但是,圖片,始終只有一張,對它處理之後,它就一直是那個樣子了,不可能同時看到它的兩個樣子,或加邊框,或和其他照片組合在一塊。如果,拿這張圖片去進行復制,那又自是另當別論,但那已經是另外一第圖片了。當然,對以下例子稍加修改,就能支持修飾的圖片的復制操作。
本座最近在寫一套消息框架,如果這個例子能用上那套框架,寫起來就會很簡單。但是,即使沒有消息框架的支持,利用消息發送來解決這個問題,也是相當小兒科的事情。為了突出重點,忽略了各種異常處理,沒有優化,也不管什麼編程風格,純粹直奔主題。解決這個例子的最重要一點,就在於打印圖片時,要一行一行地從頂到底順次打印下來。 www.2cto.com
typedef int (*ProcPictureImp)(void* pThis, int nMessage, void* param1, void* param2);
enum { PM_WIDTH, PM_HEIGHT, PM_PRINT_ROW};
struct PictureImp
{
void* pThis;
ProcPictureImp proc;
int GetWidth()
{
return (*proc)(pThis, PM_WIDTH, NULL, NULL);
}
int GetHeight()
{
return (*proc)(pThis, PM_HEIGHT, NULL, NULL);
}
int PrintRow(int nRow, ostream& out)
{
return (*proc)(pThis, PM_PRINT_ROW, (void*)nRow, (void*)&out);
}
};
class CPicture
{
public:
CPicture(const char* pDatas[], int nCount)
{
m_pDatas = pDatas;
m_nCount = nCount;
m_nWidth = 0;
for (int i=0; i<m_nCount; i++)
{
int nLen = strlen(m_pDatas[i]);
if (m_nWidth < nLen)
m_nWidth = nLen;
}
m_Imp.pThis = this;
m_Imp.proc = HandleMsg;
}
void Print(ostream& out);
public:
PictureImp m_Imp;
private:
const char** m_pDatas;
int m_nCount;
int m_nWidth;
static int HandleMsg(void* pThis, int nMessage, void* param1, void* param2);
};
int CPicture::HandleMsg(void* pThis, int nMessage, void* param1, void* param2)
{
CPicture* pSelf = (CPicture*)pThis;
switch (nMessage)
{
case PM_WIDTH:
return pSelf->m_nWidth;
case PM_HEIGHT:
return pSelf->m_nCount;
break;
case PM_PRINT_ROW:
int nRow = (int)param1;
ostream& out = *(ostream*)param2;
if (nRow >= pSelf->m_nCount)
break;
int i=0;
for (; pSelf->m_pDatas[nRow][i] != 0; i++)
out << pSelf->m_pDatas[nRow][i];
for (; i<pSelf->m_nWidth; i++)
out << ' ';
}
return 0;
}
void CPicture::Print(ostream& out)
{
int nHeight = m_Imp.GetHeight();
for (int nRow = 0; nRow<nHeight; nRow++)
{
m_Imp.PrintRow(nRow, out);
out << endl;
}
}
class CFrameDecorater
{
public:
CFrameDecorater(PictureImp& imp)
{
m_PrevImp = imp;
imp.pThis = this;
imp.proc = HandleMsg;
}
private:
PictureImp m_PrevImp;
static int HandleMsg(void* pThis, int nMessage, void* param1, void* param2);
};
int CFrameDecorater::HandleMsg(void* pThis, int nMessage, void* param1, void* param2)
{
CFrameDecorater* pSelf = (CFrameDecorater*)pThis;
PictureImp& prevImp = pSelf->m_PrevImp;
switch (nMessage)
{
case PM_WIDTH:
return prevImp.GetWidth()+2;
case PM_HEIGHT:
return prevImp.GetHeight()+2;
case PM_PRINT_ROW:
int nRow = (int)param1;
ostream& out = *(ostream*)param2;
bool bMyRow = nRow == 0 || nRow>prevImp.GetHeight();
if (nRow >= prevImp.GetWidth()+2)
break;
if (nRow == 0 || nRow>prevImp.GetHeight())
{
out << '+';
for (int i=0; i<prevImp.GetWidth(); i++)
out << '-';
out << '+';
}
else
{
out << '|';
prevImp.PrintRow(nRow-1, out);
out << '|';
}
}
return 0;
}
class CHorseDecorater
{
public:
CHorseDecorater(PictureImp& impLeft, PictureImp& impRight)
{
m_Left = impLeft;
m_Right = impRight;
impLeft.pThis = this;
impLeft.proc = HandleMsg;
impRight.pThis = this;
impRight.proc = HandleMsg;
}
private:
PictureImp m_Left;
PictureImp m_Right;
static int HandleMsg(void* pThis, int nMessage, void* param1, void* param2);
static void PrintRow(PictureImp& pict, int nRow, ostream& out)
{
if (nRow < pict.GetHeight())
pict.PrintRow(nRow, out);
else
{
for (int i=0; i<pict.GetWidth(); i++)
out << ' ';
}
}
};
int CHorseDecorater::HandleMsg(void* pThis, int nMessage, void* param1, void* param2)
{
CHorseDecorater* pSelf = (CHorseDecorater*)pThis;
PictureImp& pictLeft = pSelf->m_Left;
PictureImp& pictRight = pSelf->m_Right;
switch (nMessage)
{
case PM_WIDTH:
return pictLeft.GetWidth()+pictRight.GetWidth();;
case PM_HEIGHT:
return max(pictLeft.GetHeight(), pictRight.GetHeight());
case PM_PRINT_ROW:
int nRow = (int)param1;
ostream& out = *(ostream*)param2;
PrintRow(pictLeft, nRow, out);
PrintRow(pictRight, nRow, out);
}
return 0;
}
int main()
{
const char* init1[] = {"Paris", "in the", "Spring", "HaHa"};
CPicture pict1(init1, 3);
pict1.Print(cout);
CFrameDecorater framer1(pict1.m_Imp);
//pict1.Print(cout);
CFrameDecorater framer2(pict1.m_Imp);
pict1.Print(cout);
CPicture pict2(init1, 4);
CHorseDecorater hors(pict1.m_Imp, pict2.m_Imp);
pict1.Print(cout);
CFrameDecorater framerHorse(pict1.m_Imp);
pict1.Print(cout);
return 0;
}
嗯,明眼人一眼就看出來了,切,這不過是模仿WINDOWS的窗口消息處理函數的伎倆而已。沒有繼承、沒有虛函數,何其美妙哉!
又,注意到main()中的代碼,雖然pict1被裝飾了好幾次,但其圖片打印,始終都是pict1.Print(cout)。所謂不管八風亂起,我自巋然不動