侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Ruby中编写类与模块的风格指南

技术  /  管理员 发布于 6年前   312

在 class 定义里使用一致的结构。

    

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。类别应该只在创建实例是合理的时候使用。

   

 # 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

    当你希望将模块的实例方法变成 class 方法时,偏爱使用 module_function 胜过 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.

    在设计类层次的时候确保他们符合 Liskov Substitution Principle 原则。(译者注: LSP原则大概含义为: 如果一个函数中引用了 父类的实例, 则一定可以使用其子类的实例替代, 并且函数的基本功能不变. (虽然功能允许被扩展))

        Liskov替换原则:子类型必须能够替换它们的基类型 <br/>
        1. 如果每一个类型为T1的对象o1,都有类型为T2的对象o2,使得以T1定义的所有程序P在所有的对象o1都代换为o2时,程序P的行为没有变化,那么类型T2是类型T1的子类型。 <br/>
        2. 换言之,一个软件实体如果使用的是一个基类的话,那么一定适用于其子类,而且它根本不能察觉出基类对象和子类对象的区别。只有衍生类替换基类的同时软件实体的功能没有发生变化,基类才能真正被复用。 <br/>
        3. 里氏代换原则由Barbar Liskov(芭芭拉.里氏)提出,是继承复用的基石。 <br/>
        4. 一个继承是否符合里氏代换原则,可以判断该继承是否合理(是否隐藏有缺陷)。

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

    总是为你自己的类提供 to_s 方法, 用来表现这个类(实例)对象包含的对象.

   

 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 功能成员来定义各个实例变量的访问器或者修改器方法。

  

 # 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(构造函数) 和 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),构造器(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 一个 Struct.new - 它已经是一个新的 class。扩展它会产生一个多余的 class 层级
    并且可能会产生怪异的错误如果文件被加载多次。

    考虑添加工厂方法来提供灵活的方法来创建特定类实例。

    

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

    鸭子类型(duck-typing)优于继承。

  

 # 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.

    避免使用类变量(@@)因为他们讨厌的继承习惯(在子类中也可以修改父类的类变量)。

   

 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"

    正如上例看到的, 所有的子类共享类变量, 并且可以直接修改类变量,此时使用类实例变量是更好的主意.

    根据方法的用途为他们分配合适的可见度( private, protected ),不要让所有的方法都是 public (这是默认设定)。这是 Ruby 不是 Python。

    public, protected, 和 private 等可见性关键字应该和其(指定)的方法具有相同的缩进。并且不同的可见性关键字之间留一个空格。

   

 class SomeClass   def public_method    # ...   end   private   def private_method    # ...   end   def another_private_method    # ...   end  end

    使用 def self.method 来定义单例方法. 当代码重构时, 这将使得代码更加容易因为类名是不重复的.

  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 c test2 凫锻活e,但 test2 具有重新定x的 size 方法,因此烧叩男灰印V唤o予我晃锛姆椒ǚQ榈ダ椒 (singleton method)。


  • 上一条:
    DEDE模板中如何运行php脚本和变量在需要操作数据库字段时
    下一条:
    Perl命令行应用程序详解
  • 昵称:

    邮箱:

    1条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客