《大話設計模式》ruby版代碼:原型模式

來源:互聯網
上載者:User

標籤:

需求:

實現一個簡曆類,必須要有姓名,可以設定性別和年齡,可以設定工作經曆。最終需要三個簡曆

初步實現:

# -*- 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版代碼:原型模式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.