当前位置: 移动技术网 > IT编程>开发语言>.net > .NET原型模式讲解

.NET原型模式讲解

2017年12月12日  | 移动技术网IT编程  | 我要评论

暗夜的吸血情人,燕子鱼,醋泡花生的做法

原型模式的定义:

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

原型模式结构图:

创建型模式中一个比较特殊的模式-原型模式,有个最大的特点是克隆一个现有的对象,这个克隆的结果有2种,一种是浅度复制,另一种是深度复制。

创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象而不需要提供专门的new()操作就可以快速完成对象的创建,这无疑是一种非常有效的方式,快速的创建一个新的对象。

1.原型模式:浅度复制

定义一个接口, 用来表述所有的颜色对象接口

  /// <summary>
  /// 颜色接口
  /// </summary>
  public interface icolor
  {
    icolor clone();
    int red { get; set; }
    int green { get; set; }
    int blue { get; set; }
  }

给出红色的具体实现代码:

  public class redcolor:icolor
  {
    public int red { get; set; }
    public int green { get; set; }
    public int blue { get; set; }

    public icolor clone()
    {
      return (icolor)this.memberwiseclone(); 
    } 
  }

具体的测试代码如下:

    static void main(string[] args)
    {
      icolor color = new redcolor();
      color.red = 255;
      console.writeline("color -red " + color.red); //225
      icolor color1 = color.clone();
      color1.red = 224;
      console.writeline("color1-red " + color1.red);//224
      console.writeline("color -red " + color.red); //225
    }

可以发现:在我们修改color1对象的red属性值,没有对color的属性参生影响,即对象副本的修改不会影响对象本身的状态。

2.原型模式:深度复制

深复制考虑的情况相对来说就会比较复杂,因为有可能对象是之间有继承关系或者引用关系的时候,可能我们深复制的时候就需要注意.一般来说深复制一方面可以采用种简单的深复制对象的时候的方案,还可以通过序列化的形式来进行对象的复制。下面通过序列化的形式来实现原型模式:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication4
{
  /// <summary>
  /// 颜色接口
  /// </summary>
  public interface icolor
  {
    icolordemo clone();

    int red { get; set; }
    int green { get; set; }
    int blue { get; set; }
    factroy f{get;set;}
  }

  /// <summary>
  /// 生产颜色的工厂信息
  /// </summary>
  [serializable]
  public class factroy
  {
    public string name { get; set; }
  }
}

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication4
{
  /// <summary>
  /// 颜色
  /// </summary>
  [serializable]
  public class redcolor:icolor
  {
    public int red { get; set; }
    public int green { get; set; }
    public int blue { get; set; }
    public factroy f { get; set; }

    public icolor clone()
    {
      serializablehelper s = new serializablehelper();
      string target = s.serializable(this);
      return s.derializable<icolor>(target); 
    } 
  }
}

序列化帮助类:

/// <summary>
  /// 序列化和反序列化辅助类
  /// </summary>
  public class serializablehelper
  {
    public string serializable(object target)
    {
      using (memorystream stream = new memorystream())
      {
        new binaryformatter().serialize(stream, target);

        return convert.tobase64string(stream.toarray());
      }
    }

    public object derializable(string target)
    {
      byte[] targetarray = convert.frombase64string(target);

      using (memorystream stream = new memorystream(targetarray))
      {
        return new binaryformatter().deserialize(stream);
      }
    }

    public t derializable<t>(string target)
    {
      return (t)derializable(target);
    }
  }

测试:

  static void main(string[] args)
  {
    icolor color = new redcolor();
    color.red = 255;
    color.f = new factroy() { name="湖北工厂" };
    console.writeline("color - factroy:" + color.f.name); //湖北工厂

    icolor color1 = color.clone();
    color1.red = 234;
    color1.f.name = "北京工厂";
    console.writeline("color1- factroy:" + color1.f.name); //北京工厂
    console.writeline("color - factroy:" + color.f.name); //湖北工厂
    console.read();
  }

程序的运行结果如下:


结论:通过序列化和反序列化形成新的对象。其实只要是项目中要使用原型模式进行对象复制的情况下,都可以通过序列化的形式来进行深复制。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网