Inject Instances Through Initializers or Properties

Subclass and Override Method lets you replace entire methods, not only dependencies. It’s great for legacy code but puts almost no design pressure on the code. It also keeps dependencies somewhat hidden. Using dependency injection means making more changes, but these changes bring greater clarity.

On the surface, dependency injection (DI) means we pass dependencies into an object. Instead of allowing the object to decide its dependencies, we tell the object what to use.

DI is more than passing in instances. It promotes loosely coupled code that depends on protocols instead of concrete types. But we don’t have to go full-on with protocols to get benefits from DI. By providing a default, ...

Get iOS Unit Testing by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.