The Don't Repeat Yourself Principle says that each piece of knowledge or behavior should have one authoritative representation. Repeated code enlarges a program, makes maintenance harder, and creates change leaks: one copy may be missed or changed differently from the others. section 2.3.3 section 2.4
Use duplication as a design warning, then refactor toward shared behavior:
- Spot similar operations that multiply as requirements grow, such as overloaded add() and find() functions for each book kind. section 2.3.3
- Extract the repeated behavior into one cohesive function or class instead of maintaining parallel copies. section 2.3.3
- Check whether the refactoring also removes unnecessary subclasses, runtime type checks, and type conversions. section 2.3.4
- Keep the authoritative representation where changes naturally belong; the final design changes Attributes without modifying Book or Catalogue. section 2.3.4
Refactoring a book catalogue
A subclass-based catalogue defines separate Fiction and Cookbook classes, overloaded add() and find() functions, and dynamic_cast calls to identify matching types and attributes. Adding how-to books repeats the same structure again. A DRY redesign stores attributes as key–value pairs in one Attributes class, then gives Catalogue one add() function and one find() function. Searches can match only the supplied attributes, so a query for REGION: China returns Chinese cookbooks without requiring a cookbook-specific search function. section 2.3.3 section 2.3.4
Listing 2.28 (Program 2.4 Books-4): Catalogue.cpp
#include "Book.h"
#include "Catalogue.h"
void Catalogue::add(Attributes * const attrs) #1
{
booklist.push_back(new Book(attrs));
}
vector<Book *>Catalogue::find(const Attributes& target_attrs) #2
const
{
vector<Book *> matches;
for (Book *book : booklist)
{
Attributes *book_attributes = book->get_attributes();
if (book_attributes->is_match(target_attrs))
{
matches.push_back(book);
}
}
return matches;
}
#1 Only one add() member function
#2 Only one find() member function
Inspect the unified Catalogue implementation: one add() function constructs Book objects, and one find() function delegates matching to Attributes. section 2.3.4









