这个设计是否代表循环依赖?

huangapple go评论59阅读模式
英文:

Does this design represent circular dependency?

问题

问题1:

在给定的代码行self.sensor = Sensor(id, self)中,我不明白为什么这行代码可以正常运行,因为这不是一个循环依赖吗?(在参数中,self 是一个 Car 对象,而在传递给 Sensor 初始化函数时,它还没有完全创建(初始化)完成。)

问题2:

如果这是一个循环依赖,我该如何解决它?

回答:

是的,这是一个潜在的循环依赖问题。在这个代码中,Car 类的初始化函数中创建了一个 Sensor 对象,并将 selfCar 对象)作为参数传递给 Sensor 的初始化函数。这在某些情况下可能导致问题,因为 self 可能尚未完全初始化。

要解决这个问题,您可以考虑将 Sensor 对象的创建延迟到 Car 对象初始化完成后。您可以在 Car 类的初始化函数中留下一个占位符,然后在初始化完成后设置 sensor 属性。例如:

from random import uniform

class Car:    
    def __init__(self, position):
        self.position = position
        self.sensor = None  # 留下一个占位符

    def initialize_sensor(self):
        self.sensor = Sensor(id, self)

class Sensor:
    def __init__(self, id, carrier_car):
        self.id = id
        self.position = self.compute_position(carrier_car)

    def compute_position(self, carrier_car):
        return [carrier_car.position[0] + uniform(1, 3), carrier_car.position[1] + uniform(2, 4)]

car1 = Car([10, 10])
car1.initialize_sensor()  # 在初始化完成后设置sensor属性
print("This car's sensor coordinate is ({}, {}).".format(car1.sensor.position[0], car1.sensor.position[1]))

通过将 sensor 属性的设置延迟到 initialize_sensor 方法中,您可以确保 Car 对象已经完全初始化后再创建 Sensor 对象,避免了潜在的循环依赖问题。

英文:

Imagine this example (that may be run without any errors):

from random import uniform

class Car:    
    def __init__(self, position):
        self.position = position
        self.sensor = Sensor(id, self)

class Sensor:
    def __init__(self, id, carrier_car):
        self.id = id
        self.position = self.compute_position(carrier_car)

    def compute_position(self, carrier_car):
        return [carrier_car.position[0] + uniform(1,3), carrier_car.position[0] + uniform(2,4)]
        
car1 = Car([10,10])
print("This car's sensor coordinate is ({},{}).".format(car1.sensor.position[0], car1.sensor.position[1]))

Question 1.

Given the line self.sensor = Sensor(id, self), I don't understand how this line runs without any problem. Isn't this circular dependency? (self, in the argument, is an object of Car which has not yet been completely created (initialized) at the point it is passed to the initializer of Sensor.)

Question 2.

If this a circular dependency, how can I resolve it?

答案1

得分: 0

The self object that is passed to Sensor(id, self) is an already existing Car instance, but it does not have a sensor attribute yet.

Since Sensor only requires the position attribute of the carrier_car object, this is not a problem, since it has already been created in the line self.position = position.

In any case, this design is a bit confusing. I would not pass a Car object to Sensor, but just a position, as this is all that Sensor needs to know about the car. This would also simplify the code:

from random import uniform

class Car:    
    def __init__(self, position):
        self.position = position
        self.sensor = Sensor(id, position)

class Sensor:
    def __init__(self, id, position):
        self.id = id
        self.position = self.compute_position(position)

    def compute_position(self, position):
        return [position[0] + uniform(1,3), position[0] + uniform(2,4)]
        
car1 = Car([10,10])
print("This car's sensor coordinate is ({},{}).".format(car1.sensor.position[0], car1.sensor.position[1]))
英文:

The self object that is passed to Sensor(id, self) is an already existing Car instance, but it does not have a sensor attribute yet.

Since Sensor only requires the position attribute of the carrier_car object, this is not a problem, since it has already been created in the line self.position = position.

In any case, this design is a bit confusing. I would not pass a Car object to Sensor, but just a position, as this is all that Sensor needs to know about the car. This would also simplify the code:

from random import uniform

class Car:    
    def __init__(self, position):
        self.position = position
        self.sensor = Sensor(id, position)

class Sensor:
    def __init__(self, id, position):
        self.id = id
        self.position = self.compute_position(position)

    def compute_position(self, position):
        return [position[0] + uniform(1,3), position[0] + uniform(2,4)]
        
car1 = Car([10,10])
print("This car's sensor coordinate is ({},{}).".format(car1.sensor.position[0], car1.sensor.position[1]))

huangapple
  • 本文由 发表于 2023年2月18日 07:21:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定