synopsis
The object factory pattern is a design pattern that is used to create instances of objects. There are several common object factory patterns in Python, including factory methods, abstract factories, and simple factories. These patterns are intended to provide more flexible ways to instantiate objects, making code easier to maintain and extend.
Pros:
Encapsulate the details of object creation to reduce coupling.
Provides flexibility and makes object creation more manageable.
The way objects are created can be easily extended and modified according to requirements.
factory method pattern
The factory method pattern is based on inheritance and uses an abstract class to define the interface for creating objects, with its subclasses determining the actual objects created.
Idea Description:
The factory method pattern allows the base class to have an abstract factory method, which is implemented by concrete subclasses that create objects based on specific requirements.
Code Example:
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def make_sound(self): pass class Dog(Animal): def make_sound(self): return "Woof!" class Cat(Animal): def make_sound(self): return "Meow!" class AnimalFactory(ABC): @abstractmethod def create_animal(self): pass class DogFactory(AnimalFactory): def create_animal(self): return Dog() class CatFactory(AnimalFactory): def create_animal(self): return Cat() dog_factory = DogFactory() cat_factory = CatFactory() dog = dog_factory.create_animal() cat = cat_factory.create_animal() print(dog.make_sound()) # Output: Woof! print(cat.make_sound()) # exports: Meow!
Abstract Factory Pattern
The Abstract Factory pattern allows the creation of a set of related objects without specifying a concrete class.
Idea Description:
The Abstract Factory pattern is used to create a set of related objects, such as the creation of different products in a family.
Code Example:
from abc import ABC, abstractmethod # Abstract factory - Shape factory class ShapeFactory(ABC): @abstractmethod def create_circle(self): pass @abstractmethod def create_square(self): pass # Specific plants - round plants class CircleFactory(ShapeFactory): def create_circle(self): return Circle() def create_square(self): return None # Specific plants - square plants class SquareFactory(ShapeFactory): def create_circle(self): return None def create_square(self): return Square() # Abstract Products - Shapes class Shape(ABC): @abstractmethod def draw(self): pass # Specific products - round class Circle(Shape): def draw(self): return "Drawing Circle" # Specific products - square class Square(Shape): def draw(self): return "Drawing Square" # Create objects using abstract factories def draw_shapes(factory): circle = factory.create_circle() square = factory.create_square() print(() if circle else "Circle not supported") print(() if square else "Square not supported") # Create shapes using different factories draw_shapes(CircleFactory()) # exports: Drawing Circle / Circle not supported draw_shapes(SquareFactory()) # exports: Circle not supported / Drawing Square
This example demonstrates the Abstract Factory Pattern with two factory classes that create circles and squares. Each factory class can create a corresponding shape object and return the corresponding shape object based on the factory type.
Simple Factory Pattern
The Simple Factory pattern creates objects using a factory class that determines the object type by given conditions.
Idea Description:
The Simple Factory Pattern applies to creating objects based on conditions, but is not one of the 23 design patterns.
Code Example:
class Shape: def draw(self): pass # Specific products - round class Circle(Shape): def draw(self): return "Drawing Circle" # Specific products - square class Square(Shape): def draw(self): return "Drawing Square" # Simple factories class SimpleShapeFactory: def create_shape(self, shape_type): if shape_type == "circle": return Circle() elif shape_type == "square": return Square() else: raise ValueError("Invalid shape type") # Create objects using simple factories factory = SimpleShapeFactory() circle = factory.create_shape("circle") square = factory.create_shape("square") print(()) # Output: Drawing Circle print(()) # Output: Drawing Square
This example demonstrates the Simple Factory pattern. Here, SimpleShapeFactory creates the corresponding concrete product object based on the parameter type.
application scenario
The factory pattern is suitable for the following scenarios:
When object creation involves complex logic or conditions.
When there is a need to isolate the object creation logic.
summarize
The factory pattern provides different strategies to manage the creation of objects. By encapsulating the details of instantiation in separate classes, code is easier to maintain, extend, and modify. Choosing the appropriate factory pattern depends on specific project requirements and design considerations.
The above is Python's multiple object factory model to facilitate code maintenance and expansion of the details, more information about Python's multiple object factory model, please pay attention to my other related articles!