douzhu7507
douzhu7507
2018-08-29 20:37

注入特征或对象以分离对象,这会改变注入的对象状态

已采纳

What is more correct to use for multiple inheritance in the below case? Is the second method follows the SOLID principle correctly? Or any common design pattern can be used instead?

This is a simplified concept code. Many class needs to extend the Image class like BicycleImage, BusImage. Some of them needs to addNoise() but not every one. Also more "state changing" classes are needed like sharpenEdges, increaseIntesity, etc. Which not every child class use.

Base class:

<?php

abstract class Image
{
    private $pixels;

    public function setPixel(int $x, int $y, int $intensity): void
    {
        $this->pixels[$x][$y] = $intensity;
    }

    public function saveImage(): void {}
}

Trait method:

trait RandomNoiseTrait
{
    public function addNoise(): void
    {
        $this->setPixel(mt_rand(1, 100), mt_rand(1, 100), mt_rand(1, 100));
    }
}

class CarImage extends Image
{
    use RandomNoiseTrait;

    public function drawCar(): void {}
}

$carImage = new CarImage();
$carImage->drawCar();
$carImage->addNoise();
$carImage->saveImage();

Object injected to separate object which alters the injected objects state:

class CarImage extends Image
{
    public function drawCar(): void {}
}

class RandomNoise
{
    private $image;

    public function __construct(Image $image)
    {
        $this->image = $image;
    }

    public function addNoise(): void
    {
        $this->image->setPixel(mt_rand(1, 100), mt_rand(1, 100), mt_rand(1, 100));
    }
}

$carImage = new CarImage();
$carImage->drawCar();

$noise = new RandomNoise($carImage);
$noise->addNoise();

$carImage->saveImage();
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzi5214 douzi5214 3年前

    Your question is largely opinion based, because there are often many ways to solve a given architectural or logic problem.

    In this instance, based on the information you've provided, I would most likely opt for something like the Strategy design pattern. This isn't actually too dissimilar to what you suggest in your question.

    An example of this design pattern in the context of your question might be:

    namespace Image;
    
    use Image\Manipulation\Manipulation;
    
    abstract class Image
    {
        protected $pixels = [];
    
    
        public function setPixel(int $x, int $y, int $intensity): self
        {
            $this->pixels[$x][$y] = $intensity;
    
            return $this;
        }
    
        public function manipulate(Manipulation $manipulation): self
        {
            $manipulation->setImage($this)->manipulate();
    
            return $this;
        }
    
        abstract public function draw(): string;
    
        public function save(): bool
        {
            return true;
        }
    }
    
    
    namespace Image\Manipulation;
    
    use Image\Image;
    
    abstract class Manipulation
    {
        protected $image;
    
    
        public function setImage(Image &$image): self
        {
            $this->image = $image;
    
            return $this;
        }
    
        abstract public function manipulate();
    }
    
    
    namespace Image\Manipulation;
    
    class Noise extends Manipulation
    {
        public function manipulate()
        {
            $this->image->setPixel(mt_rand(1, 100), mt_rand(1, 100), mt_rand(1, 100));
        }
    }
    

    With it's usage being:

    $image = new Image\CarImage;
    $noise = new Image\Manipulation\Noise;
    
    $image->draw();
    $image->manipulate($noise)->draw();
    
    点赞 评论 复制链接分享