Skip to content

AbstractFactory

A pattern that creates related groups of objects without depending on concrete types.

  • When replacing an entire set of platform-specific services.
  • When separating test product families.
  • Abstract Factory
  • Concrete Factory
  • Abstract Product

The code below is a simplified Unity example based on the scenario described above.

public interface IPlatformServiceFactory
{
ILoginService CreateLoginService();
IStoreService CreateStoreService();
}
public sealed class MobilePlatformServiceFactory : IPlatformServiceFactory
{
public ILoginService CreateLoginService() => new MobileLoginService();
public IStoreService CreateStoreService() => new MobileStoreService();
}
public sealed class PcPlatformServiceFactory : IPlatformServiceFactory
{
public ILoginService CreateLoginService() => new PcLoginService();
public IStoreService CreateStoreService() => new PcStoreService();
}
  • Object creation responsibilities are well organized, which makes dependency management easier.
  • Creation policies can be changed flexibly by environment or situation.
  • Avoid introducing overly abstract creation layers for simple problems.
  • As creation rules increase, keeping documentation and tests in sync becomes more important.

This shows the flow where platform-specific product families are created behind the same interface.

Abstract Factory Flow