登录  /  注册

.NET原型模式讲解

高洛峰
发布: 2016-12-10 09:20:29
原创
969人浏览过

原型模式的定义:

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

原型模式结构图:

20161031140826605.jpg

创建型模式中一个比较特殊的模式-原型模式,有个最大的特点是克隆一个现有的对象,这个克隆的结果有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
}
登录后复制

20161031140826605.jpg

可以发现:在我们修改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();
}
登录后复制

   

程序的运行结果如下:

20161031140826605.jpg

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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号