Answer the question
In order to leave comments, you need to log in
Should I inject a service into an entity for ease of entity interface?
There is a MapPoint entity. It has a location property. There should also be a fakeLocation property, the value of which can be calculated by different algorithms. To calculate and describe algorithms, I want to use the Strategy
pattern
. There are two implementation options.
First. Inject the strategy into the entity as a service (for example, as described here ).
class MapPoint
{
/** @var array(ширина, долгота) */
private $location;
private $fakeLocationBuilderStrategy;
public function getFakeLocation()
{
return $this->fakeLocationBuilderStrategy->createFakeLocation($this->location);
}
// прочие геттеры и сеттеры...
}
FakeLocationBuilder::setFakeLocation(MapPoint $mapPoint)
{
$fakeLocation = $this->fakeLocationBuilderStrategy->createFakeLocation($this->location);
$mapPoint->setFakeLocation($fakeLocation);
}
Answer the question
In order to leave comments, you need to log in
I would throw all the logic into the model and not use services ...
Or the model as an abstract class above another model (or service).
In fact, this model will be "Strategic"
class MapLocationModel
{
protected $location;
protected $fakeLocation;
public function getFakeLocation()
{
if (null === $this->fakeLocation) {
// other logic
$fakeLocation = null;
// $this->setFakeLocation($fakeLocation ?: $this->getLocation());
$this->setFakeLocation($fakeLocation);
}
return $this->fakeLocation;
}
// public function setFakeLocation($location)
//...
}
class MapLocation extends MapLocationModel
{
protected $location;
protected $fakeLocation;
public function getFakeLocation()
{
return parent::getFakeLocation();
}
}
MapLocationEntity extends StrategyMapLocationModel
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question