Modeling changes in the context of OO

The formal semantics of an object-oriented programming language include an encapsulated state . Is there a precedent for encapsulating a potential change preceding a state change? Although the following examples are in PHP, please also think that the agnostic language is in your answer.

Background

I have an object Clientwhose task is to send requests and receive responses from the server, and this is used to change the state of an object that is on another server by calling the API. There are several Url, one with an endpoint create, and the other with an endpoint update. The problem is that it updatecan be used to update several different elements inside a given object, each of which requires different parameters.

Level objects

Imagine objecthaving the following objects:

  • ImageLayer
  • BackgroundLayer
  • Textlayer
  • AudioLayer
  • (H) Layer

To change ImageLayer, the API requires:

  • Identifier
  • URL of the new image

To change TextLayer, the API requires:

  • Identifier
  • What do you change about this (is it font, size, text content?)
  • New value

, , id , AudioLayer:

  • URL

Client::updateImageLayer(), Client::updateTextLayer(), , Client (N)Layer .

Client::updateLayer(Layer $layer, array $values), , .

, , : Change.

, Change, - , Client, API?

interface Change
{
    public function isValid();

    public function getChanges();
}

class ImageLayerChange implements Change
{
    protected $url;

    protected $id;

    public function __construct($id, $url)
    {
        $this->url = $url;
        $this->id  = $id;
    }

    public function isValid()
    {
        // Use a validator object to check url is valid etc
    }

    public function getChanges()
    {
        return array($this->id, $this->url);
    }
}

, Client Change, ChangeSet, , , isValid() getChanges() API .

  • . ? - ? / ChangeSet , , , Change.

  • , ? - .

  • - , , ? .

+4
1

, , , API Client Request.

namespace Api;

interface Client {

    /**
     * @param string $method
     * @param string $urn
     * @param array $params
     * @return Response
     */
    public function call($method, $urn, array $params = array());
}

interface Request {

    public function isValid();

    public function make(Client $client);
}

, , - . App ApiClient API, , URL- . ApiClient URL- API, URN ( ). URI. ( , API).

namespace App;

class ApiClient implements \Api\Client {

    private static $url = 'https://api.yourapp.tld';

    /**
     * Just an example implementation using json (not completed)
     */
    public function call($method, $uri, array $params = array()) {
        $cUrlHandle = \curl_init(self::$url . '/' . $uri);
        \curl_setopt($cUrlHandle, CURLOPT_CUSTOMREQUEST, $method);
        \curl_setopt($cUrlHandle, CURLOPT_RETURNTRANSFER, true);

        if (!empty($params)) {
            \curl_setopt($cUrlHandle, CURLOPT_POSTFIELDS, \json_encode($params));
        }

        $response = curl_exec($cUrlHandle);

        //...
    }

}

. API-.

, . ( , ).

class ImageLayerRequest implements \Api\Request {

    private $id;
    private $url;
    private $apiUrn;
    private $params = array();

    /**
     * If $id provided it an update request otherwise it'll be create
     * 
     * @param string $id
     * @param string $imageUrl
     */
    public function __construct($id, $imageUrl) {
        $this->id = $id;
        $this->url = $imageUrl;
    }

    public function isValid() {
        if ($this->id === null) {
            //validate create request
            //...

            $this->apiUrn = 'image-layer/create';
        } else {
            //validate update request
            //...
            $this->params['id'] = $this->id;
            $this->apiUrn = 'image-layer/update';
        }

        $this->params['url'] = $this->url;
    }

    public function make(\Api\Client $client) {
        return $client->call('post', $this->apiUrn, $this->params);
    }

}

AudioLayer:

class AudioLayerRequest implements \Api\Request {

    private $id;
    private $bitrate;
    private $url;

    //the rest is similar, just diferent parameters
}
+1

All Articles