B
bhavya internship
Guest
Hello All,
My understanding about the Factory method design pattern is -
Problem:
The scenario is when we created a class that has some other classes and method. At a later point of time there are chances of adding additional functionality to the method. To add additional functionality we are creating another method in the class instead of using the existing method.
class CUIFrameWork{
public:
CUITemplate* CreateUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponent();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
In the below code, i am adding the concrete product CUIComponentScrolling and with that the above class becomes like as shown below:
class CUIFrameWork
{
public:
CUITemplate* CreateUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponent();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
CUITemplate* CreateScrollingUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponentScrolling();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
Here to avoid creating a new method every time when adding additional functionality we use Factory method by creating an interface or superclass that has generic behavior with only the definition and allow the subclasses derive from it handles the implementation and the subclasses will decide which classes to instantiate.
Is my understanding is correct?
class CUIFrameWork
{
public:
virtual CDataComponent* MakeDataComp()
{
return new CDataComponent();
}
virtual CUIComponent* MakeUIComp()
{
return new CUIComponent();
}
virtual CToolBarComponent* MakeToolBarComp( UINT nID )
{
return new CToolBarComponent( nID );
}
CUITemplate* CreateUI()
{
CDataComponent* pData = MakeDataComp();
CUIComponent* pUI = MakeUIComp();
CToolBarComponent* pTooBar1 = MakeToolBarComp( ID_STANDARD );
pTooBar1->AddDropDownButton();
pTooBar1->AddComboBox();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
To create a scrolling UI component, we would just need to create another framework class and override the method MakeUIComp() and create a scrolling UI component.
class CUIFrameWork_ScrollingUI : public CUIFrameWork
{
public:
virtual CUIComponent* MakeUIComp()
{
return new CUIComponentScrolling();
}
};
Similar to changing the toolbar component, we can create another framework class and override the MakeToolBarComp() method and create new toolbar components.
class CUIFrameWork_SizingBars : public CUIFrameWork
{
public:
virtual CToolBarComponent* MakeToolBarComp( UINT nID )
{
return new CToolBarComponent_SizingBar( nID );
}
};
Can I take the above scenario and example to explain the Factory method design pattern?
Please provide your comments.
Thanks in Advance
Continue reading...
My understanding about the Factory method design pattern is -
Problem:
The scenario is when we created a class that has some other classes and method. At a later point of time there are chances of adding additional functionality to the method. To add additional functionality we are creating another method in the class instead of using the existing method.
class CUIFrameWork{
public:
CUITemplate* CreateUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponent();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
In the below code, i am adding the concrete product CUIComponentScrolling and with that the above class becomes like as shown below:
class CUIFrameWork
{
public:
CUITemplate* CreateUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponent();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
CUITemplate* CreateScrollingUI()
{
CDataComponent* pData = new CDataComponent();
CUIComponent* pUI = new CUIComponentScrolling();
CToolBarComponent* pTooBar1 = new CToolBarComponent();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
Here to avoid creating a new method every time when adding additional functionality we use Factory method by creating an interface or superclass that has generic behavior with only the definition and allow the subclasses derive from it handles the implementation and the subclasses will decide which classes to instantiate.
Is my understanding is correct?
class CUIFrameWork
{
public:
virtual CDataComponent* MakeDataComp()
{
return new CDataComponent();
}
virtual CUIComponent* MakeUIComp()
{
return new CUIComponent();
}
virtual CToolBarComponent* MakeToolBarComp( UINT nID )
{
return new CToolBarComponent( nID );
}
CUITemplate* CreateUI()
{
CDataComponent* pData = MakeDataComp();
CUIComponent* pUI = MakeUIComp();
CToolBarComponent* pTooBar1 = MakeToolBarComp( ID_STANDARD );
pTooBar1->AddDropDownButton();
pTooBar1->AddComboBox();
pUI->AddToolBar(pTooBar1);
return new CUITemplate( pData, pUI );
}
};
To create a scrolling UI component, we would just need to create another framework class and override the method MakeUIComp() and create a scrolling UI component.
class CUIFrameWork_ScrollingUI : public CUIFrameWork
{
public:
virtual CUIComponent* MakeUIComp()
{
return new CUIComponentScrolling();
}
};
Similar to changing the toolbar component, we can create another framework class and override the MakeToolBarComp() method and create new toolbar components.
class CUIFrameWork_SizingBars : public CUIFrameWork
{
public:
virtual CToolBarComponent* MakeToolBarComp( UINT nID )
{
return new CToolBarComponent_SizingBar( nID );
}
};
Can I take the above scenario and example to explain the Factory method design pattern?
Please provide your comments.
Thanks in Advance
Continue reading...