liveBook · Manning

Factory

As explained in Data-Oriented Programming in Java

A factory function is a function that constructs and returns an instance or structured test datum through a defined parameter interface.

A factory centralizes construction or selection behind a defined interface. In ingestion, it maps a file type to the Transformer and BulkLoader that handle it, keeping the main loop focused on workflow rather than concrete classes. section 10.2

A practical factory can:

  • Store strategies in maps keyed by a domain value such as a file type. section 10.2
  • Return a Transformer or BulkLoader through an interface without exposing its concrete implementation. section 10.2
  • Reject unknown keys immediately instead of allowing an invalid strategy selection to continue. section 10.2
  • Construct focused test data while hiding fields irrelevant to the behavior under test. section 11.3.1

Selecting ingestion strategies

An ingestion loop reads each manifest entry, asks the factory for a Transformer and BulkLoader using the entry's type, transforms the cloud object, and loads the resulting UpdatesAndInserts. Adding a new file type requires adding a strategy to the factory rather than rewriting the loop. section 10.2

Making test intent visible

A factory function such as mkAcnt(AMER, threshold.plus(ONE)) supplies routine account fields while exposing the region and spend that determine the test outcome. The test can then name above-limit, at-limit, and below-limit cases directly. section 11.3.1

Listing 10.12 The Factory Pattern
class IngestionFactory {
    private Map<String, Transformer> transformers;  
    private Map<String, BulkLoader> loaders;        
 
    // constructor elided
 
    public Transformer getTransformer(String type) {   
        return getOrThrow(this.transformers, type);    
    }
    public BulkLoader getLoader(String type) {         
        return getOrThrow(this.loaders, type);
    }
 
    public static <A> A getOrThrow(Map<String, A> m, String key) {
        if (!m.containsKey(key)) {
            throw new IllegalArgumentException("Unknown Strategy!");
        }
        return m.get(key);
    } 
}

Inspect the map-backed IngestionFactory and its getOrThrow lookup. section 10.2

Listing 11.16 Use factory functions to highlight what’s important when creating data
Account mkAcnt(Region region, USD spend) {  #A
    return new Account(
       new AccountId("000000001"),
       region,    #B
       spend,     #B
       ENTERPRISE, 
       new Sector("Education"),
       Instant.now()
    );
}

@Test 
void noDiscountForLargeAccountsInAMER() {
  USD threshold = USD.valueOf(1_500_000.00);     #C
  MyService service = new MyService(threshold);  #C
  Set<Account> shouldBeDropped = 
      Set.of(mkAcnt(AMER, threshold.plus(ONE))); #D
  Set<Account> shouldBeProcessed = Set.of(accountA, accountC);      
  Set<Account> items = union(shouldBeDropped, shouldBeProcessed);  

  Set<Result> result = service.applyDiscounts(items);   
  assertEquals(shouldBeProcessed.size(), result.size()); 
}

Inspect how mkAcnt hides incidental account fields while highlighting test-relevant parameters. section 11.3.1

How Data-Oriented Programming in Java covers it

How deep it goes
Shows map-backed lookup, unknown-key failure, and parameterized test-data construction, while deferring broader abstraction trade-offs.
Examples
Uses cloud-file ingestion with Transformers and BulkLoaders, plus account tests organized around threshold-relative factory-generated data.
What it assumes
Assumes intermediate Java, interfaces, collections, testing, and domain objects while teaching construction choices through concrete examples.
What you can do after
Builds interface-based dispatch and focused test fixtures without repeating concrete construction details.
How it connects
Connects factories to interfaces, strategy selection, transformers, repositories, test data, and the Template Pattern.

Also explained in

Compare how 13 books explain factory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.