"Big talk design mode" Ruby Code: Prototype mode

Source: Internet
Author: User

Demand:

To achieve a resume class, you must have a name, you can set the gender and age, you can set up work experience. Three resumes needed in the end

Initial implementation:

#-*-encoding:utf-8-*-#Resume ClassclassResume attr_accessor:name,: Sex,: Age,: Time_area,: CompanydefInitialize (name) @name=Name Enddefset_personal_info (sex, age) @sex=Sex @age=Age EnddefSet_work_experience (Time_area, company) @time_area=Time_area @company=Company EnddefDisplay puts"#{name} #{age} #{sex}"puts"work Experience #{time_area} #{company}"endendresume1= Resume.new ('Big Bird') Resume1.set_personal_info ('male',' in') Resume1.set_work_experience ('1998-2000','XX Company') resume2= Resume.new ('Big Bird') Resume2.set_personal_info ('male',' in') Resume2.set_work_experience ('2000-2002','XX Company') Resume3= Resume.new ('Big Bird') Resume3.set_personal_info ('male',' in') Resume3.set_work_experience ('2002-2004','XX Company') Resume1.displayresume2.displayresume3.display

The problem: If you want 20 copies, you need to instantiate 20 times. If you change an age, you have to change it 20 times, it is very troublesome to write.

Resume1 = Resume.new ('Big Bird') Resume1.set_personal_info ('male',' in') Resume1.set_work_experience ('1998-2000','XX Company') resume2=Resume1resume2.set_personal_info ('male',' in') Resume2.set_work_experience ('2000-2002','XX Company') Resume3=Resume1resume3.set_personal_info ('male',' in') Resume3.set_work_experience ('2002-2004','XX Company') Resume1.displayresume2.displayresume3.display

For the final part, it is not right to simply assign resume1 to resume2 and Resume3, because this is only a reference, not a value, resume1, Resume2, and Resume3 point to the same address.

Prototype mode: Create another customizable object from one object without needing to know the details of any creation.

#-*-encoding:utf-8-*-#Resume ClassclassResume attr_accessor:name,: Sex,: Age,: Time_area,: CompanydefInitialize (name) @name=Name Enddefset_personal_info (sex, age) @sex=Sex @age=Age EnddefSet_work_experience (Time_area, company) @time_area=Time_area @company=Company EnddefDisplay puts"#{name} #{age} #{sex}"puts"work Experience #{time_area} #{company}"Enddefself_clone self.clone endendresume1= Resume.new ('Big Bird') Resume1.set_personal_info ('male',' in') Resume1.set_work_experience ('1998-2000','XX Company') resume2=Resume1.self_cloneresume2.set_personal_info ('male',' -')Resume3=Resume1.self_cloneresume3.set_personal_info ('male',' in') Resume3.set_work_experience ('2002-2004','XX Company') Resume1.displayresume2.displayresume3.display
The Self_clone method is added in the Resume class , and Resume2 and Resume3 come from Resume1.self_clone, which only need to be instantiated once and the value of the modification is more convenient.
There is a problem, if the work experience is more complex, need a class to implement what will happen
#-*-encoding:utf-8-*-#Resume ClassclassResume attr_accessor:name,: Sex,: Age,: Work_experiencedefInitialize (name) @name=name @work_experience=Workexperience.new Enddefset_personal_info (sex, age) @sex=Sex @age=Age EnddefSet_work_experience (Time_area, Company) @work_experience. Time_area=Time_area @work_experience. Company=Company EnddefDisplay puts"#{name} #{age} #{sex}"puts"working experience #{@work_experience. Time_area} #{@work_experience. Company}"Enddefself_clone self.clone endendclassworkexperience Attr_accessor:time_area,: companyendresume1= Resume.new ('Big Bird') Resume1.set_personal_info ('male',' in') Resume1.set_work_experience ('1998-2000','XX Company') resume2=Resume1.self_cloneresume2.set_personal_info ('male',' -') Resume2.set_work_experience ('2000-2002','XX Company') Resume3=Resume1.self_cloneresume3.set_personal_info ('male',' to') Resume3.set_work_experience ('2002-2004','XX Company') Resume1.displayresume2.displayresume3.display#Big Bird 29 male#Work Experience 2002-2004 XX company#Big Bird 30 male#Work Experience 2002-2004 XX company#Big Bird 31 male#Work Experience 2002-2004 XX company

You can see that the work experience of three resumes is the same, because the object, clone is only the object reference, three resume uses the same work experience.

Shallow copy and deep copy

Shallow copy: Obj.clone

Deep copy: Marshal.load (marshal.dump (obj)), marshal.dump (obj) is a obj recursive write to a string or file, Marshal.load generates an object with the same state as the original object. In fact, the curve realizes a deep copy.

The following is a manual deep copy.

#-*-encoding:utf-8-*-#Resume ClassclassResume attr_accessor:name,: Sex,: Age,: Work_experiencedefInitialize (name) @name=name @work_experience=Workexperience.new Enddefset_personal_info (sex, age) @sex=Sex @age=Age EnddefSet_work_experience (Time_area, Company) @work_experience. Time_area=Time_area @work_experience. Company=Company EnddefDisplay puts"#{name} #{age} #{sex}"puts"working experience #{@work_experience. Time_area} #{@work_experience. Company}"EnddefSelf_clone obj=Self.clone obj.work_experience=Self.work_experience.self_clonereturnobj endendclassworkexperience Attr_accessor:time_area,: Companydefself_clone self.clone endendresume1= Resume.new ('Big Bird') Resume1.set_personal_info ('male',' in') Resume1.set_work_experience ('1998-2000','XX Company') resume2=Resume1.self_cloneresume2.set_personal_info ('male',' -') Resume2.set_work_experience ('2000-2002','XX Company') Resume3=Resume1.self_cloneresume3.set_personal_info ('male',' to') Resume3.set_work_experience ('2002-2004','XX Company') Resume1.displayresume2.displayresume3.display#Big Bird 29 male#Work Experience 1998-2000 XX company#Big Bird 30 male#Work Experience 2000-2002 XX company#Big Bird 31 male#Work Experience 2002-2004 XX company

"Big talk design mode" Ruby Code: Prototype mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.