29. Factory

image

© Jennifer M. Kohnke

The man who builds a factory builds a temple.

—Calvin Coolidge (1872–1933)

The Dependency-Inversion Principle (DIP) (Chapter 11) tells us that we should prefer dependencies on abstract classes and avoid dependencies on concrete classes, especially when those classes are volatile. Therefore, the following snippet of code violates this principle:

Circle c = new Circle(origin, 1);

Circle is a concrete class. Therefore, those modules that create instances of Circle must violate DIP. Indeed, any line of code that uses the new keyword violates DIP.

There are times when violating DIP is mostly harmless, which is pretty ...

Get Agile Principles, Patterns, and Practices in C# now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.