濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Ruby中編寫(xiě)類與模塊的風(fēng)格指南

Ruby中編寫(xiě)類與模塊的風(fēng)格指南

熱門標(biāo)簽:徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個(gè)好 高德地圖標(biāo)注客服 湖州u友防封電銷卡 滴滴外呼系統(tǒng) 百度地圖標(biāo)注自定義圖片 電銷機(jī)器人廠商代理 白銀外呼paas系統(tǒng) 地圖標(biāo)注賺錢項(xiàng)目注冊(cè) 常德電銷平臺(tái)外呼系統(tǒng)軟件價(jià)格

在 class 定義里使用一致的結(jié)構(gòu)。

    

class Person
   # extend and include go first
   extend SomeModule
   include AnotherModule

   # constants are next
   SOME_CONSTANT = 20

   # afterwards we have attribute macros
   attr_reader :name

   # followed by other macros (if any)
   validates :name

   # public class methods are next in line
   def self.some_method
   end

   # followed by public instance methods
   def some_method
   end

   # protected and private methods are grouped near the end
   protected

   def some_protected_method
   end

   private

   def some_private_method
   end
  end

    傾向使用 module,而不是只有類方法的 class。類別應(yīng)該只在創(chuàng)建實(shí)例是合理的時(shí)候使用。

   

 # bad
  class SomeClass
   def self.some_method
    # body omitted
   end

   def self.some_other_method
   end
  end

  # good
  module SomeClass
   module_function

   def some_method
    # body omitted
   end

   def some_other_method
   end
  end

    當(dāng)你希望將模塊的實(shí)例方法變成 class 方法時(shí),偏愛(ài)使用 module_function 勝過(guò) extend self。

 

  # bad
  module Utilities
   extend self

   def parse_something(string)
    # do stuff here
   end

   def other_utility_method(number, string)
    # do some more stuff
   end
  end

  # good
  module Utilities
   module_function

   def parse_something(string)
    # do stuff here
   end

   def other_utility_method(number, string)
    # do some more stuff
   end
  end

    When designing class hierarchies make sure that they conform to the
    Liskov Substitution Principle.

    在設(shè)計(jì)類層次的時(shí)候確保他們符合 Liskov Substitution Principle 原則。(譯者注: LSP原則大概含義為: 如果一個(gè)函數(shù)中引用了 父類的實(shí)例, 則一定可以使用其子類的實(shí)例替代, 并且函數(shù)的基本功能不變. (雖然功能允許被擴(kuò)展))

        Liskov替換原則:子類型必須能夠替換它們的基類型 br/>
        1. 如果每一個(gè)類型為T1的對(duì)象o1,都有類型為T2的對(duì)象o2,使得以T1定義的所有程序P在所有的對(duì)象o1都代換為o2時(shí),程序P的行為沒(méi)有變化,那么類型T2是類型T1的子類型。 br/>
        2. 換言之,一個(gè)軟件實(shí)體如果使用的是一個(gè)基類的話,那么一定適用于其子類,而且它根本不能察覺(jué)出基類對(duì)象和子類對(duì)象的區(qū)別。只有衍生類替換基類的同時(shí)軟件實(shí)體的功能沒(méi)有發(fā)生變化,基類才能真正被復(fù)用。 br/>
        3. 里氏代換原則由Barbar Liskov(芭芭拉.里氏)提出,是繼承復(fù)用的基石。 br/>
        4. 一個(gè)繼承是否符合里氏代換原則,可以判斷該繼承是否合理(是否隱藏有缺陷)。

    努力使你的類盡可能的健壯 [SOLID](http://en.wikipedia.org/wiki/SOLID_object-oriented_design\))。(

    總是為你自己的類提供 to_s 方法, 用來(lái)表現(xiàn)這個(gè)類(實(shí)例)對(duì)象包含的對(duì)象.

   

 class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end

   def to_s
    "#@first_name #@last_name"
   end
  end

    使用 attr 功能成員來(lái)定義各個(gè)實(shí)例變量的訪問(wèn)器或者修改器方法。

  

 # bad
  class Person
   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end

   def first_name
    @first_name
   end

   def last_name
    @last_name
   end
  end

  # good
  class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

    避免使用 attr。使用 attr_reader 和 attr_accessor 作為替代。

  # bad - creates a single attribute accessor (deprecated in 1.9)
  attr :something, true
  attr :one, :two, :three # behaves as attr_reader

  # good
  attr_accessor :something
  attr_reader :one, :two, :three

    考慮使用 Struct.new, 它可以定義一些瑣碎的 accessors,
    constructor(構(gòu)造函數(shù)) 和 comparison(比較) 操作。

  # good
  class Person
   attr_reader :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

  # better
  class Person  Struct.new(:first_name, :last_name)
  end

    考慮使用 Struct.new,它替你定義了那些瑣碎的存取器(accessors),構(gòu)造器(constructor)以及比較操作符(comparison operators)。

  # good
  class Person
   attr_accessor :first_name, :last_name

   def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   end
  end

  # better
  Person = Struct.new(:first_name, :last_name) do
  end

    不要去 extend 一個(gè) Struct.new - 它已經(jīng)是一個(gè)新的 class。擴(kuò)展它會(huì)產(chǎn)生一個(gè)多余的 class 層級(jí)
    并且可能會(huì)產(chǎn)生怪異的錯(cuò)誤如果文件被加載多次。

    考慮添加工廠方法來(lái)提供靈活的方法來(lái)創(chuàng)建特定類實(shí)例。

    

class Person
   def self.create(potions_hash)
    # body omitted
   end
  end

    鴨子類型(duck-typing)優(yōu)于繼承。

  

 # bad
  class Animal
   # abstract method
   def speak
   end
  end

  # extend superclass
  class Duck  Animal
   def speak
    puts 'Quack! Quack'
   end
  end

  # extend superclass
  class Dog  Animal
   def speak
    puts 'Bau! Bau!'
   end
  end

  # good
  class Duck
   def speak
    puts 'Quack! Quack'
   end
  end

  class Dog
   def speak
    puts 'Bau! Bau!'
   end
  end

    Avoid the usage of class (@@) variables due to their "nasty" behavior
    in inheritance.

    避免使用類變量(@@)因?yàn)樗麄冇憛挼睦^承習(xí)慣(在子類中也可以修改父類的類變量)。

   

 class Parent
   @@class_var = 'parent'

   def self.print_class_var
    puts @@class_var
   end
  end

  class Child  Parent
   @@class_var = 'child'
  end

  Parent.print_class_var # => will print "child"

    正如上例看到的, 所有的子類共享類變量, 并且可以直接修改類變量,此時(shí)使用類實(shí)例變量是更好的主意.

    根據(jù)方法的用途為他們分配合適的可見(jiàn)度( private, protected ),不要讓所有的方法都是 public (這是默認(rèn)設(shè)定)。這是 Ruby 不是 Python。

    public, protected, 和 private 等可見(jiàn)性關(guān)鍵字應(yīng)該和其(指定)的方法具有相同的縮進(jìn)。并且不同的可見(jiàn)性關(guān)鍵字之間留一個(gè)空格。

   

 class SomeClass
   def public_method
    # ...
   end

   private

   def private_method
    # ...
   end

   def another_private_method
    # ...
   end
  end

    使用 def self.method 來(lái)定義單例方法. 當(dāng)代碼重構(gòu)時(shí), 這將使得代碼更加容易因?yàn)轭惷遣恢貜?fù)的.

  class TestClass
   # bad
   def TestClass.some_method
    # body omitted
   end

   # good
   def self.some_other_method
    # body omitted
   end

   # Also possible and convenient when you
   # have to define many singleton methods.
   class  self
    def first_method
     # body omitted
    end

    def second_method_etc
     # body omitted
    end
   end
  end

  class SingletonTest
   def size
    25
   end
  end

  test1 = SingletonTest.new
  test2 = SingletonTest.new
  def test2.size
   10
  end
  test1.size # => 25
  test2.size # => 10

    本例中,test1 與 test2 屬於同一類別,但 test2 具有重新定義的 size 方法,因此兩者的行為會(huì)不一樣。只給予單一物件的方法稱為單例方法 (singleton method)。

您可能感興趣的文章:
  • Ruby編程中的命名風(fēng)格指南
  • Ruby編程中的語(yǔ)法使用風(fēng)格推薦
  • 淺析Ruby的源代碼布局及其編程風(fēng)格
  • 你應(yīng)該知道的Ruby代碼風(fēng)格
  • GitHub倡導(dǎo)的Ruby代碼編寫(xiě)風(fēng)格總結(jié)

標(biāo)簽:普洱 荊門 公主嶺 永州 梧州 張家界 遼寧 三沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby中編寫(xiě)類與模塊的風(fēng)格指南》,本文關(guān)鍵詞  Ruby,中,編寫(xiě),類,與,模塊,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby中編寫(xiě)類與模塊的風(fēng)格指南》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Ruby中編寫(xiě)類與模塊的風(fēng)格指南的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    中山市| 临湘市| 上林县| 四平市| 桂阳县| 五华县| 镇原县| 永宁县| 蓝山县| 驻马店市| 祁门县| 肥乡县| 定南县| 乌什县| 柞水县| 达拉特旗| 本溪市| 鲜城| 长阳| 隆回县| 朝阳县| 平阳县| 叶城县| 酉阳| 清水县| 石泉县| 金堂县| 江门市| 从江县| 平阴县| 南靖县| 绥中县| 张家川| 来宾市| 逊克县| 墨脱县| 洛隆县| 阿拉善左旗| 阿拉善盟| 德江县| 张掖市|