0

If i have an Persistence class which implements a method "toDTO" and this method return a POJO representing the object, will this be categorized as an adapter pattern?

Please note, that this is not a question of the implementation as such, but rather in which way it would be categorized.

2 Answers2

1

If the DTO uses the live state of the Persistence object then this may be classified as an adapter; if it makes a defensive copy of the data, discarding the original, then it is not an adapter.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Im not sure i understand your point, would it be a bother to clarify a little? (sorry for the late reply) i mean its a pojo so it would always be regarded as a defensive copy, or am i misunderstanding? – Andy Vidkjær Jan 18 '13 at 21:44
  • If the DTO doesn't hold a reference to the persistence object, then there can definitely be no word of the *Adapter* pattern at work. – Marko Topolnik Jan 18 '13 at 21:56
  • Well in my case, it will hold an id referencing the object in db. so that would definitely be adapter pattern? – Andy Vidkjær Jan 18 '13 at 22:06
  • That's an extralinguistic reference, it has nothing to do with OOP patterns. – Marko Topolnik Jan 18 '13 at 22:09
0

Note that DTO is usually classified as a pattern on itself. See Fowler's Patterns of Enterprise Application Architecture.

See also this SO question for discussion about DTO being an anti-pattern.

Community
  • 1
  • 1
brabec
  • 4,632
  • 8
  • 37
  • 63