(no title)
andor | 2 years ago
I see two reasons for using a factory:
1. Reducing the scope of what a piece of code is responsible for (a.k.a. "Single-Responsibility-Principle").
Assume you are working on a graphics app that can draw 50 different shapes. The currently selected tool is stored in a variable, and there is a long switch statement that returns a new Shape depending on that variable. You wouldn't want the switch statement to dominate the rest of the code:
handleClick(x, y) {
selectedTool = getSelectedTool();
shape = switch (selectedTool) {
case "CIRCLE" -> new Circle();
// 49 more lines here...
}
drawShape(shape, x, y);
}
Not only would it get more difficult to read the code, but also the test for handleClick() would have to test for all 50 shapes. If the instantiation of the shape is separated out into its own function, handleClick() can be shorter. If the factory function is injectable, the tests for handleClick() can focus on the coordination work that the function does: it asks the factory for the shape and draws it in the right place.2. Allowing to reconfigure what kind of objects are created through Dependency Injection. For instance:
class CommentRepository {
constructor(dataSource, queryFactory) { ... }
getComments(articleId) {
commentsQuery = queryFactory.getCommentsQuery(articleId);
return dataSource.query(commentsQuery).map(toResponseType);
}
}
class PostgresQueryFactory { ... }
class RedisQueryFactory { ... }
winrid|2 years ago
winrid|2 years ago