douzhu7507 2018-08-29 20:37
浏览 55
已采纳

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

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 2018-08-30 21:59
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘