建网站素材,wordpress调用分类栏目,游戏网站开发毕业设计,oa办公系统官网页版简介
设计模式是为了解决一些出现的问题设计的解决方案。是长时间经验的总结#xff0c;是根据不同问题从而提出并且实践出来的解决办法。使用不同的设计模式可以解决不同的问题。 设计模式可以分为三种大类别#xff1a;分别是创建型模式、结构型模式、行为型模式。
在开发…简介
设计模式是为了解决一些出现的问题设计的解决方案。是长时间经验的总结是根据不同问题从而提出并且实践出来的解决办法。使用不同的设计模式可以解决不同的问题。 设计模式可以分为三种大类别分别是创建型模式、结构型模式、行为型模式。
在开发中假设不使用设计模式可能会造成耦合度过高会造成一定的代码冗余而且可能会影响后续的开发进程合理的使用适合的设计模式会提高整体的灵活性降低后续的维护成本。
创建型模式顾名思义是处理对象创建的设计模式降低复杂度创建复杂对象时使用。
工厂模式
在类中实现一个接口创建指定对象使一个类的实例化延迟到了子类。简单来说把类的创建都封装起来只需要调用一个子类方法就可以实现类的创建并不会暴露创建类的逻辑。
以下为一个工厂类的实例 python代码如下
#原创不易多多支持
#Bloghttps://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代码如下
//原创不易多多支持
//Bloghttps://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* applefactory-getFruit(apple, 红,甜的);cout 苹果的味道 apple -color endl;
}从以上代码实例可以看出每次新建类只需要知道类的特定标号或者说特定的名称即可不需要关心类的实现逻辑在一定程度上减少了代码冗余减少了重复做功但是也暴露了一个问题每次要新增一个类都需要在工厂类中去实现逻辑也增强了之间的耦合度所有的类的创建都给了一个工厂去进行当你的类的种类繁多时你的代码会显得臃肿不堪所以设计模式需要考量当前项目需求是否符合此模式再进行使用。
抽象工厂模式
单独的一个工厂去创建所有类型的类会显得这个工厂臃肿不堪那么创建多个工厂不就ok了每个工厂都做一种类别的事情。就像上班一样有前端后端、UI、运维一样每个人都分工合作这样就很有调理了。
抽象工厂很好的解决了这个问题。以下为抽象工厂示例代码。 以下代码从以上的示例代码基础上增加了绘制
根据上面代码进行修改我们定义几个食物基类Food定义其它几个类别例如水果、主食以及蔬菜这是几种不同类别的食物。再定3个工厂分别用来处理不同的食物这样就不用担心所有的类都放一个工厂进行处理了最后再定义一个抽象工厂类这个类对所有的工厂进行一个统一封装这样就实现了抽象工厂类。
python代码
#抽象工厂主工厂
#原创不易多多支持
#Bloghttps://me.csdn.net/A757291228
class MainFactory:def getFactory(self,factory_name):if factory_nameFruit:return FruitFactory()elif factory_nameVegetables:return VegetablesFactory()elif factory_nameStaple: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()fruitFactorymainFactory.getFactory(Fruit)applefruitFactory.getFruit(apple, green, sweet)print(苹果的颜色,apple.color,苹果的口味,apple.taste)
C实现
#include iostream
using namespace std;
//原创不易多多支持
//Bloghttps://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的觉得麻烦就没写了后面有时间就补)