Wrong OO architecture, sharing objects and dependencies? Maybe I am missing a design template here?

OK, I am creating a small PHP application. The current structure looks something like this:

$client = new AppName\Client;
$model = $client->getModel();
$model->getNode();

Everything is beautiful here. The client is responsible for connecting and running commands through Socket for the small C application that I created. I just play with architecture here. Client :: getModel returns an instance of the model. During getModel, I inject Client inside Model through the constructor. Something like that:

public function getModel()
{
    return new Model('parameter', $this);
}

Which in turn...

// Model
public function __construct($param, Client $client)
{
    $this->setClient($client);
    // ...
}

However, during Model :: getNode, I want to return an instance of Node. However, unlike getModel, I do not want to enter the Client again. But I need to. Right now, I'm doing this:

public function getNode()
{
    return new Node('parameter', $this->getClient());
}

, . -, ... Injection Dependency, , Dependency .

+3
3

, , , . OO , /, /, - - , , "".

, (, ). , ( , ...)

- , , . , , , .

0

, setClient() getClient()

setClient() getClient(), .

setClient($client){
    $this->client = $client;
}

getClient(){
    return $this->client;
}
0

( !). factory! :

$client = new AppName\Client($model);

You should never mix factory logic with your business logic, as this makes testing impossible.

0
source

All Articles