簡單工廠:
從前有個工廠,這個工廠能生產兩個產品A和B。有一天我來買A,我給工廠說,給我來個A。
工廠老闆聽見了,哦,你要A啊。我找一下啊。行,我發現我們廠能生產A,你等下哦,我馬上給你造處來。
工廠老闆將造A的需求拋給生產線,生產線走產品A的線路生產出來A。最後給我。
class Product 產品的基類,提供虛函數接口。 生產線的頭
{
public:
virtual void show() = 0;
};
class Product_A : public Product 產品A 生產A線
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product 產品B 生產B線
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
--------------------------------------------------------------------------------
class Factory 工廠老闆
{
public:
Product* Create(char i) 返回的是產品的基類
{ 相當於一個基類指針指向了派生類對象。在調用的時候調用派生類的同名覆蓋方法。
switch (i) 需要在工廠中確定產品類型
{
case 'A':
return new Product_A; 由於在繼承中支持由下向上的轉化。所以return子類類型。
break;
case 'B':
return new Product_B;
break;
default:
break;
}
}
};
---------------------------------------------------------------------------------
int main()
{
Factory *factory = new Factory();
factory->Create('A')->show(); 告訴工廠老闆,我要一個A產品
factory->Create('B')->show(); 告訴工廠老闆,我要一個B產品
return 0;
}
缺點:現在我想要C,我也想要工廠能造C。麻煩了,工廠得造個生產C線來。
違反了開放封閉原則:軟件實體(類、模塊、函數)可以擴展,但是不可修改。
具體一點,如果要加C,那麼首先需要增加C類,這個屬於擴展是允許的。但此時工廠類中需要加判斷,這個就屬於修改,不允許。
工廠方法:
還是從前的哪個工廠老闆,你不是要C麼,你不是不讓我自己選麼。那行,我把A、B、C三個子工廠老闆給你找來了,你要啥直接問他們要。
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Product_C : public Product 擴增的!!!!!!
{
public:
void show()
{
cout << "Product_C" << endl;
}
};
-----------------------------------------------------------------------------------
class Factory 大工廠
{
public:
virtual Product* create() = 0;
};
class Factory_A : public Factory 小工廠A 有因爲虛函數,訪問了父生產線的A子生產線
{
public:
Product* create()
{
return new Product_A;
}
};
class Factory_B : public Factory 小工廠B
{
public:
Product* create()
{
return new Product_B;
}
};
class Factory_C : public Factory 小工廠C 擴增的!!!
{
public:
Product* create()
{
return new Product_C;
}
};
-----------------------------------------------------------------------------------
int main()
{
Factory* productA = new Factory_A(); 問大工廠要產品A 由於虛函數的關係,調用了小工廠A
Factory* productB = new Factory_B(); 問大工廠要產品B
Factory* productC = new Factory_C(); 問大工廠要產品C 擴增的!!!
productA->create()->show();
productB->create()->show();
productC->create()->show();
return 0;
}
擴增的我也寫上去了,發現確實沒有進行類內函數的修改,只是對類的擴增。
缺點:每增加一種產品,就需要增加一個對象的工廠。開小工廠不要錢的啊。
抽象工廠:
哇,開廠房要錢的啊,那我不開廠房了。就這倆廠房,我多加生產線行不。
//定義抽象類
class product1
{
public:
virtual void show() = 0;
};
//定義具體類
class product_A1 :public product1
{
public:
void show()
{
cout << "product A1" << endl;
}
};
class product_B1 :public product1
{
public:
void show()
{
cout << "product B1" << endl;
}
};
//定義抽象類
class product2
{
public:
virtual void show() = 0;
};
//定義具體類
class product_A2 :public product2
{
public:
void show()
{
cout << "product A2" << endl;
}
};
class product_B2 :public product2
{
public:
void show()
{
cout << "product B2" << endl;
}
};
-------------------------------------------------------------------------------------
class Factory
{
public:
virtual product1 *creat1() = 0;
virtual product2 *creat2() = 0;
};
class FactoryA :public Factory
{
public:
product1 *creat1()
{
return new product_A1();
}
product2 *creat2()
{
return new product_A2();
}
};
class FactoryB :public Factory
{
public:
product1 *creat1()
{
return new product_B1();
}
product2 *creat2()
{
return new product_B2();
}
};
------------------------------------------------------------------------------------
int main()
{
Factory *factoryA = new FactoryA();
factoryA->creat1()->show();
factoryA->creat2()->show();
Factory *factoryB = new FactoryB();
factoryB->creat1()->show();
factoryB->creat2()->show();
return 0;
}
我還沒有搞清楚的是,如果抽象工廠在要增加產品A2,不是也將改FactoryA。