检查类中是否存在违反SOLID原则的方法?

问题描述 投票:2回答:2

我有一个名为Bird的类,它接受构造函数中的鸟类数组。我正在尝试在其中实现一个函数,它将检查当前是否有任何鸟类正在飞行,请记住所有代码都应符合SOLID原则。

我有以下两个班级(鹦鹉和鸵鸟)

class Parrot extends FlyingBirds{}   
class Ostrich extends BirdDetail{}

BirdDetail类

abstract class BirdDetail {

  protected $didNotSleepLastNight;

  public function __construct(bool $didNotSleepLastNight)
  {
    $this->didNotSleepLastNight= $didNotSleepLastNight;
  }

  public function didNotSleepLastNight(): bool
  {
    return $this->didNotSleepLastNight;
  }

}

FlyingBirds(并非所有的鸟类都能像鸵鸟一样飞翔)

abstract class FlyingBirds extends BirdDetail{

 protected $isFlyingNow;

 public function __construct(bool $didNotSleepLastNight, bool $isFlyingNow)
 {
    parent::__construct($didNotSleepLastNight);
    $this->isFlyingNow = $isFlyingNow;
 }

 public function isFlyingNow(): bool
 {
    return $this->isFlyingNow;
 }
}   

然后我有一个叫Bird的课

class Bird
{
  private $details;

 public function __construct(array $details)
 {
    $this->details = $details;
 }

 public function didNotSleepLastNight(): bool
 {
    foreach ($this->details as $detail) {

        if ($detail->didNotSleepLastNight()) {
            return true;
        }
    }

    return false;
 }

 public function isFlyingNow(): bool
 {
    foreach ($this->details as $detail) {

        if ($detail->isFlyingNow()) 
        {
            return true;
        }
    }
    return false;
  }
}

现在我将Parrot和Ostrich的实例传递给Bird Constructor

$bird = new Bird([new Parrot(true, false), new Ostrich(false)]);     

if($bird->isFlyingNow())
{
 echo "Yes";
}
else
{
 echo "No";
}

问题是上面的代码给了我以下错误

Fatal error: Uncaught Error: Call to undefined method Ostrich::isFlyingNow()

那是因为Ostrich / BirdDetail类没有名为isFlyingNow的方法。

可以通过使用以下代码替换Birds类中的isFlyingNow方法来解决此问题:

public function isFlyingNow(): bool
{

    foreach ($this->details as $detail) {

        if (method_exists($detail, 'isFlyingNow') && $detail->isFlyingNow())             
        {
            return true;
        }
    }

    return false;

}

您能否告诉我上述修复是否违反了SOLID原则?或者可以以更好的方式解决问题?

php oop solid-principles design-principles
2个回答
3
投票

没有真正打破SOLID,但根本没有很好的设计。

对于更简洁的方法,请使用接口。

由于并非所有的鸟类都可以飞行,因此对所有鸟类采用面向公众的方法isFlying()似乎相当浪费。

也许你可以这样做:

您的基础Bird类,您可以在其中定义所有鸟类的常见属性和方法。

class Bird {
}

然后,用不同的接口来描述Bird子类的不同可能行为。有些鸟飞,有些游泳,有人说话,有些人唱歌,有些人潜水,有些人可以跑,等等。

传单:

interface FlyingBird {

    function isFlying():bool;

    function takeOff();

    function land();

    function fly($bearing, $distance);

}

对于谈话者:

interface TalkingBird {

    function say($something);
}

歌手:

interface SingingBird {
    function sing(array $notes);
}

游泳者(你可能需要区分那些在水面上游泳的人和那些可以在水面下潜水的游泳者)。

interface SwimmingBird {
   function isSwimming(): bool;
   // etc
}

对于跑步者:

interface RunningBird {
   function isRunning(): bool;
   // etc
}

然后你可以有像Parrot(苍蝇和谈话,但不唱歌,跑步或游泳)的课程

class Parrot extends Bird implements TalkingBird, FlyingBird {
    // todo: actual implementation
}

或者Ostrich(可以跑,但不会游泳,唱歌,说话或飞行):

class Ostrich extend Birds implements RunningBird { /* implementation */}

甚至Penguin(可游泳,不能飞,不能跑,不能唱歌,不能说话):

class Penguin extends Bird implements SwimmingBird { /* implementation */ }

等等整个@package Ornithology正在形成。

这些类的用户应检查实例是否正在实现适当的接口:

if ($birdInstance instanceof FlyingBird && $birdInstance->isFlying()) {
   echo "This bird is flying!";
}

为了简化这些类的组合,您可能需要创建一些特征:

EG

trait FlyingBirdTrait {

    private $flying = false;

    function isFlying():bool {
       return $this->flying;
    }

    function takeOff() {
       $this->flying = true;
    }

    function land() {
       $this->flying = false;
    }

    function fly($bearing, $altitude, $distance) {
       if (!$this->isFlying()) {
           $this->takeOff();
       }
       // calculate new position;

    }
}

然后像Parrot这样的类可以使用:

class Parrot extends Bird implements TalkingBird, FlyingBird {
    uses FlyingBirdTrait;
    // rest of the implementation, etc;
}

-1
投票

这不违反SOLID原则。如果Bird子类拒绝继承的方法,那将违反Liskov原则,但这不是你正在做的事情。

我同意你的代码不优雅。更清晰的解决方案是在Bird超类中定义一个isFlyingNow抽象方法,实现在鸵鸟和其他非飞行类中返回false。然后,您不需要在运行时检查对象是否提供此方法。

© www.soinside.com 2019 - 2024. All rights reserved.