dpe77294 2018-10-15 10:52
浏览 73

通过多个对象传递参数 - 仅在最终对象中使用

I have a service class that contains a number of objects that hold different kinds of data and their respective behaviour. One of these objects has other objects that use some of the data of the first two objects...try saying that 10 times fast!

So in the example below, object3 takes arguments x and y from object1 and parameter z from object2. In turn, these parameters aren't "used" by object3, but are passed through as arguments into object3_1 and object3_2.

class ServiceClass {
    protected $object1;
    protected $object2;
    protected $object3;

    public function doSomething (){
        $this->object3->calculateSomething($x, $y, $z);
    }
}

class Object1Class {
    protected $data = {"x"=>"y"};

    public function getX (){
        //return x
            }

    public function getY (){
        //return y
    }
}

class Object2Class {
    protected $data = "I'm data!";

    public function getData (){
        //return string
    }
}

class Object3Class {
    protected $object3_1;
    protected $object3_2;

    public function calculateSomething ($x, $y, $z){
        $this->object3_1->getSomething($x, $y);
        $this->object3_2->getSomethingElse($x, $y, $z);
            }
    }

So basically i'm trying to find the best way of getting information from the service, to object3_1/3_2?

The 4 ways I've got so far are:

  1. Pass data from objects into object3 method and then pass on to object3_1/3_2

    $this->object3->calculateSomething($this->object1->getX(), $this->object1->getY(), $this->object2->getZ());
    
  2. Pass object1 and object2 into object3 then either pass data to object3_1/3_2 or pass the objects again

    $this->object3->calculateSomething($this->object1, $this->object2);
    
  3. Create a new class that contains properties and behaviour for the required data and pass that around.

    class DataClass {
        protected $xy;
        protected $z;
    
        public function __contruct($xy, $z){
            $this->xy = $xy;
            $this->z = $z;
        }
    
        public function getX(){
            // return x
        };
    
        public function getY(){
            // return y
        };
    
        public function getZ(){
            // return z
        };
    }
    
    $this->object3->calculateSomething($DataObject);
    
  4. Create an array of the data elements and pass that around.

All of the options work, it just doesn't feel right passing the same argument through multiple methods. In the contrived example above, I've only made it go 2 classes deep for the sake of brevity, but in some areas of my project data has to be passed up to 6 layers deep before it's utilised.

With option 1, the parameter list has the real chance of growing in number - not that the final calculation method is doing too many things, but just requires all these bits of data for a single calculation. Option 3 seems attractive to me but it would duplicate the behaviour code from the original objects - so then passing the objects themselves seems better...and that's the circle i've been going in for the last few days.

Is there a better way of doing this, or am I just over thinking things?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么