1

I have an @Entity. I want to make calculations on its fields. The problem is that I need to use extra constant parameters for some calculations that came from a @Service. How should I handle this problem? Probably I shouldn't autowire a service into a model.

Another option is to make a service for the model to handle all the calculations and store results in the model. I don't like it, because recalculations are more problematic and formulas are not stored together with the model.

Any idea?

erencan
  • 3,725
  • 5
  • 32
  • 50
Hubidubi
  • 850
  • 4
  • 18
  • 34
  • What kind of calculations and will they persist with the DB? – erencan Sep 17 '13 at 19:02
  • Calculations won't be persisted in DB. I need calculations like the product of a field and a constant or sum of two fields. I want to get the result of the calculations via getters so they are recalculated every time. – Hubidubi Sep 17 '13 at 19:17

1 Answers1

1

As far as i understand, you need to implement an adapter design pattern.

The adapter pattern is a structural design pattern. In the adapter pattern, a wrapper class (ie, the adapter) is used translate requests from it to another class (ie, the adaptee). In effect, an adapter provides particular interactions with an adaptee that are not offered directly by the adaptee.

You need to implement a adapter class which holds both your entity and parameters from your @Service (Adaptee1 and Adaptee2 in the UML) . Calculations can be done in the adapter methods (lets say methodA in the UML)

enter image description here

There is no need to set Adapter class to spring context. A dynamic instance can be created in the client code, most possibly in your @Service bean.

enter image description here

I find avajava design patterns tutorial simple and usefull, then i recommend you to have a look.

See also

Adapter Pattern avajava.com

Adapter Pattern

Examples of GoF Design Patterns

Community
  • 1
  • 1
erencan
  • 3,725
  • 5
  • 32
  • 50