簡介
設計模式是爲了解決一些出現的問題設計的解決方案。是長時間經驗的總結,是根據不同問題從而提出並且實踐出來的解決辦法。使用不同的設計模式可以解決不同的問題。
設計模式可以分爲三種大類別:分別是創建型模式、結構型模式、行爲型模式。
在開發中,假設不使用設計模式,可能會造成耦合度過高,會造成一定的代碼冗餘,而且可能會影響後續的開發進程;合理的使用適合的設計模式會提高整體的靈活性,降低後續的維護成本。
創建型模式顧名思義是處理對象創建的設計模式,降低複雜度,創建複雜對象時使用。
工廠模式
在類中實現一個接口創建指定對象,使一個類的實例化延遲到了子類。簡單來說把類的創建都封裝起來,只需要調用一個子類方法就可以實現類的創建,並不會暴露創建類的邏輯。
以下爲一個工廠類的實例:
python代碼如下:
#原創不易多多支持
#Blog:https://me.csdn.net/A757291228
class Factory:
def getFruit(self, name, color, taste ):
if name == 'apple':
return Apple(color,taste)
class Fruit:
def __init__(self):
print("水果類初始化")
def get_color(self):
return self.color
def get_taste(self):
return self.taste
class Apple(Fruit):
def __init__(self,color,taste):
print("蘋果初始化")
self.color = color
self.taste = taste
class Banana(Fruit):
def __init__(self,color,taste):
print("香蕉初始化")
self.color = color
self.taste = taste
if __name__ == '__main__':
factory = Factory()
appleClass = factory.getFruit("apple", "green", "sweet")
print('蘋果的顏色',appleClass.get_color(),'蘋果的味道',appleClass.get_taste())
C++代碼如下:
//原創不易,多多支持
//Blog:https://me.csdn.net/A757291228
#include <iostream>
using namespace std;
class Fruit {
public:
string color;
string taste;
Fruit() {
}
};
class Apple : public Fruit {
public:
Apple(string color, string taste) {
this->color = color;
this->taste = taste;
cout << "蘋果類創建"<< endl;
}
};
class Factory {
public:
Fruit* getFruit(string name, string color, string taste)
{
if (name == "apple") {
return new Apple(color, taste);
}
}
};
int main(){
Factory* factory = new Factory();
Fruit* apple=factory->getFruit("apple", "紅","甜的");
cout << "蘋果的味道"<< apple ->color<< endl;
}
從以上代碼實例可以看出,每次新建類,只需要知道類的特定標號,或者說特定的名稱即可,不需要關心類的實現邏輯,在一定程度上減少了代碼冗餘,減少了重複做功;但是,也暴露了一個問題:每次要新增一個類,都需要在工廠類中去實現邏輯,也增強了之間的耦合度,所有的類的創建都給了一個工廠去進行,當你的類的種類繁多時,你的代碼會顯得臃腫不堪,所以設計模式需要考量當前項目需求是否符合此模式再進行使用。
抽象工廠模式
單獨的一個工廠去創建所有類型的類會顯得這個工廠臃腫不堪,那麼創建多個工廠不就ok了?每個工廠都做一種類別的事情。就像上班一樣,有前端後端、UI、運維一樣,每個人都分工合作,這樣就很有調理了。
抽象工廠很好的解決了這個問題。以下爲抽象工廠示例代碼。
以下代碼從以上的示例代碼基礎上,增加了繪製;
根據上面代碼進行修改,我們定義幾個食物基類Food;定義其它幾個類別,例如水果、主食以及蔬菜,這是幾種不同類別的食物。再定3個工廠,分別用來處理不同的食物,這樣就不用擔心所有的類都放一個工廠進行處理了,最後再定義一個抽象工廠類,這個類對所有的工廠進行一個統一封裝,這樣就實現了抽象工廠類。
python代碼:
#抽象工廠,主工廠
#原創不易,多多支持
#Blog:https://me.csdn.net/A757291228
class MainFactory:
def getFactory(self,factory_name):
if factory_name=='Fruit':
return FruitFactory()
elif factory_name=='Vegetables':
return VegetablesFactory()
elif factory_name=='Staple':
return StapleFactory()
#工廠類
class FruitFactory:
def getFruit(self, name, color, taste ):
if name == 'apple':
return Apple(color,taste)
class VegetablesFactory:
def getFruit(self, name, color, taste ):
if name == 'carrot':
return Carrot(color,taste)
class StapleFactory:
def getFruit(self, name, color, taste ):
if name == 'rice':
return Rice(color,taste)
#Food 基類
class Food:
def __init__(self,taste):
print("食物類初始化")
self.taste = taste
#水果主食和蔬菜三個基類
class Fruit(Food):
def __init__(self):
print("水果類初始化")
def get_color(self):
return self.color
def get_taste(self):
return self.taste
class Vegetables(Food):
def __init__(self):
print("蔬菜類初始化")
def get_color(self):
return self.color
def get_taste(self):
return self.taste
class Staple(Food):
def __init__(self):
print("主食類初始化")
def get_color(self):
return self.color
def get_taste(self):
return self.taste
#具體類別
class Apple(Fruit):
def __init__(self,color,taste):
print("蘋果初始化")
self.color = color
self.taste = taste
class Rice(Staple):
def __init__(self,color,taste):
print("米飯初始化")
self.color = color
self.taste = taste
class Carrot(Vegetables):
def __init__(self,color,taste):
print("紅蘿蔔初始化")
self.color = color
self.taste = taste
if __name__ == '__main__':
mainFactory = MainFactory()
fruitFactory=mainFactory.getFactory('Fruit')
apple=fruitFactory.getFruit("apple", "green", "sweet")
print('蘋果的顏色:',apple.color,'蘋果的口味:',apple.taste)
C++實現:
#include <iostream>
using namespace std;
//原創不易,多多支持
//Blog:https://me.csdn.net/A757291228
//食物基類
class Food {
public:
string color;
string taste;
Food() {
}
};
//三個類別
class Fruit :public Food {
public:
Fruit() {
}
};
class Vegetables :public Food {
public:
Vegetables() {
}
};
class Staple :public Food {
public:
Staple() {
}
};
//三個具體類
class Apple : public Fruit {
public:
Apple(string color, string taste) {
this->color = color;
this->taste = taste;
cout << "蘋果類創建" << endl;
}
};
class Rice : public Staple {
public:
Rice(string color, string taste) {
this->color = color;
this->taste = taste;
cout << "蘋果類創建" << endl;
}
};
class Carrot : public Vegetables {
public:
Carrot(string color, string taste) {
this->color = color;
this->taste = taste;
cout << "蘋果類創建" << endl;
}
};
//工廠基類
class Factory {
public:
virtual Food* getFood()
{
}
};
//具體工廠類
class FruitFactory {
public:
Food* getFood(string name, string color, string taste)
{
if (name == "apple") {
return new Apple(color, taste);
}
}
};
class StapleFactory {
public:
Food* getFood(string name, string color, string taste)
{
if (name == "apple") {
return new Apple(color, taste);
}
}
};
class VegetablesFactory {
public:
Fruit* getFood(string name, string color, string taste)
{
if (name == "apple") {
return new Apple(color, taste);
}
}
};
//主工廠,抽象工廠
class MainFactory {
public:
FruitFactory* getFactory(string factory_name)
{
if (factory_name == "Fruit") {
return new FruitFactory();
}
}
};
int main() {
MainFactory* main_factory = new MainFactory();
FruitFactory* fruit = main_factory->getFactory("Fruit");
Food* apple = fruit->getFood("apple", "紅", "甜甜的");
cout << "蘋果的味道" << apple->taste << endl;
}
以上代碼都通過了抽象工廠進行統一的不同屬性的工廠調用,增強了邏輯和結構。
看到這裏了點個讚唄~(本來說要加C#和Java的,覺得麻煩就沒寫了後面有時間就補)