2

Let's say I have write transformers (e.g. data presentation layer) in such ways that the usage looks like these (using PHP syntax):

A: $userTransformer can be used for different users, kind of like a helper.

$userTransformer->transform($user) // Outputs user data for a webpage

B: $userTransformer is specifically for one user.

$userTransformer->transform() // Same user output

Are there terms describing the ways these transformer classes are designed? A doesn't have any dependency during instantiation, whereas B requires $user to be instantiated. Personally, I prefer B, and I'm trying to look up some literature regarding this.

Zoe
  • 27,060
  • 21
  • 118
  • 148
laketuna
  • 3,832
  • 14
  • 59
  • 104

1 Answers1

2

In the language of UML, consider the difference between dependency and association.

Dependency:

$userTransformer->transform($user) // user is just a method argument

Association:

$userTransformer->transform() // user is a class field

There are two forms of association: aggregation and composition. Personally, when designing class relationships, I think in terms of "strength of relationships" where:

dependency < aggregation < composition

Mario Galic
  • 47,285
  • 6
  • 56
  • 98