設計模式-Factory模式戲說
1。利用抽象基類繼承的方式,和抽象基類之間的從屬關係來構造整個系統,達到一個相對清晰的類層次關係。
2。保持整個系統的可擴充和最低的維護成本。
我說清楚了嗎?上碼,^_^。
///////////////////////////////////////////////////////////////
class 飛彈
{
public:
vitrual ~飛彈();
protected:
飛彈();
}
//------------------------------------------------------------
class 地對空飛彈 :public 飛彈
{
public:
地對空飛彈();
~地對空飛彈();
}
class 反導防禦:public 飛彈
{
public:
反導防禦();
~反導防禦();
}
/////////////////////////////////////////////////////////////
class 車軲轆
{
public:
vitrual ~車軲轆();
protected:
車軲轆();
}
//-------------------------------------------------
class 韓國車軲轆 :public 車軲轆
{
public:
韓國車軲轆();
~韓國車軲轆();
}
class 東風車軲轆 :public 車軲轆
{
public:
東風車軲轆();
~東風車軲轆();
}
/////////////////////////////////////////////////////////////////
class 輪胎
{
public:
vitrual ~輪胎();
protected:
輪胎();
}
//---------------------------------------------
class 米琪琳 :pubilc 輪胎
{
public:
米琪琳();
~米琪琳();
}
class 吉利 :pubilc 輪胎
{
public:
吉利();
~吉利();
}
//////////////////////////////////////////////////////////////////
class 汽車
{
public:
vitrual ~汽車();
virtual 輪胎* 輪胎()const {return new 輪胎;}
virtual 車軲轆* 車軲轆()const {return new 車軲轆;}
virtual 飛彈* 飛彈()const {return new 飛彈;}
protected:
汽車();
}
class 我的車 : public 汽車
{
public:
我的車();
~我的車();
virtual 輪胎* 輪胎()const {return new 吉利;}
virtual 車軲轆* 車軲轆()const {return new 東風車軲轆;}
virtual 飛彈* 飛彈()const {return new 地對空飛彈;}
}
class 老婆的車 : public 汽車
{
public:
老婆的車();
~老婆的車();
virtual 輪胎* 輪胎()const {return new 米琪琳;}
virtual 車軲轆* 車軲轆()const {return new 韓國車軲轆;}
virtual 飛彈* 飛彈()const {return new 反導防禦;}
}
class 童車 : public 汽車
{
public:
童車();
~童車();
virtual 輪胎* 輪胎()const {return new 吉利;}
virtual 車軲轆* 車軲轆()const {return new 東風車軲轆;}
virtual 飛彈* 飛彈()const {return new 反導防禦;}
}