MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Java Design Patterns | Factory Method
  1. Overview and Definition
  2. Class Diagram
  3. Implementation Example
    • The Product
    • The Concrete Product
    • The Factory
    • Testing the Factory Method Pattern

  1. Overview and Definition
    The Factory Method pattern is a creational design pattern that provides an interface for creating objects without specifying their exact classes. This pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

    Key Components:
    • Product: An interface or abstract class that defines the common interface for objects that the factory method creates.
    • Concrete Product: Specific implementations of the Product interface that provide actual functionality.
    • Creator (Factory): An abstract class that defines the factory method. Concrete subclasses implement this method to create specific Product objects.
    • Client: The code that uses the factory to create objects without knowing the specific implementation details.

    Benefits:
    • Loose Coupling: Client code doesn't need to know about specific concrete classes, only the Product interface.
    • Extensibility: New product types can be added easily by creating new concrete products and updating the factory method.
  2. Class Diagram
    Factory Method Pattern Components:
    • Product: Report interface - defines the common interface for objects created by the factory.
    • Concrete Product: HtmlReport class - implements the Product interface with specific functionality.
    • Creator: ReportFactory abstract class - declares the factory method that returns Product objects.
    • Concrete Creator: HtmlReportFactory class - implements the factory method to create specific Product instances.
    ┌────────────────────────────┐
    │       <<interface>>        │
    │          Report            │  ← Product
    ├────────────────────────────┤
    │ + generate(String): String │
    └────────────────────────────┘
                △
                │ implements
                │
    ┌────────────────────────────┐
    │         HtmlReport         │  ← Concrete Product
    ├────────────────────────────┤
    │ + generate(String): String │
    └────────────────────────────┘
    
    
    ┌─────────────────────────┐
    │      <<abstract>>       │
    │      ReportFactory      │  ← Creator
    ├─────────────────────────┤
    │ + createReport(): Report│
    └─────────────────────────┘
                △
                │ extends
                │
    ┌─────────────────────────┐
    │    HtmlReportFactory    │  ← Concrete Creator
    ├─────────────────────────┤
    │ + createReport(): Report│
    └─────────────────────────┘
    
  3. Implementation Example
    • The Product:
      The Product interface defines the contract that all concrete products must follow. In this example, we create a Report interface that can generate reports from input data.
      public interface Report {
          public String generate(final String data);
      }
      
    • The Concrete Product:
      The Concrete Product implements the Product interface and provides specific functionality. Here, HtmlReport converts CSV data into HTML format with bold formatting for each data element.
      public class HtmlReport implements Report {
          public String generate(final String csvData) {
              final StringBuilder htmlData = new StringBuilder();
      
              if (csvData != null) {
                  final String[] dataElements = csvData.split(",");
      
                  for (final String dataElement : dataElements) {
                      htmlData.append(String.format("<b>%s</b>", dataElement));
                  }
              }
      
              return htmlData.toString();
          }
      }
      
    • The Factory:
      The abstract Factory class defines the factory method that subclasses will implement to create specific Product objects. Each concrete factory knows which specific product to create.

      Creator:
      public abstract class ReportFactory {
          public abstract Report createReport();
      }
      
      Concrete Creator:
      public class HtmlReportFactory extends ReportFactory {
          public Report createReport() {
              return new HtmlReport();
          }
      }
      
    • Testing the Factory Method Pattern:
      The client code demonstrates how to use the concrete factory to create objects. Each factory type knows which specific product to create, promoting loose coupling and extensibility.
      public class FactoryMethodPatternTest {
          public static void main(String[] args) {
              // create the factory for HTML reports
              final ReportFactory factory = new HtmlReportFactory();
              
              // use the factory to create the report
              final Report report = factory.createReport();
      
              // generate the report with sample data
              final String htmlReport = report.generate("1,abc");
      
              System.out.println(htmlReport);
          }
      }
      
© 2025 mtitek