만세! 한국어 버전이 드디어 출시되었습니다! 피드백을 공유하거나 오류를 보고하려면 메시지를 보내주세요.

Change Unidirectional Association to Bidirectional

Problem

You have two classes that each need to use the features of the other, but the association between them is only unidirectional.

Solution

Add the missing association to the class that needs it.

Before
Change Unidirectional Association to Bidirectional - Before
After
Change Unidirectional Association to Bidirectional - After

Why Refactor

Originally the classes had a unidirectional association. But with time, client code needed access to both sides of the association.

Benefits

  • If a class needs a reverse association, you can simply calculate it. But if these calculations are complex, it's better to keep the reverse association.

Drawbacks

  • Bidirectional associations are much harder to implement and maintain than unidirectional ones.

  • Bidirectional associations make classes interdependent. With a unidirectional association, one of them can be used independently of the other.

How to Refactor

  1. Add a field for holding the reverse association.

  2. Decide which class will be "dominant". This class will contain the methods that create or update the association as elements are added or changed, establishing the association in its class and calling the utility methods for establishing the association in the associated object.

  3. Create a utility method for establishing the association in the "non-dominant" class. The method should use what it's given in parameters to complete the field. Give the method an obvious name so that it isn't used later for any other purposes.

  4. If old methods for controlling the unidirectional association were in the "dominant" class, complement them with calls to utility methods from the associated object.

  5. If the old methods for controlling the association were in the "non-dominant" class, create the methods in the "dominant" class, call them, and delegate execution to them.