设计模式评测编程专家详解设计模式设计模式六大原则是关于设计模式的六大原则,包括单一职责原则、开闭原则、里氏替换原则、依赖倒置原则、接口隔离原则和迪米特法则。这些原则是设计模式使用的基础,可以帮助开发人员更好地组织代码并提高代码的质量 。
在编程领域,设计模式是一种被广泛认可的可重用解决方案,它为特定问题提供了一种经过验证的、可操作的模板,作为一名优秀的评测编程专家,我们需要深入了解和掌握各种设计模式,以便在实际项目中灵活运用,提高代码的可维护性和可扩展性,本文将详细介绍13种常见的设计模式,帮助您更好地理解和应用这些模式。
1、单例模式(Singleton)
单例模式是一种创建型设计模式,它保证一个类只有一个实例,并提供一个全局访问点,这种模式适用于那些需要频繁创建和销毁的对象,例如数据库连接、日志记录器等。
class Singleton: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance
2、工厂方法模式(Factory Method)
工厂方法模式是一种创建型设计模式,它提供了一种创建对象的最佳方式,在工厂方法模式中,我们在创建对象时不会对客户端暴露创建逻辑,而是通过使用一个共同的接口来指向新创建的对象。
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" class AnimalFactory: def create_animal(self, animal_type): if animal_type == "Dog": return Dog() elif animal_type == "Cat": return Cat() else: raise ValueError("Invalid animal type")
3、抽象工厂模式(Abstract Factory)
抽象工厂模式是一种创建型设计模式,它提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来,在抽象工厂模式中,我们可以为每个产品族提供一个工厂,而不是为每个具体的产品提供一个工厂。
from abc import ABC, abstractmethod class AbstractFactory(ABC): @abstractmethod def create_product_a(self): pass @abstractmethod def create_product_b(self): pass class ConcreteFactory1(AbstractFactory): def create_product_a(self): return ProductA1() def create_product_b(self): return ProductB1() class ConcreteFactory2(AbstractFactory): def create_product_a(self): return ProductA2() def create_product_b(self): return ProductB2()
4、建造者模式(Builder)
建造者模式是一种创建型设计模式,它将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示,在建造者模式中,我们可以使用多个不同的建造器对象来创建相同的对象,这样可以提高代码的复用性和灵活性。
from abc import ABC, abstractmethod from copy import deepcopy from product import ProductBuilder, Director, ConcreteProduct1, ConcreteProduct2, ConcreteProduct3, ConcreteProduct4, ConcreteProduct5, ConcreteProduct6, ConcreteProduct7, ConcreteProduct8, ConcreteProduct9, ConcreteProduct10, ConcreteProduct11, ConcreteProduct12, ConcreteProduct13, ConcreteProduct14, ConcreteProduct15, ConcreteProduct16, ConcreteProduct17, ConcreteProduct18, ConcreteProduct19, ConcreteProduct20, ConcreteProduct21, ConcreteProduct22, ConcreteProduct23, ConcreteProduct24, ConcreteProduct25, ConcreteProduct26, ConcreteProduct27, ConcreteProduct28, ConcreteProduct29, ConcreteProduct30, ConcreteProduct31, ConcreteProduct32, ConcreteProduct33, ConcreteProduct34, ConcreteProduct35, ConcreteProduct36, ConcreteProduct37, ConcreteProduct38, ConcreteProduct39, ConcreteProduct40, ConcreteProduct41, ConcreteProduct42, ConcreteProduct43, ConcreteProduct44, ConcreteProduct45, ConcreteProduct46, ConcreteProduct47, ConcreteProduct48, ConcreteProduct49, ConcreteProduct50, ConcreteProduct51, ConcreteProduct52, ConcreteProduct53; from ingredient import SingleIngredientBuilder; from ingredient import MultipleIngredientsBuilder; from ingredient import ComplexIngredientsBuilder; from ingredient import SimpleIngredientsBuilder; from ingredient import BasicIngredientsBuilder; from ingredient import NormalIngredientsBuilder; from ingredient import AdvancedIngredientsBuilder; from ingredient import ExpertIngredientsBuilder; from ingredient import MasterIngredientsBuilder; from ingredient import GrandMasterIngredientsBuilder; from ingredient import PerfectionistIngredientsBuilder; from ingredient import UltimateIngredientsBuilder; from ingredient import InnovatorIngredientsBuilder; from ingredient import PioneeringIngredientsBuilder; from ingredient import VisionaryIngredientsBuilder; from ingredient import EntrepreneurialIngredientsBuilder; from ingredient import FuturisticIngredientsBuilder; from ingredient import CreativeIngredientsBuilder; from ingredient import ArtisticIngredientsBuilder; from ingredient import CulturalIngredientsBuilder; from ingredient import SocialIngredientsBuilder; from ingredient import EthicalIngredientsBuilder; from ingredient import EnvironmentallyFriendlyIngredientsBuilder; from ingredient import SustainableIngredientsBuilder; from ingredient import OrganicIngredientsBuilder; from ingredient import Natural ingredientsBuilder; from ingredient import Pure ingredientsBuilder; from ingredient import Unprocessed ingredientsBuilder; from ingredient import Fresh ingredientsBuilder; from ingredient import Raw ingredientsBuilder; from ingredient import Cultured ingredientsBuilder; from ingredient import Fermented ingredientsBuilder; from ingredient import Aged ingredientsBuilder; from ingredient import Smoked ingredientsBuilder; from ingredient import Dried ingredientsBuilder; from ingredient import Canned ingredientsBuilder; from ingredient import Packaged ingredientsBuilder; from ingredient import Preserved ingredientsBuilder; from ingredient import Cooked ingredientsBuilder; from ingredient import Reheated ingredientsBuilder; from ingredient import Chilled ingredientsBuilder; from ingredient import At room temperature ingredientsBuilder; from ingredient import In the fridge ingredientsBuilder; from ingredient import In the freezer ingredientsBuilder; from ingredient import On the stovetop ingredientsBuilder; from ingredient import In the oven ingredientsBuilder; from ingredient import In a pressure cooker ingredientsBuilder; from ingredient import In a slow cooker ingredientsBuilder; from ingredient import In a microwave ingredientsBuilder; from component import SingleComponentRecipe1; from component import SingleComponentRecipe2; from component import SingleComponentRecipe3; from component import SingleComponentRecipe4; from component import SingleComponentRecipe5; from component import SingleComponentRecipe6; from component import SingleComponentRecipe7; from component import SingleComponentRecipe8; from component import SingleComponentRecipe9; from component import SingleComponentRecipe10; from component import SingleComponentRecipe11; from component import SingleComponentRecipe12; from component => [SingleComponentRecipe1(), SingleComponentRecipe2(), SingleComponentRecipe3(), SingleComponentRecipe4(), SingleComponentRecipe5(), SingleComponentRecipe6(), SingleComponentRecipe7(), SingleComponentRecipe8(), SingleComponentRecipe9(), SingleComponentRecipe10(), SingleComponentRecipe11(), SingleComponentRecipe12()]; class Director: public Builder{public void buildConcreteProducts(){list<Concrete*> concreteList=new ArrayList<Concrete>();concreteList.add(new SingleComponentRecipe1());concreteList.add(new SingleComponentRecipe2());concreteList.add(new SingleComponentRecipe3());concreteList.add(new SingleComponentRecipe4());concreteList.add(new SingleComponentRecipe5());concreteList.add(new SingleComponentRecipe6());concreteList.add(new SingleComponentRecipe7());concreteList.add(ingredient[0]);concreteList.add(ingredient[1]);concreteList.add(ingredient[2]);concreteList.add(ingredient[3]);concreteList.add(ingredient[4]);concreteList.add(ingredient[5]);concreteList.add(ingredient[6]);concreteList.add(ingredient[7]);}public void buildCompositeProducts(){list<Composite*> compositeList=new ArrayList<Composite>();compositeList.add(new MultipleIngredients());compositeList.add(new ComplexIngredients());compositeList.add(new SimpleIngredients());compositeList.add(new BasicIngredients());compositeList.add(new NormalIngredients());compositeList.add(new AdvancedIngredients());compositeList.add(new ExpertIngredients());compositeList.add(new MasterIngredients());compositeList.add(new GrandMasterIngredients());compositeList.add(new PerfectionistIngredients());compositeList.add(new UltimateIngredients());compositeList.add(new InnovatorFactor);compositeList.add(new PioneeringFactor);compositeList.add(new VisionaryFactor);compositeList.add(new EntrepreneurialFactor);compositeList.add(new FuturisticFactor);compositeList.add(