开发者

Ruby modules that use some common method:where to put this method?

开发者 https://www.devze.com 2023-03-04 06:41 出处:网络
Given some Uploader Class, I have three modules included in it. They have each their own distinctive uses and that\'s why I divided them into three modules. But, as it turns out, there is one method

Given some Uploader Class, I have three modules included in it.

They have each their own distinctive uses and that's why I divided them into three modules. But, as it turns out, there is one method that all three modules need.

So, it is so far obvious to me that there must be a better way than defining this method in each of these three modules. Should I make a fourth module and put 开发者_如何学编程this method in it, and have these original three modules include this fourth module? Or is there a better way?


Yes, it sounds like you should have a fourth module. But it sounds like you might actually need a class hierarchy rather than independent, flat modules. Apply the 'is a' test - can you say an UploaderA instance is an Uploader? If so, you should have

class Uploader
  include FourthModule  # with shared functionality for all uploaders
  # or just go ahead and put the shared functionality right in the Uploader class here.
end

class UploaderA < Uploader
end
0

精彩评论

暂无评论...
验证码 换一张
取 消