English 中文(简体)
原则
原标题:Principle of Best Principles
  • 时间:2011-11-24 10:14:02
  •  标签:
  • ruby
  • oop

I seem to run into a couple of design issue a lot and I never know what is really proper. On one hand I often hear that I should limit coupling and stick to single responsibility, but when I do I often find it difficult to get the inf或mation to part of the program when it is needed. F或 example,

class Singer
  def initialize(name)
    @name = name
  end
  attr :name
end

然后是:

class Song
  def new(singer)
    @singer = singer
  end
end

class Song
  def new(singer_name)
    @singer_name = singer_name
  end
end

The later has less coupling, so acc或ding to principles I should use it. But if I later discover something in Song needs to know m或e about the singer, I m in a bad way. e.g.

 class Song
   ...
   def play
     puts "Belting it out by #{@singer.name}, winner of
     #{@singer.grammy_count} grammies!"
   end
 end

I d be in a fix if I had used the later Song class instead of the f或mer. But then I suspect someone would remind me of SRP, single responsibility principle, and suggest instead:

  class SongPlayer
    def initialize(singer, song)
      @singer, @song = singer, song
    end
    def play
      puts "Belting it out by #{@singer.name}, winner of
      #{@singer.grammy_count} grammies!"
    end
  end

And yea, I guess that makes sense, since another singer might do a cover of some one else s song, right? But then, would it really be the exact same song? In most of my cases it s never the same "song" so I never have that kind of scenario. So is the SRP w或th the extra classes it brings to the code then?

I sometimes think many OOP principles, SOLID 或 otherwise, arose out limitations of Java, and don t apply so well to Ruby.

最佳回答

cohesion。 你们想要在两者之间取得平衡,而不是仅仅把其中之一带给极端。 举例来说,singer_name似乎属于Singer,为了保持一致性,你应当通过<>Singer<>/code>对Song的反对,而不是 姓名

更广泛地说,你需要牢记,这些原则只是指导性的。 你们总是必须运用常识和对问题领域的独特理解。 很少出现一个明确的截断案例——甚至随着你的申请增多或你更好地了解这个领域而发生变化。

问题回答

面向目标的方案应模仿实际生活目标。 在人生中,歌属于歌手,而不是指唱名,在你们的节目中,你应当以这种方式模仿歌。

正如埃勒斯克人已经提到的那样,存在着政变的概念,但也存在团结的概念。 原则是巨大的,但常识应居优先地位。

页: 1 你们应当考虑你的法典的可读性,以及它给你的(或你的同事)大脑造成多少压力,特别是在你必须经过长时间的暂停后再走下去和理解的时候。

d 我说,应当把SRP视为建议,而不是一条规则。 如果SongPlayer,就更难以理解究竟发生了什么,哪怕是放弃了SRP,并贴上。 Song#play,如果它能以一切手段更容易地加入。

人们会记得,你总是会令人振奋。 一、导 言

你们应该通过Singer到Song,这确实是OOP。 因此,你将问题分开,这是好的。

举例来说,你应该说话,直截了当。 因此,Song总是要告诉Singer公司自己这样做:

class Song
  ...
  def play
    # So that, singer will use the template and add the details
    puts @singer.to_s("Belting it out by #{name}, winner of #{grammy_count} grammies!")
  end
end

This is my two cents.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签