Design Pattern Implementer | Python

Factory Method Pattern Overview

Illustrates the Factory Method Pattern in Python implementing shape creation logic decoupled from client code using Shape, Circle, Square classes and ShapeFactory. Promotes loose coupling, scalability, and adherence to OOP principles.


Empty image or helper icon

Prompt

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def draw(self):
        pass

class Circle(Shape):
    def draw(self):
        print("Circle: draw() method")

class Square(Shape):
    def draw(self):
        print("Square: draw() method")

class ShapeFactory:
    def create_shape(self, shape_type):
        if shape_type == 'Circle':
            return Circle()
        elif shape_type == 'Square':
            return Square()
        else:
            return None

# Client code
shape_factory = ShapeFactory()
circle = shape_factory.create_shape('Circle')
circle.draw()

square = shape_factory.create_shape('Square')
square.draw()

Answer

Design Pattern: Factory Method Pattern

Problem/Code Analysis:

  • The provided code snippet demonstrates the Factory Method Pattern where the ShapeFactory class is responsible for creating instances of different shapes (Circle and Square) based on a given shape type.
  • The ShapeFactory acts as a Factory class that encapsulates the object creation logic, decoupling the client code from the specific classes being created.

Code Template - Python:

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def draw(self):
        pass

class Circle(Shape):
    def draw(self):
        print("Circle: draw() method")

class Square(Shape):
    def draw(self):
        print("Square: draw() method")

class ShapeFactory:
    def create_shape(self, shape_type):
        if shape_type == 'Circle':
            return Circle()
        elif shape_type == 'Square':
            return Square()
        else:
            return None

# Client code
shape_factory = ShapeFactory()
circle = shape_factory.create_shape('Circle')
circle.draw()

square = shape_factory.create_shape('Square')
square.draw()

Implementation Details:

  • Factory Method Pattern: Offers an interface for creating instances of a class, but allows subclasses to alter the type of objects that will be created.
  • ShapeFactory: Encapsulates the creation logic for different shapes, providing a method create_shape() to instantiate specific shapes.
  • Shape, Circle, Square: Represents the hierarchy of Shape classes where Circle and Square extend the abstract class Shape and implement the draw method.
  • Client Code: Requests shape instances from the ShapeFactory without knowing the specific class that will be created, promoting flexibility and maintainability.

Comments:

  • The Factory Method Pattern promotes loose coupling by delegating the responsibility of object creation to a separate class (ShapeFactory).
  • It enhances code scalability as new shape classes can be added easily without modifying the existing client code.
  • The use of abstract classes ensures a common interface for different shapes, enabling polymorphic behavior.

By following the Factory Method Pattern, the code structure is organized, maintainable, and adheres to the principles of object-oriented design.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

Illustrates the Factory Method Pattern in Python implementing shape creation logic decoupled from client code using Shape, Circle, Square classes and ShapeFactory. Promotes loose coupling, scalability, and adherence to OOP principles.