We have a persistence backed object, and it has a
versionfield in the database. Assume there’s lots of unrelated code in this class.
# # We're branching on this "version" field, and we return one # of 2 policies. # class Foo def policy if version == 'a' PolicyA.new else PolicyB.new end end end # # PolicyA calls a single check on a collaborator. # class PolicyA def pass?(collaborator) collaborator.check_x? end end # # PolicyB calls two different checks. # class PolicyB def pass?(collaborator) collaborator.check_x? && collaborator.check_y? end end # # One day, we need a 3rd policy version. # class Foo def policy if version == 'a' PolicyA.new elsif version == 'b' PolicyB.new else PolicyC.new end end end # # PolicyC needs 2 collaborators, so we add a second argument # and assign a default. # class PolicyA def pass?(collaborator_a, _collaborator_b = nil) collaborator_a.check_x? end end # # Same here. # class PolicyB def pass?(collaborator_a, _collaborator_b = nil) collaborator_a.check_x? && collaborator_a.check_y? end end # # Here we pass in a second collaborator and query it. # class PolicyC def pass?(collaborator_a, collaborator_b) collaborator_a.check_y? && collaborator_b.check_z? end end # # Some code has to use this thing. # class PolicyChecker def check(foo) foo.policy.pass?(collaborator_a, collaborator_b) end end
Is there a good way out of this that doesn’t involve subclassing Foo?
What about dealing with these collaborators? What happens when a third one comes along?
foo to get at
policy feels wrong, but
policy knowing about
foo.version also feels wrong.
Whats a good path forward from here?
Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.