Skip to content
aipatterns.dev

Category

Dealing with Generalization

Techniques that move members up and down class hierarchies, create or collapse hierarchies, and swap inheritance for delegation.

What these techniques solve

Inheritance hierarchies drift. Sibling classes copy the same method, a base class carries fields only one subclass uses, a subclass inherits a contract it can't honor, or a hierarchy has outlived the variation that justified it. These techniques reshape hierarchies, and replace inheritance with composition when the "is-a" relationship no longer holds.

Put shared behavior at the lowest level that all of its users share, and prefer delegation whenever a subclass would ignore part of what it inherits.

Members

How to choose within this category

Go has no inheritance, so only the interface and delegation techniques apply there. Moving members along a hierarchy can change which override runs, so run the tests for every subclass after each move.

See also

Original writing and examples. Topic coverage follows Refactoring.Guru, which explains this topic with its own illustrations.