객체지향 설계를 하다 보면, 인스턴스를 직접 생성하지 않고 필요한 객체를 상황에 맞게 생성해주는 패턴이 자주 등장한다. 오늘은 그중 대표적인 디자인 패턴인 Factory Pattern에 대해 예제 코드와 함께 정리할 예정이다.
Factory Pattern은 객체 생성 로직을 별도의 클래스나 메서드로 분리하여, 유연하고 확장 가능한 구조를 만드는 디자인 패턴이다.
즉, 직접 클래스를 생성하지 않고, 팩토리 메서드를 통해 객체를 생성한다. 이로 인해 코드를 바꾸지 않고도 새로운 클래스를 쉽게 추가하거나 교체할 수 있게 된다.
객체 생성의 책임을 별도의 팩토리(Factory) 클래스로 위임한다고 이해할 수 있는데, 이는 객체 생성 방식을 캡슐화(숨김)하고, 필요한 객체를 상황에 맞게 유연하게 생성할 수 있도록 해준다.
from abc import ABCMeta, abstractstaticmethod
class IPerson(metaclass=ABCMeta):
@abstractstaticmethod
def person_method():
""" Interface Method """
IPerson
은 추상 클래스이며, person_method()
는 반드시 구현해야 한다.class Student(IPerson):
def __init__(self):
self.name = "Basic Student Name"
def person_method(self):
print("I am a student")
class Teacher(IPerson):
def __init__(self):
self.name = "Basic Teacher Name"
def person_method(self):
print("I am a teacher")
IPerson
을 상속받고 person_method()
를 구현해야만 인스턴스 생성이 가능하다.class PersonFactory:
@staticmethod
def build_person(person_type):
if person_type == "Student":
return Student()
if person_type == "Teacher":
return Teacher()
print("Invalid Type")
return -1
Student
나 Teacher
를 직접 모르더라도 build_person()
만 사용하면 됨if __name__ == "__main__":
choice = input("What type of person do you want to create?\n")
person = PersonFactory.build_person(choice)
if person != -1:
person.person_method()
코드 실행 예시
What type of person do you want to create?
Student
I am a student
What type of person do you want to create?
Teacher
I am a teacher
What type of person do you want to create?
Alien
Invalid Type
@abstractmethod
는 Python에서 인터페이스(Interface) 또는 템플릿 역할을 하고자 할 때 핵심적으로 사용되는 기능이다.
객체지향 프로그래밍에서 추상 클래스(abstract class) 는 일부 메서드만 정의된 상태로, 자식 클래스가 반드시 오버라이딩해서 구현해야 할 메서드를 강제하고 싶을 때 사용된다.
즉, 자식 클래스가 반드시 구현해야 하는 메서드를 정의하는 데 사용하는 데코레이터이다.
from abc import ABC, abstractmethod
class Animal(ABC): # ABC를 상속받아야 추상 클래스가 됨
@abstractmethod
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("Woof!")
class Cat(Animal):
def speak(self):
print("Meow")
dog = Dog()
dog.speak() # Woof!
이때 발생하는 에러는 TypeError: Can't instantiate abstract class Dog with abstract methods speak
추상 메서드를 정적 메서드로 만들 수 있음
from abc import ABCMeta, abstractstaticmethod
class Factory(metaclass=ABCMeta):
@abstractstaticmethod
def create():
pass
@abstractmethod
는 자식 클래스가 꼭 구현해야 할 메서드를 강제함으로써 코드 구조의 일관성과 안정성을 높여주는 도구이다.
@abstractmethod
: 자식 클래스에서 반드시 구현해야 하는 메서드 정의ABC
/ ABCMeta
: 추상 클래스를 정의할 때 사용하는 베이스Factory Pattern은 "어떤 객체를 만들지 결정하는 책임"을 객체 외부로 분리하는 디자인 패턴으로 유지보수성과 확장성이 매우 뛰어나다.