Thursday, November 15, 2018

For Any Pattern: "Does This Allow More Objects to Each Deal with Fewer Things?"

An expert woodsman or carpenter is better than one person who does both...at least in code.

One of the principles of design patterns thinking is favoring delegation between objects over inheritance from one class to another.

I think we can actually expend that if we want to:
Favor delegation between objects over the same object doing two things.
Remember, when you inherit you end up with one object even though you wrote two classes. So the way I just wrote it still works for inheritance but it also works for, say, lumping everything together into a giant algorithm.

Note: Not more classes. More objects.

If you are considering a design pattern for your codebase and you're not certain if it applies, ask yourself this:
"Does this allow more objects to each deal with fewer things?"
If so, you may be headed in the right direction. If not, maybe consider a different approach.