Skip to content
Commitwise
Menu

Factory Method

Also called: Factory, Static factory method

A pattern that puts object creation behind a method, so callers ask for what they need and one place decides which concrete class to build.

Definition

In the classic form, a base class's routine calls an overridable method to create the object it works with, and each subclass returns a different kind. More broadly, factories and static factory methods (List.of, URL(string:)) keep construction in one place, can validate input, return cached immutable instances or subtypes, and let callers depend only on an interface.

In practice

factory.sender(Channel.SMS) returns a NotificationSender; only the factory knows it's an SmsSender with an API key.

Related terms

Esc

Type to search the whole site.