Dependency Injection pattern

OO design has to deal with objects interacting with each other. The resulting dependencies between those objects are potential reasons for side effects.
Therefore, the dependencies should be reduced as much as possible.

A Tax object is directly coupled with the Person class.
The tax calculation only works for people in this naive implementation:

taxation=Taxation.new5_000taxation.debit(20)# => 1000

However, other objects (companies or generally legal forms) should also be taxable.

Dependency Injection

In such situations the necessary flexibility can be achieved with Dependency Injection. In this case, the required object (Person, Company, etc.) is not instantiated directly in the class (Tax) any longer. But it is created outside and passed instead: