# Creational Patterns

> Every principle in this category is listed as a record.

Page: Ontology · Principles
Canonical: https://banes-lab.com/ontology#arch-category-creational-patterns

Every principle in this category is listed as a record. Each record carries its kind, its severity, the scopes it applies at and the layer it lives in, then the edge relations that join it to other records, the records that point back at it, the contracts that answer to it and the tensions it takes part in. The descriptors say how it is violated, detected, measured, repaired and enforced. Where the record carries one, an exemplar shows the shape before and after the principle is applied.

Relations diagram

The relations inside this category.

```mermaid
flowchart LR
n_factory_pattern["Factory Pattern"]
n_factory_method_pattern["Factory Method Pattern"]
n_abstract_factory_pattern["Abstract Factory Pattern"]
n_builder_pattern["Builder Pattern"]
n_prototype_pattern["Prototype Pattern"]
n_singleton_pattern["Singleton Pattern"]
```

### Factory Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: recommended
- Scope: [object_creation](https://banes-lab.com/records/force/object-creation.md), module
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Creation Variation](https://banes-lab.com/records/lex/creation-variation.md)

Reinforces
[Open/Closed Principle (OCP)](https://banes-lab.com/records/arch/open-closed.md), [Encapsulation](https://banes-lab.com/records/arch/encapsulation.md)

Enables
[Polymorphic Construction](https://banes-lab.com/records/lex/polymorphic-construction.md)

In tension with
[Simplicity](https://banes-lab.com/records/lex/simplicity.md)

Conflicts with
[Scattered Construction Logic](https://banes-lab.com/records/lex/scattered-construction-logic.md)

Referenced by
[Registry Pattern](https://banes-lab.com/records/arch/registry-pattern.md)

Tensions
[Factory Pattern Simplicity](https://banes-lab.com/records/tension/factory-pattern-simplicity.md)

Violated by
duplicated conditional construction

Detected by
repeated constructors/switches

Measured by
construction duplication count

Refactored by
Extract Factory

Enforced by
creation policy review

Before

```typescript
const foo = new Foo("foo", 0, [], new Date(), "draft");
```

After

```typescript
function makeFoo(name: string): Foo {
return new Foo(fooId(), name, 0, [], clock.now(), "draft");
}
```

### Factory Method Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: contextual
- Scope: class hierarchy, framework
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Subclass-Controlled Creation](https://banes-lab.com/records/lex/subclass-controlled-creation.md)

Reinforces
[Open/Closed Principle (OCP)](https://banes-lab.com/records/arch/open-closed.md)

Enables
[Deferred Instantiation](https://banes-lab.com/records/lex/deferred-instantiation.md)

In tension with
[Inheritance Complexity](https://banes-lab.com/records/lex/inheritance-complexity.md)

Conflicts with
[Concrete Constructor Coupling](https://banes-lab.com/records/lex/concrete-constructor-coupling.md)

Tensions
[Factory Method Pattern Inheritance Complexity](https://banes-lab.com/records/tension/factory-method-pattern-inheritance-complexity.md)

Violated by
fixed construction in base workflow

Detected by
base class directly instantiates variant

Measured by
variant construction duplication

Refactored by
Introduce Factory Method

Enforced by
[design review](https://banes-lab.com/records/arch/design-review.md)

Before

```typescript
class FooImporter {
import(raw: string) { return new JsonFooParser().parse(raw); }
}
```

After

```typescript
abstract class FooImporter {
protected abstract parser(): FooParser;
import(raw: string) { return this.parser().parse(raw); }
}
class JsonFooImporter extends FooImporter {
protected parser() { return new JsonFooParser(); }
}
```

### Abstract Factory Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: contextual
- Scope: product family, component
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Related Product Variants](https://banes-lab.com/records/lex/related-product-variants.md)

Reinforces
[Interchangeability](https://banes-lab.com/records/arch/interchangeability.md)

Enables
[Family-Level Replacement](https://banes-lab.com/records/lex/family-level-replacement.md)

In tension with
[Boilerplate](https://banes-lab.com/records/lex/boilerplate.md)

Conflicts with
[Mixed Product Families](https://banes-lab.com/records/lex/mixed-product-families.md)

Tensions
[Abstract Factory Pattern Boilerplate](https://banes-lab.com/records/tension/abstract-factory-pattern-boilerplate.md)

Violated by
incompatible product combinations

Detected by
manual selection of related product classes

Measured by
family mismatch defects

Refactored by
Introduce Abstract Factory

Enforced by
factory conformance tests

Before

```typescript
const store = env === "test" ? new MemoryFooStore() : new SqlFooStore();
const bus = env === "test" ? new MemoryFooBus() : new KafkaFooBus();
```

After

```typescript
interface FooPlatformFactory {
store(): FooStore;
bus(): FooBus;
}
class TestFooPlatformFactory implements FooPlatformFactory {
store() { return new MemoryFooStore(); }
bus() { return new MemoryFooBus(); }
}
```

### Builder Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: recommended
- Scope: object construction, API
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Complex Construction](https://banes-lab.com/records/lex/complex-construction.md)

Reinforces
[Intent-Revealing Interface](https://banes-lab.com/records/arch/intent-revealing-interface.md)

Enables
[Valid Object Creation](https://banes-lab.com/records/lex/valid-object-creation.md)

In tension with
[Boilerplate](https://banes-lab.com/records/lex/boilerplate.md)

Conflicts with
[Telescoping Constructor](https://banes-lab.com/records/lex/telescoping-constructor.md)

Tensions
[Builder Pattern Boilerplate](https://banes-lab.com/records/tension/boilerplate-builder-pattern.md)

Violated by
constructors with many optional params

Detected by
high-arity constructors

Measured by
constructor parameter count

Refactored by
Introduce Builder

Enforced by
API review

Before

```typescript
const foo = new Foo("foo_1", "Foo", [], 0, false, undefined, "draft");
```

After

```typescript
const foo = new FooBuilder()
.withId("foo_1")
.withName("Foo")
.withStatus("draft")
.build();
```

### Prototype Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: contextual
- Scope: [object_creation](https://banes-lab.com/records/force/object-creation.md), runtime
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Cloneable Template Object](https://banes-lab.com/records/lex/cloneable-template-object.md)

Reinforces
[Runtime Extensibility](https://banes-lab.com/records/arch/runtime-extensibility.md)

Enables
[Dynamic Object Creation](https://banes-lab.com/records/lex/dynamic-object-creation.md)

In tension with
[Copy Semantics](https://banes-lab.com/records/lex/copy-semantics.md)

Conflicts with
[Complex Factory Hierarchies](https://banes-lab.com/records/lex/complex-factory-hierarchies.md)

Tensions
[Prototype Pattern Copy Semantics](https://banes-lab.com/records/tension/copy-semantics-prototype-pattern.md)

Violated by
expensive repeated setup

Detected by
duplicate initialization flows

Measured by
initialization duplication/cost

Refactored by
Introduce Prototype, Add Clone Semantics

Enforced by
clone tests

Before

```typescript
function copyFoo(foo: Foo) {
return new Foo(foo.id, foo.name, [...foo.tags], foo.settings.theme, foo.settings.mode);
}
```

After

```typescript
class FooPrototype {
constructor(private readonly base: Foo) {}
clone(overrides: Partial<Foo> = {}): Foo {
return structuredClone({ ...this.base, ...overrides });
}
}
const template = new FooPrototype(await buildExpensiveFoo());
const draft = template.clone({ name: "quick" });
```

### Singleton Pattern

- Kind: [pattern](https://banes-lab.com/records/kind/pattern.md)
- Severity: contextual/discouraged unless justified
- Scope: [object_creation](https://banes-lab.com/records/force/object-creation.md), lifetime, composition root
- Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)

Details

Requires
[Single-Instance Need](https://banes-lab.com/records/lex/single-instance-need.md)

Reinforces
[Controlled Instantiation](https://banes-lab.com/records/lex/controlled-instantiation.md)

Enables
[Shared Resource Access](https://banes-lab.com/records/lex/shared-resource-access.md)

In tension with
[Testability](https://banes-lab.com/records/arch/testability.md), [Dependency Injection](https://banes-lab.com/records/arch/dependency-injection.md)

Conflicts with
[Global Mutable State](https://banes-lab.com/records/lex/global-mutable-state.md)

Tensions
[Singleton Pattern Testability](https://banes-lab.com/records/tension/singleton-pattern-testability.md), [Singleton Pattern Dependency Injection](https://banes-lab.com/records/tension/dependency-injection-singleton-pattern.md)

Violated by
a global mutable instance reached from anywhere

Detected by
static global access to a shared service

Measured by
global-instance reach-in count

Refactored by
Compose Single Instance at the Root, Inject It

Enforced by
composition-root review

Before

```typescript
let instance: FooService | undefined;
function getFooService() { return instance ??= new FooService(); }
```

After

```typescript
class FooService {}
export function composeApp() {
const fooService = new FooService();
return { fooService, fooController: new FooController(fooService) };
}
```

## Links to

- [pattern](https://banes-lab.com/records/kind/pattern.md)
- [object_creation](https://banes-lab.com/records/force/object-creation.md)
- [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)
- [Creation Variation](https://banes-lab.com/records/lex/creation-variation.md)
- [Open/Closed Principle (OCP)](https://banes-lab.com/records/arch/open-closed.md)
- [Encapsulation](https://banes-lab.com/records/arch/encapsulation.md)
- [Polymorphic Construction](https://banes-lab.com/records/lex/polymorphic-construction.md)
- [Simplicity](https://banes-lab.com/records/lex/simplicity.md)
- [Scattered Construction Logic](https://banes-lab.com/records/lex/scattered-construction-logic.md)
- [Registry Pattern](https://banes-lab.com/records/arch/registry-pattern.md)
- [Factory Pattern / Simplicity](https://banes-lab.com/records/tension/factory-pattern-simplicity.md)
- [Subclass-Controlled Creation](https://banes-lab.com/records/lex/subclass-controlled-creation.md)
- [Deferred Instantiation](https://banes-lab.com/records/lex/deferred-instantiation.md)
- [Inheritance Complexity](https://banes-lab.com/records/lex/inheritance-complexity.md)
- [Concrete Constructor Coupling](https://banes-lab.com/records/lex/concrete-constructor-coupling.md)
- [Factory Method Pattern / Inheritance Complexity](https://banes-lab.com/records/tension/factory-method-pattern-inheritance-complexity.md)
- [Design Review](https://banes-lab.com/records/arch/design-review.md)
- [Related Product Variants](https://banes-lab.com/records/lex/related-product-variants.md)
- [Interchangeability](https://banes-lab.com/records/arch/interchangeability.md)
- [Family-Level Replacement](https://banes-lab.com/records/lex/family-level-replacement.md)
- [Boilerplate](https://banes-lab.com/records/lex/boilerplate.md)
- [Mixed Product Families](https://banes-lab.com/records/lex/mixed-product-families.md)
- [Abstract Factory Pattern / Boilerplate](https://banes-lab.com/records/tension/abstract-factory-pattern-boilerplate.md)
- [Complex Construction](https://banes-lab.com/records/lex/complex-construction.md)
- [Intent-Revealing Interface](https://banes-lab.com/records/arch/intent-revealing-interface.md)
- [Valid Object Creation](https://banes-lab.com/records/lex/valid-object-creation.md)
- [Telescoping Constructor](https://banes-lab.com/records/lex/telescoping-constructor.md)
- [Builder Pattern / Boilerplate](https://banes-lab.com/records/tension/boilerplate-builder-pattern.md)
- [Cloneable Template Object](https://banes-lab.com/records/lex/cloneable-template-object.md)
- [Runtime Extensibility](https://banes-lab.com/records/arch/runtime-extensibility.md)
- [Dynamic Object Creation](https://banes-lab.com/records/lex/dynamic-object-creation.md)
- [Copy Semantics](https://banes-lab.com/records/lex/copy-semantics.md)
- [Complex Factory Hierarchies](https://banes-lab.com/records/lex/complex-factory-hierarchies.md)
- [Prototype Pattern / Copy Semantics](https://banes-lab.com/records/tension/copy-semantics-prototype-pattern.md)
- [Single-Instance Need](https://banes-lab.com/records/lex/single-instance-need.md)
- [Controlled Instantiation](https://banes-lab.com/records/lex/controlled-instantiation.md)
- [Shared Resource Access](https://banes-lab.com/records/lex/shared-resource-access.md)
- [Testability](https://banes-lab.com/records/arch/testability.md)
- [Dependency Injection](https://banes-lab.com/records/arch/dependency-injection.md)
- [Global Mutable State](https://banes-lab.com/records/lex/global-mutable-state.md)
- [Singleton Pattern / Testability](https://banes-lab.com/records/tension/singleton-pattern-testability.md)
- [Singleton Pattern / Dependency Injection](https://banes-lab.com/records/tension/dependency-injection-singleton-pattern.md)

## Linked from

- [The layer topology](https://banes-lab.com/ontology/schema/the-layer-topology.md)
- [The membership](https://banes-lab.com/ontology/schema/the-membership.md)
