[java] 추상 팩토리 패턴

추상 팩토리 패턴은 객체 생성에 관한 패턴으로, 관련된 객체들을 생성하기 위한 인터페이스를 제공합니다. 이 패턴은 구체적인 클래스의 인스턴스화를 피하며, 코드의 유연성과 확장성을 높이는 데 도움을 줍니다.

동기

소프트웨어 디자인에서, 추상 팩토리 패턴은 객체의 구상 클래스를 노출시키지 않고 공통 인터페이스를 사용하여 관련 객체 집합을 생성할 수 있도록 합니다. 이를 통해 시스템이 서로 다른 구상 클래스를 사용하더라도 호환성을 유지할 수 있습니다.

예제

다음은 자바를 사용한 간단한 추상 팩토리 패턴의 예제입니다.

public interface Shape {
   void draw();
}

public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

public class Square implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

public interface Color {
   void fill();
}

public class Red implements Color {
   @Override
   public void fill() {
      System.out.println("Inside Red::fill() method.");
   }
}

public class Green implements Color {
   @Override
   public void fill() {
      System.out.println("Inside Green::fill() method.");
   }
}

public abstract class AbstractFactory {
   public abstract Color getColor(String color);
   public abstract Shape getShape(String shape) ;
}

public class ShapeFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType) {
      if (shapeType == null) {
         return null;
      }        
      if (shapeType.equalsIgnoreCase("RECTANGLE")) {
         return new Rectangle();
      } else if (shapeType.equalsIgnoreCase("SQUARE")) {
         return new Square();
      }
      return null;
   }

   @Override
   public Color getColor(String color) {
      return null;
   }
}

public class ColorFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType) {
      return null;
   }

   @Override
   public Color getColor(String color) {
      if (color == null) {
         return null;
      }        
      if (color.equalsIgnoreCase("RED")) {
         return new Red();
      } else if (color.equalsIgnoreCase("GREEN")) {
         return new Green();
      }
      return null;
   }
}

public class FactoryProducer {
   public static AbstractFactory getFactory(String choice){
      if(choice.equalsIgnoreCase("SHAPE")){
         return new ShapeFactory();
      } else if(choice.equalsIgnoreCase("COLOR")){
         return new ColorFactory();
      }
      return null;
   }
}

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {
      // Get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

      // Get an object of Shape Rectangle
      Shape shape1 = shapeFactory.getShape("RECTANGLE");

      // Call draw method of Shape Rectangle
      shape1.draw();

      // Get an object of Shape Square
      Shape shape2 = shapeFactory.getShape("SQUARE");

      // Call draw method of Shape Square
      shape2.draw();

      // Get color factory
      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

      // Get an object of Color Red
      Color color1 = colorFactory.getColor("RED");

      // Call fill method of Red
      color1.fill();

      // Get an object of Color Green
      Color color2 = colorFactory.getColor("Green");

      // Call fill method of Green
      color2.fill();
   }
}

요약

추상 팩토리 패턴은 관련된 객체를 생성하는 인터페이스를 제공합니다. 이로써 클라이언트는 구체적인 구현 클래스를 알 필요 없이 객체를 생성할 수 있습니다. 코드를 수정하지 않고 새로운 유형의 객체 집합을 쉽게 추가할 수 있도록 해주는 패턴입니다.