總結(jié)一下工作中遇到的類擴(kuò)展:
1、類繼承:
當(dāng)多個(gè)類公用很多方法的時(shí)候可以將公用方法部分抽取出來,需要的類做相關(guān)繼承。
例子:
復(fù)制代碼 代碼如下:
class A ActiveRecord::Base
def a
p "it was a "
end
end
class BA
end
class CA
end
B.new.a #=>"it was a "
C.new.a #=>"it was a "
2、抽象類
當(dāng)多個(gè)類要繼承一個(gè)類時(shí),用第一種方法,會(huì)遇到一個(gè)問題。
(引用一個(gè)別人的注解來描述抽象類的運(yùn)用吧https://ihower.tw/rails4/activerecord-others.html)
單一表格繼承STI(Single-table inheritance)
如何將物件導(dǎo)向中的繼承概念,對(duì)應(yīng)到關(guān)聯(lián)式資料庫的設(shè)計(jì),是個(gè)大哉問。Rails內(nèi)建了其中最簡(jiǎn)單的一個(gè)解法,只用一個(gè)資料表儲(chǔ)存繼承體系中的物件,搭配一個(gè)type欄位用來指名這筆資料的類別名稱。
要開啟STI功能,依照慣例只要有一個(gè)欄位叫做type,型態(tài)字串即可。假設(shè)以下的posts資料表有欄位叫做type,那麼這三個(gè)Models實(shí)際上就會(huì)共用posts一個(gè)資料表,當(dāng)然,還有這兩個(gè)子類別也都繼承到父類別的validates_presence_of :subject:
復(fù)制代碼 代碼如下:
class Post ActiveRecord::Base
validates_presence_of :subject
end
class GuestPost Post
end
class MemberPost Post
end
讓我們進(jìn)入rails console實(shí)驗(yàn)看看,Rails會(huì)根據(jù)你使用的類別,自動(dòng)去設(shè)定type欄位:
復(fù)制代碼 代碼如下:
post = GuestPost.create( :subject => "guest")
post.type # "GuestPost"
post.id # 1
post = MemberPost.create( :subject => "member" )
post.id # 2
post.type # "MemberPost"
GuestPost.last # 1
很遺憾,也因?yàn)檫@個(gè)慣例的關(guān)係,你不能將type這麼名字挪做它用。
STI最大的問題在於欄位的浪費(fèi),如果繼承體系中交集的欄位不多,那麼使用STI就會(huì)非常的浪費(fèi)空間。如果有較多的不共用的欄位,筆者會(huì)建議不要使用這個(gè)功能,讓個(gè)別的類別有自己的資料表。要關(guān)閉STI,請(qǐng)父類別加上self.abstract_class = true
復(fù)制代碼 代碼如下:
class Post ActiveRecord::Base
self.abstract_class = true
end
class GuestPost Post
end
class MemberPost Post
end
這裡的GuestPost和MemberPost就需要有自己的Migrations建立guest_posts和member_posts資料表。
你還可以在某個(gè)類中,引入多個(gè)依賴
復(fù)制代碼 代碼如下:
class DependencyPost
require_dependency 'guestpost'
require_dependency 'memberpost'
end
3、類拓展混入
ruby的類是單繼承的,要實(shí)現(xiàn)多繼承的功能需要用mixin(參合模式)的方式,即類拓展混入來實(shí)現(xiàn)。例子:
復(fù)制代碼 代碼如下:
module Extract
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def a
p "it was a "
end
end
end
class AActiveRecord::Base
include Extract
end
A.new.a #=>"it was a"
4、代理類
當(dāng)某個(gè)功能是比較復(fù)雜的,當(dāng)然寫再lib中,作為一個(gè)面向函數(shù)的方法去處理很簡(jiǎn)單,也可以用代理類的方式實(shí)現(xiàn)面向?qū)ο蟮恼{(diào)用。
例子:
復(fù)制代碼 代碼如下:
class AActiveRecord::Base
def generate_schedule
generator = Generator::ExcelGenerator.new(self)
generator.generate_schedule
end
end
module Generator
class ExcelGenerator
attr_reader :excel,:workbook,:a,:worksheet
attr_accessor :styles
def initialize(a)
@excel ||= Axlsx::Package.new
@workbook ||= @excel.workbook
@worksheet = @workbook.add_worksheet(:name => '測(cè)試生成一個(gè)excel文件')
@a ||= a
@styles ||= Hash.new
end
def generate_schedule
#excel內(nèi)容的具體定義
end
end
end
A.new.generate_schedule 就可以通過代理類ExcelGenerator實(shí)現(xiàn)一個(gè)A的類實(shí)例方法generate_schedule
當(dāng)然也可以通過include 一個(gè)model的方式實(shí)現(xiàn)添加類實(shí)例方法,有時(shí)候也可以混合使用。另外使用代理類的好處在于多個(gè)類都需要相同方法的時(shí)候可以定義共同的部分,舉一個(gè)發(fā)郵件的例子:
復(fù)制代碼 代碼如下:
class AActiveRecord::Base
include SendEmail
end
class BActiveRecord::Base
include SendEmail
end
實(shí)現(xiàn)引入模塊:
復(fù)制代碼 代碼如下:
module SendEmail
#include this module to class::A and B
#use like that-- A.first.send_email
def send_email
Email.call_email(self)
end
end
實(shí)現(xiàn)代理類:
復(fù)制代碼 代碼如下:
class Email ActionMailer::Base
default :from => "test@email.com"
def self.call_email(obj)
define_method "#{obj.state}" do |obj|
@obj = obj
mail(:to => @obj.email, :subject => "XX標(biāo)題" )
end
send("#{obj.state}").deliver
#根據(jù)不同對(duì)象obj.state得到不同狀態(tài)下,定義不同方法,然后send派發(fā)調(diào)用相關(guān)對(duì)象狀態(tài)的模板。
end
end
RUBY很靈活當(dāng)然還有很多其他的方法實(shí)現(xiàn)更多的方式,以后再慢慢總結(jié)。
您可能感興趣的文章:- Ruby最簡(jiǎn)單的消息服務(wù)器代碼
- 淺析Ruby中繼承和消息的相關(guān)知識(shí)