zoukankan      html  css  js  c++  java
  • 设计模式笔记7:原型模式

    1.原型模式用处:

    ·用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

     ps:当我们需要创建大量相同对象的时候,就可以用原型模式大批量复制对象。和现实生活中

    的复印机相似,通过一个原型(模板)批量复制相同的对象;

    2.深复制与潜复制的区别:

           复制需要用到MemberwiseClone()方法;

    1,浅复制:如果字段是值类型的,则对该字段进行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象;因此原始对象和其副本 引用同一对象;

    2,深复制;
        把引用对象的变量指向复制过来的新对象,而不是原有的被引用的对象;

    ps:同是复制对象,深复制在复制引用对象时会为引用生成新的一个对象副本不影响原对象。而浅复制直接复制引用;

    3.代码

     
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 原型模式
     8 {
     9     class Person : ICloneable
    10     {
    11         public Person(string name, int age, Car car)
    12         {
    13             this.Name = name;
    14             this.Age = age;
    15             this.MyCar = car;
    16             
    17         }
    18         private Person(Car car)
    19         {
    20             // 核心:对于引用对象调用其Clone方法创建一个新的副本。如果被复制对象内部含有引用成员也适用这个规则。
    21             this.MyCar = (Car)car.Clone();
    22         }
    23         public int Age { get; set; }
    24         public string Name { get; set; }
    25 
    26         public Car MyCar { get; set; }
    27         public object Clone()
    28         {
    29             // 对引用类型进行深层复制
    30             Person p = new Person(MyCar);
    31             p.Name = Name;
    32             p.Age = Age;
    33             return p;
    34         }
    35 
    36         public void SayHi()
    37         {
    38             Console.WriteLine("我是{0},我今年{1}岁,我的车是{2}", this.Name, this.Age, this.MyCar.Name);
    39         }
    40     }
    41 
    42     class Car:ICloneable
    43     {
    44         public string Name { get; set; }
    45 
    46         public object Clone()
    47         {
    48             return this.MemberwiseClone();
    49         }
    50     }
    51 }
    Person
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 原型模式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             // 浅复制  值类型不相互影响,引用类型成员都指向同一地址。
    14             // 深复制  引用和值类型都重新创建副本。
    15             Car c = new Car() { Name = "奇瑞" };
    16             Person p1 = new Person("张三", 18, c);
    17             Person p2 = (Person)p1.Clone();
    18             p2.Name = "李四";
    19             p2.MyCar.Name = "一汽";
    20             Person p3 = (Person)p1.Clone();
    21             p3.Name = "王五";
    22             p3.MyCar.Name = "广汽";
    23             p1.SayHi();
    24             p2.SayHi();
    25             p3.SayHi();
    26         }
    27     }
    28 }
    Program

    深复制核心:对于引用对象调用其Clone方法创建一个新的副本。如果被复制对象内部含有引用成员也适用这个规则。 
  • 相关阅读:
    07word转换pdf
    高德纳《计算机程序设计艺术》(The Art of Computer Programming)的作者
    DbVisualizer 8 解决中文乱码问题 (20120310 20:23)
    IT has never been easier or harder
    el表达式和s:property的区别
    df 和 du 命令详解
    dbvisualizer 8.0 破解(free>personal)
    ping 中的TTL查看操作系统
    netstat 监控TCP/IP网络
    ls l 命令
  • 原文地址:https://www.cnblogs.com/simple-blog/p/4143870.html
Copyright © 2011-2022 走看看