zoukankan      html  css  js  c++  java
  • 用序列化方法实现的Prototype的深拷贝

    下面是上次写的一个用序列化方法实现的Prototype的深拷贝代码,可供大家参考——注意其中Clone方法不是虚方法,写一次就可以满足所有子类的需要:

    using System;
    using System.Runtime;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;

    //抽象原型
    [Serializable]
    public abstract class Prototype
    {
            public abstract int I
            {
                    get;
                    set;
            }

            public abstract string S
            {
                    get;
                    set;
            }

            public abstract int[] A
            {
                    get;
                    set;
            }
            public abstract void print();
            public Prototype Clone()
            {

                    MemoryStream stream = new MemoryStream();

                    BinaryFormatter formatter = new BinaryFormatter();

                    formatter.Serialize(stream, this);

                    stream.Position = 0;

                    return (Prototype)formatter.Deserialize(stream);

            }
    }


    [Serializable]
    public class ConcretePrototype : Prototype
    {
            public int i;
            public string s;
            public int[] a;

            public override int I
            {
                    get { return i; }
                    set { this.i = value; }
            }

            public override string S
            {
                    get { return s; }
                    set { this.s = value; }
            }

            public override int[] A
            {
                    get { return a; }
                    set { this.a = value; }
            }

            public override void print()
            {
                    Console.Write("i={0}, s={1}, a={2}", i, s, a);
                    foreach (int item in a) { Console.Write(item+","); }
                    Console.WriteLine();

            }

    }


    public class GameSystem
    {
            public void Run(Prototype prototype)
            {
                    Prototype item1 = prototype.Clone();
                    item1.I = 100;
                    item1.S = "hello";
                    item1.A = new int[] { 1, 2, 3 };

                    Prototype item2 = prototype.Clone();

                    item2.I = 400;
                    item2.S = "world";
                    item2.A = new int[] { 100, 200, 300 };

                    item1.print();
                    item2.print();
                    Console.WriteLine(item1);
            }

    }

    class App
    {
            public static void Main()
            {
                    GameSystem gameSystem=new GameSystem();

                    gameSystem.Run(new ConcretePrototype());
                           
                   
            }
    }

  • 相关阅读:
    原创 记录一次线上Mysql慢查询问题排查过程
    原创 |我是如何解决POI解析Excel出现的OOM问题的?
    FastJson序列化时候出现了$ref?还不赶紧学习下
    fastjson自定义序列化竟然有这么多姿势?
    SpringBoot2.0整合WebSocket,实现后端数据实时推送!
    SpringMVC+Mybatis 如何配置多个数据源并切换?
    异常: java.security.InvalidKeyException: Illegal key size
    一分钟带你了解下MyBatis的动态SQL!
    一分钟带你了解下Spring Security!
    历时七天,史上最强MySQL优化总结,从此优化So Easy!
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/347122.html
Copyright © 2011-2022 走看看