For context, I have a SourceControlSystem
factory class that detects if a user is using Git, SVN, or another source control system and returns an object to interact with that source control system. The implementation is here, but I don't think it's very relevant.
So, at the start of my library, I have this single line:
@source_control_system = SourceControlSystem.create
Now the problem is that some classes depend on this variable. I have a Churn
class that needs this variable, but a Churn
class instance is only created inside of a Turbulence
class.
So I'm doing stuff like the following:
First, I pass @source_control_system
to a Turbulence
instance:
Turbulence.new(@source_control_system).data
And then inside the Turbulence
class:
class Turbulence
def initialize(source_control_system)
@source_control_system = source_control_system
end
...
# Turbulence doesn't need @source_control_system
# It merely passes it to Churn
def churn
@churn ||= Churn.new(@paths, @source_control_system).churn
end
end
And then I finally pass it to to the Churn
class:
class Churn
def initialize(paths, source_control_system)
@paths = paths
@source_control_system = source_control_system
end
# do stuff with @source_control_system here
end
Do you consider this to be a problem or not?
I don't think creating that variable is that expensive so I could just call SourceControlSystem.create
twice, once inside the Churn
class and another outside of it. Do you see any harm in that? I could do that but it seems... odd.
Or should SourceControlSystem
be some sort of global singleton object? That way, every class that needed it could instantly access it and call methods on it. You know, like the Math
module. That way, I wouldn't have to keep passing it as an argument to a bunch of classes.
How would you do it?
SourceControlSystem.create
? \$\endgroup\$SourceControlSystem.create
twice, but that didn't seem right to me. \$\endgroup\$