在 ruby​​ 中使用组合

发布于 2024-09-03 06:47:42 字数 148 浏览 1 评论 0原文

我是 Ruby 新人,但从事 .net 开发已有很多年了。我想在我的几个模型中实现组合,以确保它们尽可能松散耦合,但不知道从哪里开始,或者是否确实需要这样做,而我仍然像 .net 开发人员一样思考。

谁能给我一些关于从哪里开始的指示。

干杯 科林·G

I'm new Ruby but been a .net dev for many a year. I want to implement composition within a couple of my model to make sure they are as loosely coupled as possible but have no idea where to start, or if this is really needed and I'm still thinking to much like a .net dev.

Can anyone give me some pointers on where to start.

Cheers
Colin G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聊慰 2024-09-10 06:47:42

你是指这种事吗?

class Engine
    attr_reader :horsepower, :litres
end

class Gearbox
    attr_reader :manufacturer, :model_no
end

class Car
    def initialize(engine, gearbox)
        raise "Invalid Engine Object" if !engine.kind_of(Engine)
        raise "Invalid Gearbox Object" if !gearbox.kind_of(Gearbox)
        @engine = engine
        @gearbox = gearbox
    end
end


car = Car.new(Engine.new, Gearbox.new) 

Do you mean this sort of thing?

class Engine
    attr_reader :horsepower, :litres
end

class Gearbox
    attr_reader :manufacturer, :model_no
end

class Car
    def initialize(engine, gearbox)
        raise "Invalid Engine Object" if !engine.kind_of(Engine)
        raise "Invalid Gearbox Object" if !gearbox.kind_of(Gearbox)
        @engine = engine
        @gearbox = gearbox
    end
end


car = Car.new(Engine.new, Gearbox.new) 
挥剑断情 2024-09-10 06:47:42

Ruby 是一种面向对象的动态类型语言。作为一种动态语言,Ruby 开发人员比 .net 开发人员更倾向于使用反射和动态修改代码。当然,因为它是一种面向对象的语言,所以您可以使用与 .net 中基本相同的原则,而且您也应该这样做,但要始终环顾四周,看看如何以更动态的方式实现相同的功能。

例如,ActiveRecord ORM 使用composed_of 方法来解决组合问题,该方法将动态地将适当的字段和属性添加到您的类中。我并不是说这是应该这样做的方式(例如 DataMapper,这是 ruby​​ 的另一个 ORM,选择更“保守”的方法,因此更像 (Fluent)NHibernate),它只是一个例子如何以不同的方式做事。

像 AOP 或 DI 这样的东西对于动态语言来说并不是一个陌生的概念,它们通常只是以另一种方式完成。对语言的动态方面保持开放的态度,但不要过度。

Ruby is an object-oriented but dynamically typed language. Being a dynamic language, rubyists tend to use reflections and dynamic modifying of the code much more than .net developers. Of course because it's an object-oriented language you can use mostly the same principles as in .net, and you should too, but always look around and see how that same thing could be implemented in a more dynamic way.

For example the ActiveRecord ORM solves composition using a composed_of method that will dynamically add the appropriate fields and properties to your class. I'm not saying that this is the way it should be done (for example DataMapper, which is another ORM for ruby, choose a more "conservative" approach, and so resembles more like (Fluent)NHibernate), it's just an example of how things can be done differently.

Things like AOP, or DI aren't a foreign concept for dynamic languages, they are just usually done in an alternative way. Keep an open mind on the dynamic aspects of the language but don't overdo them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文