mercredi 2 novembre 2016

Deciding a dessign pattern

I have this Map object, representing a GoogleMap. I may contain markers with info windows. Markers may be clustered. Markers may respond to DOM events (click, drag, etc.). All this stuff comes from 3rd party library egeloen/ivory-google-map.

I want to create this map, add Marker objects with coordinates, that are kept in database, add a InfoWindow according to informaction kept in the same database, but keep my controller clean form all this marker/infowindow creation repetitive code.

There are two cases:

  • Static map - markers are clustered, not draggable or removable, on click they show info window.
  • Editable map - markers are not clustered, they are draggable, new markers can be added on click, info window shows different content.

The use should look like:

$map = (new StaticMap($places))->getMap(); //places holds coordinates, infowindow content
// or 
$map = (new EditableMap($places))->getMap();

What design principals should I apply to reach this goal. Because I dont want controller to know about all these Marker, Map, InfoWindow classes.

My own solucion is to create abstract class for common behaviour.

abstract class AbstractMap
{
    public function __construct(array $place)
    {
        $this->map = new Map();

        $this->proccessPlaces($places);
    }

    public function getMap() {
       ...
    }
}

class StaticMap extends AbstractMap {}

class EditableMap extends AbstractMap {}

The proccess of creating markers, infowindows is verbose and I don't think the factory pattern is suitable. Maybe Builder or Decorator? Or maybe extend the 3rd party Map class and add missing behaviour?

Aucun commentaire:

Enregistrer un commentaire