patternsfactory

Factory Pattern pattern for C development

⭐1stars🔀0forks📁View on GitHub🕐Updated Jan 4, 2026

When & Why to Use This Skill

This Claude skill implements the Factory Pattern specifically for C development, offering a robust framework for structured object creation. It centralizes construction logic to hide allocation details, ensures consistent memory management through standardized naming conventions, and promotes modular architecture by facilitating dependency injection during the initialization phase.

Use Cases

  • Standardizing Object Creation: Using the `*_create()` convention to encapsulate heap allocation and initialization, ensuring callers receive fully prepared structs without needing to know internal allocation details.
  • Memory Management Clarity: Distinguishing between heap-allocated objects (via `*_create`) and pre-allocated memory initialization (via `*_init`) to prevent memory leaks and ownership confusion in C projects.
  • Dependency Injection: Passing environment contexts (like `ik_env_t`) into factory functions to provide objects with access to loggers, configurations, and clocks without relying on global state.
  • Enhanced Unit Testing: Decoupling object construction from global variables, allowing developers to inject mock dependencies at creation time for more reliable and isolated testing.
namepatterns/factory
descriptionFactory Pattern pattern for C development

Factory Pattern

Function that encapsulates object creation, returning an allocated and initialized struct. Centralizes construction logic and hides allocation details from callers.

ikigai Application

Current usage:

  • ik_scrollback_create() - creates scrollback buffer
  • ik_mark_create() - creates checkpoint marks
  • ik_input_parser_create() - creates input parser

Convention: Use *_create() for heap allocation (caller owns), *_init() for initializing pre-allocated memory.

With ik_env_t: Factories should receive ik_env_t *env as first parameter for access to logger, config, and clock during construction.

Testing: Factories enable injecting dependencies at creation time rather than reaching for globals.