I have a collection of instances of a class called LogicSignal
I have a set of methods and "operator" methods I can call on these instances, for example
anded = signal_a & signal_b.value_at_cycle_n_minus(2)
will give me the bitwise and of the value of signal_a at the current time and the value of signal_b at the current cycle - 2.
The signals change with time, so when I run this
puts signal_a & signal_b
advance_time
puts signal_a & signal_b
I will get the values anded at different times
But if I run this
anded = signal_a & signal_b
puts added
advance_time
puts anded
I will get the same values printed.
The problem is that I need to pass arbitrary functions of signals into other objects, and they need to be able to get the results of those functions at arbitrary times.
I know that if run this
anded = lambda { signal_a & signal_b }
puts anded.call
advance_time
puts anded.call
I will get the values anded at different times, but anded
is not a signal, so I can't use it in place of a signal. So for example if some object needed to be able to get the anded
value at some arbitrary point in time, it could not call anded.value_at_cycle_n_minus() , because anded
is a proc and not a signal
My first thought is to use something like the composite pattern and create a CompositeLogicSignal class that implents all the methods of the LogicSignal class but does so by calling a proc that takes the child signals.
So I would have something like this
anded = CompositeLogicSignal.new [signal_a, signal_b] , lambda { |a,b| a & b }
Is there an easier way, or is there some other pattern that makes more sense for this problem?
Aucun commentaire:
Enregistrer un commentaire