Prototype mode-copy your resume to find a job

  Prototype definition : Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.
  Example background: To find a job, you need to prepare a lot of resumes, but I don't want to write codewords one by one, so I think of the copy and paste function. A friend also asked me to help him make a resume. The format can be copied and pasted. If you want to make the resume content different, you need to use the deep copy function. Here we show examples of different work experiences.
  The copy function also has the difference between shallow copy and deep copy .


Insert picture description here

Prototype class: .net provides the ICloneable interface, in which there is a method Clone(), so you don't actually need to write a prototype class to implement the interface.

abstract class Prototype
{
    
    
	private string id;		
	public Prototype(string id)		//构造函数重载
	{
    
    
		this.id=id;
	}
	public string Id		//Id属性只读
	{
    
    
		get {
    
    return id;}
	}
	public abstract Prototype Clone(); //抽象类的Clone方法
}

Specific prototype class:

//具体原型类实现复制
class ConcretePrototype1:Prototype
{
    
    
	public ConcretePrototype1(string id):base(id)
	{
    
    }
	public override Prototype Clone()	//重写Clone方法
	{
    
    
		return (Prototype)this.MemberwiseClone();	//MemberwiseClone()就是浅复制
	}
}

Client code:

static void Main (string[] args)
{
    
    
	ConcretePrototype1 p1 = new ConcretePrototype1("I");
	ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();	//把Clone后的值强转成ConcretePrototype1类型
	Console.WriteLine("Clone:{0}",c1.Id);
	Console.Read();
}


  Shallow copy: In the MemberwiseClone() method, when the field is a value type, it is copied bit by bit; when the field is a reference type, the reference is copied, and the referenced object is not copied.

Work experience category:

    class WorkExperience
    {
    
    
        private string workDate;     //工作时间
        public string WorkDate
        {
    
    
            get {
    
     return workDate; }
            set {
    
     workDate = value; }
        }
        private string company;		//所在公司
        public string Company
        {
    
    
            get {
    
     return company; }
            set {
    
     company = value; }
        }

Resume category:

class Resume:ICloneable		//实现接口
    {
    
    
        private string name, sex, age;

        private WorkExperience work; //引用工作经历对象

        public Resume(string name)
        {
    
    
            this.name = name;
            work = new WorkExperience(); //在简历类实例化同时实例化工作经历
        }
        //设置个人信息
        public void SetPersonalInfo(string sex,string age)
        {
    
    
            this.sex = sex;
            this.age = age;
        } 
        //设置工作经历
        public void SetWorkExperience(string workDate, string company)
        {
    
    
            work.WorkDate = workDate;
            work.Company = company;
        }
        //显示
        public void Display()
        {
    
    
            Console.WriteLine("{0} {1} {2}",name,sex,age);
            //这里工作经历使用的引用,对于引用类型,浅复制只复制引用,不复制引用的对象
            Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
        }
        public Object Clone()
        {
    
    
            return (Object)this.MemberwiseClone();		//这里用到浅复制方法了
        }

Client:

		static void Main(string[] args)
        {
    
    
            Resume a = new Resume("飞机");
            a.SetPersonalInfo("男","29");
            a.SetWorkExperience("2020年11月29日","九天星月公司");

            Resume b = (Resume)a.Clone();	//a的值浅复制给了b
            b.SetWorkExperience("2020年11月29日","米西我的超人公司");	 //引用被覆盖

            Resume c = (Resume)a.Clone();	//a的值浅复制给了c
            c.SetPersonalInfo("男","24");	//重新复制
            c.SetWorkExperience("2020年11月30日","瓜里瓜气公司");	//引用被覆盖
            a.Display();
            b.Display();
            c.Display();

            Console.Read();
        }

Display: the displayed work experience is the last value

飞机 男 29
工作经历:20201130日 瓜里瓜气公司
飞机 男 29
工作经历:20201130日 瓜里瓜气公司
飞机 男 24
工作经历:20201130日 瓜里瓜气公司


  Deep copy: Point the variable of the referenced object to the copied new object instead of the original referenced object.


Work experience category:

class WorkExperience:ICloneable //实现了接口
{
    
    
	省略……
}
public Object Clone()	//实现克隆方法
{
    
    
	return (object)this.MemberwiseClone();	//依然是MemberwiseClone(),但简历类里传值方式会有改变
}

Resume category:

class Resume:ICloneable		//实现接口
    {
    
    
        private string name, sex, age;

        private WorkExperience work; //引用工作经历对象

        public Resume(string name)
        {
    
    
            this.name = name;
            work = new WorkExperience(); //在简历类实例化同时实例化工作经历
        }
        private Resume(WorkExperience work)//构造函数,参数是工作经历类型
        {
    
    
            this.work = (WorkExperience)work.Clone(); //这里的Clone()方法是工作经历类里的方法,浅复制了引用
        }
        //设置个人信息
        public void SetPersonalInfo(string sex,string age)
        {
    
    
            不变,省略……
        } 
        //设置工作经历
        public void SetWorkExperience(string workDate, string company)
        {
    
    
			不变,省略……
        }
        //显示
        public void Display()
        {
    
    
            不变,省略……
        }
        public Object Clone()
        {
    
    
        	//使用构造方法,让工作经历浅复制完成,赋值给新对象,而不是原来被引用的对象
            Resume obj = new Resume(this.work); 
            obj.name = this.name;
            obj.sex = this.name;
            obj.age = this.age;
            return obj;
        }

The client code remains unchanged.
Display: The three work experiences are different this time.

飞机 男 29
工作经历:20201129日 九天星月公司
飞机 大鸟 29
工作经历:20201129日 米西我的超人公司
飞机 男 24
工作经历:20201130日 瓜里瓜气公司

Guess you like

Origin blog.csdn.net/CharmaineXia/article/details/110662622