要对模型的操作中,为了实现不现的命令,可以用简单工厂+策略模式,这个也是实现mvc架构的一个重要的设计模式
具体代码如下
class CContext//使用策略模式加简单工厂
{
public:
CContext(){m_pCmd = new CComand(), m_pEx = new CExplan(),m_pMove = new CMove();};//实现即时平移
virtual ~CContext(){ if(m_pCmd) {delete m_pCmd; } if(m_pEx){delete m_pEx;} if(m_pMove) {delete m_pMove;}};
void TypeCmd(int Num);
void OnLButtonDown(UINT nFlags, CPosition pos){m_pCmd->OnLButtonDown(nFlags, pos);};
void OnMouseMove(UINT nFlags, CPosition pos){m_pCmd->OnMouseMove(nFlags, pos),m_pMove->OnMouseMove(nFlags, pos);};
void OnRButtonDown(UINT nFlags, CPosition pos){m_pCmd->OnRButtonDown(nFlags, pos);};
void OnLButtonUp(UINT nFlags, CPosition pos){m_pCmd->OnLButtonUp(nFlags, pos);};
void OnMouseWheel(UINT nFlags, short zDelta, CPosition pos){m_pCmd->OnMouseWheel(nFlags, zDelta, pos), m_pEx->OnMouseWheel(nFlags, zDelta, pos);};
void OnMButtonDown(UINT nFlags, CPosition pos){m_pCmd->OnMButtonDown(nFlags, pos), m_pMove->OnMButtonDown(nFlags, pos);};
void OnMButtonUp(UINT nFlags, CPosition pos){m_pCmd->OnMButtonUp(nFlags, pos), m_pMove->OnMButtonUp(nFlags, pos);};
private:
CComand *m_pCmd;
CComand *m_pEx;//实现即时中键缩放
CComand *m_pMove;//实现即中键平移
};
void CContext::TypeCmd(int Num)
{
if(m_pCmd)
{
delete m_pCmd;
m_pCmd = NULL;
}
switch(Num)
{
case ID_DRAW_LINE :
m_pCmd = new CCreateLine();//改变光标
break;
case ID_DRAW_CIRCLE :
m_pCmd = new CCreateCircle();
break;
case ID_DRAW_RECT :
m_pCmd = new CCreateRect();
break;
case ID_EXPLANSMALL :
m_pCmd = new CExsmall();
break;
case ID_EXPLANLarge :
m_pCmd = new CExlarge();
break;
case ID_TRANSLATE :
m_pCmd = new CBaseMove();//平移命令
break;
case ID_PICK :
m_pCmd = new CSelect();//检选
break;
case ID_UNPICK :
m_pCmd = new CUnSelect();//去掉捡选
break;
case ID_DELETE :
m_pCmd = new CDelete();//删除
break;
case ID_ALLDELETE :
m_pCmd = new CAllDelete();//全删
break;
case ID_MIRROR :
m_pCmd = new CMirror();
break;
case ID_ROTATE :
m_pCmd = new CMirror();
break;
default:
break;
}
}