日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

(创建模式 上)设计模式——工厂、抽象工厂 C++/Python3实现

發布時間:2023/12/4 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (创建模式 上)设计模式——工厂、抽象工厂 C++/Python3实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

設計模式是為了解決一些出現的問題設計的解決方案。是長時間經驗的總結,是根據不同問題從而提出并且實踐出來的解決辦法。使用不同的設計模式可以解決不同的問題。
設計模式可以分為三種大類別:分別是創建型模式、結構型模式、行為型模式。

在開發中,假設不使用設計模式,可能會造成耦合度過高,會造成一定的代碼冗余,而且可能會影響后續的開發進程;合理的使用適合的設計模式會提高整體的靈活性,降低后續的維護成本。

創建型模式顧名思義是處理對象創建的設計模式,降低復雜度,創建復雜對象時使用。

工廠模式

在類中實現一個接口創建指定對象,使一個類的實例化延遲到了子類。簡單來說把類的創建都封裝起來,只需要調用一個子類方法就可以實現類的創建,并不會暴露創建類的邏輯。

以下為一個工廠類的實例:
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.colordef get_taste(self):return self.tasteclass Apple(Fruit):def __init__(self,color,taste):print("蘋果初始化")self.color = colorself.taste = tasteclass Banana(Fruit):def __init__(self,color,taste):print("香蕉初始化")self.color = colorself.taste = tasteif __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.colordef get_taste(self):return self.tasteclass Vegetables(Food):def __init__(self):print("蔬菜類初始化")def get_color(self):return self.colordef get_taste(self):return self.tasteclass Staple(Food):def __init__(self):print("主食類初始化")def get_color(self):return self.colordef get_taste(self): return self.taste #具體類別 class Apple(Fruit):def __init__(self,color,taste):print("蘋果初始化")self.color = colorself.taste = taste class Rice(Staple):def __init__(self,color,taste):print("米飯初始化")self.color = colorself.taste = tasteclass Carrot(Vegetables):def __init__(self,color,taste):print("紅蘿卜初始化")self.color = colorself.taste = tasteif __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的,覺得麻煩就沒寫了后面有時間就補)

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的(创建模式 上)设计模式——工厂、抽象工厂 C++/Python3实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。