lundi 28 février 2022

Implement an interface in a class with additional methods

I'm trying to embrace clean code and design patterns and came across to the following scenario.

interface SystemLogInterface
{
    public function log(string $type);
}

My fist class implementation is

class LogDownloadService implements SystemLogInterface
{
  public function log(){}
}

and my second class

class LogUserService implements SystemLogInterface
{
  public function setAttr(){}
  public function log(){}
}

My thought is that the second class is not able to implement the same interface because the consumer of this class can also invoke the setAttr() method.

For example

public function foo(LogUserService $service)
{
 $service->setAttr($this->getAttr())
 $service->log()
}

If i want to change the interface implementation i will need to also change the body of the method, which is wrong.

How am i supposed to approach this?

Note: setAttr() can not be moved to the constructor

Aucun commentaire:

Enregistrer un commentaire