程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 設計模式之原型模式,設計模式原型

設計模式之原型模式,設計模式原型

編輯:關於.NET

設計模式之原型模式,設計模式原型


1、類圖

實例類圖

2、創建項目

……………………………………

3、 新建周報類WeeklyLog:充當原型角色,Clone()方法為克隆方法,用於實現原型對象的克隆,Attachmentch充當成員類。

Attachmentch代碼如下:

using System;

namespace PrototypeSample

{

    [Serializable]

    class Attachment

    {

        private string name;

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

 

        public void Download()

        {

            Console.WriteLine("下載附件,文件名為{0}。",name);

        }

    }

}

 

4、WeeklyLog類代碼如下:

using System;

using System.IO;

using System.Collections;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization;

namespace PrototypeSample

{

    [Serializable]

    class WeeklyLog

    {

        private Attachment attachment;

        private string name;

        private string date;     

        private string content;

 

        public Attachment Attachment

        {

            get { return attachment; }

            set { attachment = value; }

        }

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        public string Date

        {

            get { return date; }

            set { date = value; }

        }

        public string Content

        {

            get { return content; }

            set { content = value; }

        }

        /*

        //使用MemberwiseClone()方法實現淺克隆

        public WeeklyLog Clone()

        {

            return this.MemberwiseClone() as WeeklyLog;

        }

         */

        //使用序列化方式實現深克隆

        public WeeklyLog Clone()

        {

            WeeklyLog clone = null;

            //序列化

            FileStream fs = new FileStream("Temp.dat", FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();

            try

            {

                //對當前對象執行序列化操作到Temp.dat

                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 = (WeeklyLog)formatter1.Deserialize(fs1);

            }

            catch (SerializationException e)

            {

                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);

                throw;

            }

            finally

            {

                fs.Close();

            }

            return clone;

        }

    }

}

5、 客戶端測試類Program:

using System;

namespace PrototypeSample

{

    class Program

    {

        static void Main(string[] args)

        {

            /*

            ConcretePrototypeB prototype, copy;

            prototype = new ConcretePrototypeB();

            //prototype.Attr = "Sunny";

            copy = (ConcretePrototypeB)prototype.Clone();

            //copy.Attr = "Tom";

            Console.WriteLine(prototype == copy);

            //Console.WriteLine(prototype.GetType() == copy.GetType());

            Console.WriteLine(prototype.Member == copy.Member);

            Console.Read();

            */

 

            WeeklyLog log_previous, log_new;

            log_previous = new WeeklyLog();

            Attachment attachment = new Attachment();

            log_previous.Attachment = attachment;

            log_new = log_previous.Clone();

            Console.WriteLine("周報是否相同?{0}",(log_previous == log_new)?"是":"否");

            Console.WriteLine("附件是否相同?{0}",(log_previous.Attachment == log_new.Attachment)?"是":"否");

           Console.Read();

        }

    }

}

6、 編譯及運行程序,輸出如下結果:

 

7、 深克隆解決方案:

1) 將周報類WeeklyLog和附件類Attachment標記為可序列化(Serializable)

[Serializable]

class WeeklyLog

{

    private Attachment attachment;

……

}

[Serializable]

class Attachment

{

……

}

//使用序列化方式實現深克隆

public WeeklyLog Clone()

{

    WeeklyLog 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 = (WeeklyLog)formatter1.Deserialize(fs1);  //反序列化

    }

    catch (SerializationException e)

    {

        Console.WriteLine("Failed to deserialize. Reason: " + e.Message);

        throw;

    }

    finally

    {

        fs1.Close();

    }

        return clone;

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved