Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 58 design patterns public ElectricGuitar retrieveInstrument long identifier return entityManager find ElectricGuitar class identifier Factories Creating domain objects might involve more complex logic than just calling a constructor To address this DDD uses factories The idea is to encapsulate the creation of complex objects into separate methods or classes If the creation of domain objects is tightly coupled to existing objects in the domain it makes sense to define factories as methods of domain types For the instrument craft shop example Ill create some music instances based on instruments I define a Music value object public class Music private final String description public Music String description this description description public String getDescription return description The creation of the value object is tightly bound to an instrument type and will therefore be placed as a method of the domain object type The same holds true if the creation requires information about the actual instance that is contained in its properties public class ElectricGuitar
You must have JavaScript enabled to view digital editions.