问题
I have a couple of questions: following the perfect response that I got on that question Business logic dependent on model attribute.
I plan on usin a Strategy pattern, and I was wondering where I (in which folder) should put the classes files? should they go in the models folder? Also, I have a default behavior, do I still need to create a Interface class and have my default strategy derived from this, or I define my default strategy as the base strategy?
回答1:
This is a subjective answer, therefore it's likely it will be closed at some point.
As I provided an answer to the original question, let me try to answer this one.
There are two possible places where you may store this file:
applib
In this specific case, I'd go with the lib folder. For each Rails app I'm working on I generally have a specific folder within lib called with the same name of the project that represents the namespace where I store the business logic specific for my app.
For instance, if my project is called whatever, I have a lib/whatever along with a file lib/whatever.rb that is the root of my namespace.
module Whatever
end
Each file within the lib/whatever folder is nested within that namespace. In this case, you may create a folder specific for your commission strategies at lib/whatever/commissions and create the following files
# lib/whatever/commissions/alpha_strategy.rb
class Whatever::Commissions::AlphaStrategy
end
# lib/whatever/commissions/beta_strategy.rb
class Whatever::Commissions::BetaStrategy
end
To reduce the number of files, you can simply have a lib/whatever/commissions.rb file that will store all the strategies. This works well when the implementation of each strategy is relatively short.
# lib/whatever/commissions.rb
module Whatever::Commissions
class AlphaStrategy
end
class BetaStrategy
end
end
Let's see, instead, why I would not place these files in app. Again, this response is subjective and it's based on my personal experience with fairly large and complex Rails applications.
I've seen using /app for storing a lot of stuff. A common pattern, for example, is to create an app/workers folder within app. Which may make sense as workers are global (still, you can store them in the lib). In your case, what you are creating is really oriented towards a single model, therefore having a dedicated folder in /app is probably too much. Also, if you use a generic name such as strategies that will not be meaningful at all.
So why not app/models then? Well, in fact this may be the second alternative. However, I prefer to only store models in that folder and models, to me, means only persistence-related features.
What you are creating is not directly related to persistence. It uses a persisted object (an instance of a model), but it has nothing to do with saving data to the database. Therefore, it doesn't really make sense in the app/models.
In a small project you can likely save it in app/models, but I've worked with projects where you have over 30 models, and if you store all those not-persistence-related features in app/models then your folder will slowly become a mess.
来源:https://stackoverflow.com/questions/34121051/strategy-pattern-files-location-rails