Chapter 9. Please recycle: Reusability

 

This chapter covers

  • Generalizing a piece of software to a wider context
  • Using generics to write reusable classes
  • Using and customizing mutable collectors on data streams

In the previous chapters, you developed concrete classes that solved a specific problem. Now, assume you need to generalize your solution to a broader variety of problems. Ideally, you should discern the essential features of the problem, separate them from what’s merely incidental, and develop a solution for all the problems that share the same essential structure. Unfortunately, discerning the essential from the incidental is far from obvious. Roughly speaking, you should try to keep the key structure—that is, the part that may be useful in other contexts.

This final chapter assumes you’re familiar with generics, including bounded type parameters.

9.1. Establishing boundaries

In the first decades of OOP, reusability was considered one of the selling points of the paradigm. The promise was that all you ever had to do was write small, reusable components and combine them with existing reusable components pulled off the shelf. After some 50 years of practice (the first OO language was 1967’s Simula), some of this promise has been confirmed, and some has proven to be off target.

9.2. The general framework

9.3. A generic container implementation

9.4. General considerations

9.5. Recovering water containers    [Generic]

9.6. Social network posts

9.7. And now for something completely different

9.8. Real-world use cases

9.9. Applying what you learned

Summary

Answers to quizzes and exercises

Further reading