Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Prototype prototype = new ConcretePrototype();
            //prototype.Attr = "Hello, Prototype Pattern";
            //Prototype copy = prototype.Clone();
            //Console.WriteLine(copy.Attr);

            //ConcretePrototypeA copy;
            //var prototype = new ConcretePrototypeA();
            //copy = (ConcretePrototypeA)prototype.Clone();
            //Console.WriteLine(prototype == copy);
            //Console.WriteLine(prototype.Member == copy.Member);

            ConcretePrototypeB copy;
            var prototype = new ConcretePrototypeB();

            prototype.Member = new MemberB("Test");
            copy             = (ConcretePrototypeB)prototype.Clone();
            Console.WriteLine(prototype == copy);
            Console.WriteLine(prototype.Member == copy.Member);
            Console.WriteLine(prototype.Member.Value == copy.Member.Value);


            Console.Read();
        }
Ejemplo n.º 2
0
        public override Prototype Clone()
        {
            ConcretePrototypeB clone     = null;
            FileStream         fs        = new FileStream("Temp.dat", FileMode.Create);
            BinaryFormatter    formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, this);  //序列化
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }

            FileStream      fs1        = new FileStream("Temp.dat", FileMode.Open);
            BinaryFormatter formatter1 = new BinaryFormatter();

            try
            {
                clone = (ConcretePrototypeB)formatter1.Deserialize(fs1);  //反序列化
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs1.Close();
            }
            return(clone);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //Prototype prototype = new ConcretePrototype();
            //prototype.Attr = "Hello, Prototype Pattern";
            //Prototype copy = prototype.Clone();
            //Console.WriteLine(copy.Attr);

            //ConcretePrototypeA copy;
            //var prototype = new ConcretePrototypeA();
            //copy = (ConcretePrototypeA)prototype.Clone();
            //Console.WriteLine(prototype == copy);
            //Console.WriteLine(prototype.Member == copy.Member);

            ConcretePrototypeB copy;
            var prototype = new ConcretePrototypeB();
            prototype.Member = new MemberB("Test");
            copy = (ConcretePrototypeB)prototype.Clone();
            Console.WriteLine(prototype == copy);
            Console.WriteLine(prototype.Member == copy.Member);
            Console.WriteLine(prototype.Member.Value == copy.Member.Value);

            Console.Read();
        }