標籤:
需求:
實現一個簡曆類,必須要有姓名,可以設定性別和年齡,可以設定工作經曆。最終需要三個簡曆
初步實現:
# -*- encoding: utf-8 -*-#簡曆類class Resume attr_accessor :name, :sex, :age, :time_area, :company def initialize(name) @name = name end def set_personal_info(sex, age) @sex = sex @age = age end def set_work_experience(time_area, company) @time_area = time_area @company = company end def display puts "#{name} #{age} #{sex}" puts "工作經曆 #{time_area} #{company}" endendresume1 = Resume.new(‘大鳥‘)resume1.set_personal_info(‘男‘, ‘29‘)resume1.set_work_experience(‘1998-2000‘, ‘xx公司‘)resume2 = Resume.new(‘大鳥‘)resume2.set_personal_info(‘男‘, ‘29‘)resume2.set_work_experience(‘2000-2002‘, ‘xx公司‘)resume3 = Resume.new(‘大鳥‘)resume3.set_personal_info(‘男‘, ‘29‘)resume3.set_work_experience(‘2002-2004‘, ‘xx公司‘)resume1.displayresume2.displayresume3.display
存在的問題:如果要20份,就需要執行個體化20次。如果改一個年齡,也要改20次,這樣寫很麻煩。
resume1 = Resume.new(‘大鳥‘)resume1.set_personal_info(‘男‘, ‘29‘)resume1.set_work_experience(‘1998-2000‘, ‘xx公司‘)resume2 = resume1resume2.set_personal_info(‘男‘, ‘29‘)resume2.set_work_experience(‘2000-2002‘, ‘xx公司‘)resume3 = resume1resume3.set_personal_info(‘男‘, ‘29‘)resume3.set_work_experience(‘2002-2004‘, ‘xx公司‘)resume1.displayresume2.displayresume3.display
對於最終一部分,簡單將resume1賦值給resume2和resume3是不對的,因為這樣僅僅是傳引用,而沒有傳值,resume1、resume2和resume3指向的是同一個地址。
原型模式:從一個對象再建立另一個可定製的對象,而且不需要知道任何建立的細節。
# -*- encoding: utf-8 -*-#簡曆類class Resume attr_accessor :name, :sex, :age, :time_area, :company def initialize(name) @name = name end def set_personal_info(sex, age) @sex = sex @age = age end def set_work_experience(time_area, company) @time_area = time_area @company = company end def display puts "#{name} #{age} #{sex}" puts "工作經曆 #{time_area} #{company}" end def self_clone self.clone endendresume1 = Resume.new(‘大鳥‘)resume1.set_personal_info(‘男‘, ‘29‘)resume1.set_work_experience(‘1998-2000‘, ‘xx公司‘)resume2 = resume1.self_cloneresume2.set_personal_info(‘男‘, ‘28‘)resume3 = resume1.self_cloneresume3.set_personal_info(‘男‘, ‘29‘)resume3.set_work_experience(‘2002-2004‘, ‘xx公司‘)resume1.displayresume2.displayresume3.display
Resume類中增加了self_clone方法,resume2和resume3來自resume1.self_clone,這樣只需要執行個體化一次,修改值也比較方便。
存在一個問題,如果工作經驗複雜一些,需要一個類來實現會怎麼樣
# -*- encoding: utf-8 -*-#簡曆類class Resume attr_accessor :name, :sex, :age, :work_experience def initialize(name) @name = name @work_experience = WorkExperience.new end def set_personal_info(sex, age) @sex = sex @age = age end def set_work_experience(time_area, company) @work_experience.time_area = time_area @work_experience.company = company end def display puts "#{name} #{age} #{sex}" puts "工作經曆 #{@work_experience.time_area} #{@work_experience.company}" end def self_clone self.clone endendclass WorkExperience attr_accessor :time_area, :companyendresume1 = Resume.new(‘大鳥‘)resume1.set_personal_info(‘男‘, ‘29‘)resume1.set_work_experience(‘1998-2000‘, ‘xx公司‘)resume2 = resume1.self_cloneresume2.set_personal_info(‘男‘, ‘30‘)resume2.set_work_experience(‘2000-2002‘, ‘xx公司‘)resume3 =resume1.self_cloneresume3.set_personal_info(‘男‘, ‘31‘)resume3.set_work_experience(‘2002-2004‘, ‘xx公司‘)resume1.displayresume2.displayresume3.display#大鳥 29 男#工作經曆 2002-2004 xx公司#大鳥 30 男#工作經曆 2002-2004 xx公司#大鳥 31 男#工作經曆 2002-2004 xx公司
可以看出三個簡曆的工作經曆是相同的,因為對於對象,clone的只是對象的引用,三個resume用到的是同一個工作經曆。
淺拷貝與深拷貝
淺拷貝:obj.clone
深拷貝:Marshal.load(Marshal.dump(obj)),Marshal.dump(obj)是把obj遞迴地寫入字串或檔案,Marshal.load產生一個與原對象狀態相同的對象。其實是曲線實現了深拷貝。
下面是手動進行的深拷貝。
# -*- encoding: utf-8 -*-#簡曆類class Resume attr_accessor :name, :sex, :age, :work_experience def initialize(name) @name = name @work_experience = WorkExperience.new end def set_personal_info(sex, age) @sex = sex @age = age end def set_work_experience(time_area, company) @work_experience.time_area = time_area @work_experience.company = company end def display puts "#{name} #{age} #{sex}" puts "工作經曆 #{@work_experience.time_area} #{@work_experience.company}" end def self_clone obj = self.clone obj.work_experience = self.work_experience.self_clone return obj endendclass WorkExperience attr_accessor :time_area, :company def self_clone self.clone endendresume1 = Resume.new(‘大鳥‘)resume1.set_personal_info(‘男‘, ‘29‘)resume1.set_work_experience(‘1998-2000‘, ‘xx公司‘)resume2 = resume1.self_cloneresume2.set_personal_info(‘男‘, ‘30‘)resume2.set_work_experience(‘2000-2002‘, ‘xx公司‘)resume3 =resume1.self_cloneresume3.set_personal_info(‘男‘, ‘31‘)resume3.set_work_experience(‘2002-2004‘, ‘xx公司‘)resume1.displayresume2.displayresume3.display#大鳥 29 男#工作經曆 1998-2000 xx公司#大鳥 30 男#工作經曆 2000-2002 xx公司#大鳥 31 男#工作經曆 2002-2004 xx公司
《大話設計模式》ruby版代碼:原型模式