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












