简介
设计模式是为了解决一些出现的问题设计的解决方案。是长时间经验的总结,是根据不同问题从而提出并且实践出来的解决办法。使用不同的设计模式可以解决不同的问题。
设计模式可以分为三种大类别:分别是创建型模式、结构型模式、行为型模式。
在开发中,假设不使用设计模式,可能会造成耦合度过高,会造成一定的代码冗余,而且可能会影响后续的开发进程;合理的使用适合的设计模式会提高整体的灵活性,降低后续的维护成本。
创建型模式顾名思义是处理对象创建的设计模式,降低复杂度,创建复杂对象时使用。
工厂模式
在类中实现一个接口创建指定对象,使一个类的实例化延迟到了子类。简单来说把类的创建都封装起来,只需要调用一个子类方法就可以实现类的创建,并不会暴露创建类的逻辑。
以下为一个工厂类的实例:
python代码如下:
#原创不易多多支持#Blog:https://me.csdn.net/A757291228class 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.tasteclass Apple(Fruit): def __init__(self,color,taste): print("苹果初始化") self.color = color self.taste = tasteclass Banana(Fruit): def __init__(self,color,taste): print("香蕉初始化") self.color = color self.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/A757291228class 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.tasteclass 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 = tasteclass Rice(Staple): def __init__(self,color,taste): print("米饭初始化") self.color = color self.taste = tasteclass Carrot(Vegetables): def __init__(self,color,taste): print("红萝卜初始化") self.color = color self.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的,觉得麻烦就没写了后面有时间就补)