Edit: I realized that this pattern is very similar to currying , a method that functional programmers use to indicate the function of parameters before calling. The difference here is that we create constructors for objects instead of simple currying functions.
Over the course of several projects, I found myself using this fancy design pattern that I cannot find. Does he have a name? Maybe this is just bad practice, tell me.
Design Pattern
With this template, you would ...
- An abstract abstract class without abstract methods (we can discuss this later).
- A lot of "implementations" of the base class. However, these implementations will only be used to invoke the base class constructor .
Java example (with a hypothetical scenario)
I am defining a hypothetical scenario to provide some context.
Scenario:
Bob writes a small API for scanning source code. He wants to check if a comment begins or ends with a given index in the source code.
Here is Bob's code.
1. Abstract base class
public abstract class CommentDetector { private final String startPattern; private final String endPattern; protected CommentDetector(String startPattern, String endPattern) { this.startPattern = startPattern; this.endPattern = endPattern; } public boolean commentStartsAt(int index, String sourceCode) {
You might be wondering why it is abstract, but does not have abstract methods. This is simply because Bob does not want you to instantiate it directly. Bob wants you to CommentDetector implementation, and then instantiate it instead. Here are two of Bob's implementations ...
2. Some implementations
One for multi-line comments in Java:
public class JavaMultiLineCommentDetector extends CommentDetector { public JavaMultiLineCommentDetector() { super("/*", "*/"); } }
One for single line comments in Java:
public class JavaSingleLineCommentDetector extends CommentDetector { public JavaSingleLineCommentDetector() { super("//", "\n"); } }
Bob wrote these implementations for us so that we could write new JavaMultiLineCommentDetector() instead of new CommentDetector("/*", "*/") .
Bob also recommends that you write your own implementations for other languages, if necessary.
Summary
It seems that the purpose of this design pattern is to improve code readability by predefining constructor calls.
This design pattern provides a polymorphic view of the code (although it may not be truly polymorphic).
Writing new implementations is quick and easy.
Implementations are independent of each other and can be compiled / deployed independently.
Does this design template have a name?