Template method pattern

Define the skeleton of an algorithm in a method, deferring some steps to subclasses. The template approach allows subclasses to redefine certain steps in an algorithm without changing the structure of the algorithm.P289

The characteristics of
  • Lead the algorithm framework and protect the algorithmP288
  • Maximized reuse codeP288
  • Algorithms exist in one place and are easy to modifyP288
  • Focus on the algorithm itself, with subclasses providing the full implementationP288
  • The template methods themselves are decoupled from the internal concrete operationsP289

Design principles

The Hollywood principle: Don’t let low-level components call high-level components. Let high-level components call low-level components. P296

advantages
  • Prevent dependency corruption (dependency corruption makes it difficult for users to understand the design of the system)P296

To consider

What other models adopt the Hollywood principle? P297

  • Factory method, observer
  • Abstract factory, facade, command

To consider

We know we should use composition more and inheritance less. The implementation of the sort() template method decides not to use inheritance, and the sort method is implemented as a static method with a run-time and Comparable combination. What are the pros and cons of this approach? How do you deal with this difficult problem? Do Java arrays make all this particularly troublesome? P305

advantages
  • Decoupled arrays and objects, avoiding arrays of objects inheriting arrays
disadvantages
  • The object of a sortable array must beComparableThe comparison logic cannot be changed dynamically
    • Can be usedComparatorInterface that accepts two objects to be compared and returns the comparison result; And in thesortMethod plus oneComparatorparameter
Do Java arrays make all this particularly troublesome?
  • Java arrays are not the main thing that makes this all so much trouble, but Java arrays and object arrays don’t have much to do with each other and can’t have much coupling, so inheritance shouldn’t be used. If an object array inherits from a Java array, the object itself is required to beComparable, which greatly limits the usage scenarios and scope of arrays.

To consider

Consider another pattern, which is a special case of template methods, where primitive operations are used to create and return objects. What’s the model?

  • Factory method pattern, abstract factory pattern

thoughts

  • The sense template approach delegates multiple policies internally and hands them to subclasses to implement specific policies. In fact, when you’re writing code, you often use this idea unintentionally. For example, when dealing with excel file import, the steps are relatively fixed. Various verification is carried out first, and then excel file is processed (parsed into different objects according to different requests), and finally the import result is returned. I took the excel file step out of the way and used the functional interface.
public Result handleExcel(File excelFile, ExcelConsumer consumer) {
    // All kinds of checks
    
    boolean success = consumer.consume(excelFile);
    
    // Build the result to encapsulate the object and return it
}
Copy the code

This post is posted on GitHub: Reading-Notes/Head-First-Design-Patterns