zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--原型模式

     1 using System;
     2 
     3 namespace Prototype
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/24 19:46:36 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// WorkExperience说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class WorkExperience:ICloneable
    12     {
    13         private string workData;
    14 
    15         public string WorkData
    16         {
    17             get { return workData; }
    18             set { workData = value; }
    19         }
    20 
    21         private string company;
    22 
    23         public string Company
    24         {
    25             get { return company; }
    26             set { company = value; }
    27         }
    28 
    29         public object Clone()
    30         {
    31             return (object)this.MemberwiseClone();
    32         }
    33     }
    34 }
    View Code
     1 using System;
     2 
     3 namespace Prototype
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/24 19:36:45 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Resume说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Resume:ICloneable
    12     {
    13         private string name;
    14         private string sex;
    15         private string age;
    16 
    17         private WorkExperience workExperience;
    18 
    19         public Resume(string name)
    20         {
    21             this.name = name;
    22             workExperience = new WorkExperience();
    23         }
    24 
    25         private Resume(WorkExperience work)
    26         {
    27             this.workExperience = (WorkExperience)work.Clone();
    28         }
    29 
    30         public void SetPersonalInfo(string sex,string age)
    31         {
    32             this.sex = sex;
    33             this.age = age;
    34         }
    35 
    36         public void SetWorkExperience(string timeArea,string company)
    37         {
    38             workExperience.WorkData = timeArea;
    39             workExperience.Company = company;
    40         }
    41 
    42         public void Display()
    43         {
    44             Console.WriteLine("{0}{1}{2}",name,sex,age);
    45             Console.WriteLine("工作经历:{0}{1}", workExperience.WorkData, workExperience.Company);
    46         }
    47 
    48         public object Clone()
    49         {
    50             Resume obj = new Resume(this.workExperience);
    51             obj.name = this.name;
    52             obj.sex = this.sex;
    53             obj.age = this.age;
    54 
    55             return obj;
    56         }
    57     }
    58 }
    View Code
     1 using System;
     2 namespace Prototype
     3 {
     4     class Program
     5     {
     6         static void Main(string[] args)
     7         {
     8             Resume a = new Resume("大鸟");
     9             a.SetPersonalInfo("","29");
    10             a.SetWorkExperience("1998-2000","xx公司");
    11 
    12             Resume b = (Resume)a.Clone();
    13             b.SetWorkExperience("1998-2006","YY企业");
    14 
    15             Resume c = (Resume)a.Clone();
    16             c.SetPersonalInfo("","24");
    17             c.SetWorkExperience("1998-2003","ZZ企业");
    18 
    19             a.Display();
    20             b.Display();
    21             c.Display();
    22         }
    23     }
    24 }
    View Code
  • 相关阅读:
    2018全球最强物联网公司榜单揭晓
    物联网
    kalman滤波
    自动驾驶
    CAN总线基础
    Linux系统下x86和ARM的区别有哪些?
    算法课笔记系列(七)—— 平摊分析Amortized Analysis
    深入理解Linux内存分配
    linux内核--自旋锁的理解
    DMA(直接存储器存取)
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5524652.html
Copyright © 2011-2022 走看看