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.
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()
create_shape()
to instantiate specific shapes.By following the Factory Method Pattern, the code structure is organized, maintainable, and adheres to the principles of object-oriented design.
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.