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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
laketuna
  • 3,481
  • 13
  • 55
  • 100

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
  • 45,265
  • 6
  • 51
  • 87