策略模式文件位置Rails [英] Strategy pattern files location Rails

查看:110
本文介绍了策略模式文件位置Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题:
遵循这个问题的完美答复依赖于模型属性的业务逻辑

我计划使用一个策略模式,我想知道我在哪个文件夹放上课文件?他们应该在模型文件夹中?
此外,我有一个默认行为,还需要创建一个Interface类,并从中导出我的默认策略,或者我将默认策略定义为基本策略?

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?

推荐答案

这是一个主观的答案,因此有可能在某些时候关闭。

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:


  • 应用程序

  • lib

  • app
  • lib

在这种具体情况下,我将使用 lib 文件夹。对于我正在开发的每个Rails应用程序,我通常会在lib中调用一个具有相同名称的项目的特定文件夹,该文件名代表我存储我应用程序的业务逻辑的命名空间。

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.

例如,如果我的项目被称为任何,我有一个 lib / whatever 文件 lib / whatever.rb 这是我的命名空间的根。

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

lib /中的每个文件文件夹嵌套在该命名空间中。在这种情况下,您可以在 lib / whatever /佣金创建一个特定于您的佣金策略的文件夹,并创建以下文件

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

为了减少文件数量,您只需要一个 lib / whatever / commissions.rb 文件即可存储所有的策略。当每个策略的实现相对较短时,这个功能很好。

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

相反,为什么我不会将这些文件放在 app 中。再次,这种反应是主观的,它是基于我在相当大和复杂的Rails应用程序的个人经验。

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.

我已经看到使用 / app 用于存储很多东西。例如,常见的模式是在应用程序内创建一个 app / workers 文件夹。这可能是工作人员是全球性的(仍然可以将它们存储在 lib 中)。在您的情况下,您正在创建的是真正面向单个模型,因此在 / app 中的专用文件夹可能太多了。此外,如果您使用通用名称,例如策略,根本不会有意义。

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.

在一个小项目中,您可能会保存在 app / models 中,但是我已经与您拥有超过30个模型的项目合作,如果您将所有不持久相关功能存储在 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.

这篇关于策略模式文件位置Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆