Core Design Patterns(8) Prototype 原型模式
VS 2008
当我们需要实例化一个类的多个实例,并且实例化的代价比较昂贵的时候,可以使用原型模式。
1. 模式UML图

2. 应用

IPrototype.cs
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Linq;
using System.Linq;
 using System.Text;
using System.Text;

 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {

 public interface IPrototype<T> {
    public interface IPrototype<T> {

 T Clone();
        T Clone();
 T DeepCopy();
        T DeepCopy();
 }
    }
 }
}
 
UserInfo.cs
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Linq;
using System.Linq;
 using System.Text;
using System.Text;
 using System.IO;
using System.IO;
 using System.Xml.Serialization;
using System.Xml.Serialization;

 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {

 [Serializable]
    [Serializable]
 public class UserInfo : IPrototype<UserInfo> {
    public class UserInfo : IPrototype<UserInfo> {

 public int UserId {
        public int UserId {
 get;
            get;
 set;
            set;
 }
        }
 public string UserName {
        public string UserName {
 get;
            get;
 set;
            set;
 }
        }
 public PetInfo Pet {
        public PetInfo Pet {
 get;
            get;
 set;
            set;
 }
        }

 IPrototype
        IPrototype Members 
 }
    }
 }
}
 
PetInfo.cs
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Linq;
using System.Linq;
 using System.Text;
using System.Text;
 using System.Xml.Serialization;
using System.Xml.Serialization;

 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {

 [Serializable]
    [Serializable]
 public class PetInfo {
    public class PetInfo {

 public string PetName {
        public string PetName {
 get;
            get;
 set;
            set;
 }
        }
 }
    }
 }
}
 
Client
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Linq;
using System.Linq;
 using System.Text;
using System.Text;
 using DesignPattern.Prototype.BLL;
using DesignPattern.Prototype.BLL;

 namespace DesignPattern.Prototype {
namespace DesignPattern.Prototype {
 class Program {
    class Program {
 static void Main(string[] args) {
        static void Main(string[] args) {
 UserInfo u = new UserInfo() {
            UserInfo u = new UserInfo() {
 UserId = 1,
                UserId = 1,
 UserName = "guozhijian",
                UserName = "guozhijian",
 Pet = new PetInfo() {
                Pet = new PetInfo() {
 PetName = "nana"
                    PetName = "nana"
 }
                }
 };
            };
 UserInfo uCopy = u.Clone();
            UserInfo uCopy = u.Clone();

 Console.WriteLine("{0},{1},{2}", uCopy.UserId.ToString(), uCopy.UserName, uCopy.Pet.PetName);
            Console.WriteLine("{0},{1},{2}", uCopy.UserId.ToString(), uCopy.UserName, uCopy.Pet.PetName);

 UserInfo uDeepCopy = u.DeepCopy();
            UserInfo uDeepCopy = u.DeepCopy();
 uDeepCopy.Pet.PetName = "sasa";
            uDeepCopy.Pet.PetName = "sasa";
 Console.WriteLine("{0},{1},{2}", uDeepCopy.UserId.ToString(), uDeepCopy.UserName, uDeepCopy.Pet.PetName);
            Console.WriteLine("{0},{1},{2}", uDeepCopy.UserId.ToString(), uDeepCopy.UserName, uDeepCopy.Pet.PetName);

 Console.WriteLine(u.Pet.PetName);
            Console.WriteLine(u.Pet.PetName);


 }
        }
 }
    }
 }
}
 
Output
 
 
当我们需要实例化一个类的多个实例,并且实例化的代价比较昂贵的时候,可以使用原型模式。
1. 模式UML图

2. 应用

IPrototype.cs
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq; using System.Text;
using System.Text;
 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {
 public interface IPrototype<T> {
    public interface IPrototype<T> {
 T Clone();
        T Clone(); T DeepCopy();
        T DeepCopy(); }
    } }
}
UserInfo.cs
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq; using System.Text;
using System.Text; using System.IO;
using System.IO; using System.Xml.Serialization;
using System.Xml.Serialization;
 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {
 [Serializable]
    [Serializable] public class UserInfo : IPrototype<UserInfo> {
    public class UserInfo : IPrototype<UserInfo> {
 public int UserId {
        public int UserId { get;
            get; set;
            set; }
        } public string UserName {
        public string UserName { get;
            get; set;
            set; }
        } public PetInfo Pet {
        public PetInfo Pet { get;
            get; set;
            set; }
        }
 IPrototype
        IPrototype }
    } }
}
PetInfo.cs
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq; using System.Text;
using System.Text; using System.Xml.Serialization;
using System.Xml.Serialization;
 namespace DesignPattern.Prototype.BLL {
namespace DesignPattern.Prototype.BLL {
 [Serializable]
    [Serializable] public class PetInfo {
    public class PetInfo {
 public string PetName {
        public string PetName { get;
            get; set;
            set; }
        } }
    } }
}
Client
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq; using System.Text;
using System.Text; using DesignPattern.Prototype.BLL;
using DesignPattern.Prototype.BLL;
 namespace DesignPattern.Prototype {
namespace DesignPattern.Prototype { class Program {
    class Program { static void Main(string[] args) {
        static void Main(string[] args) { UserInfo u = new UserInfo() {
            UserInfo u = new UserInfo() { UserId = 1,
                UserId = 1, UserName = "guozhijian",
                UserName = "guozhijian", Pet = new PetInfo() {
                Pet = new PetInfo() { PetName = "nana"
                    PetName = "nana" }
                } };
            }; UserInfo uCopy = u.Clone();
            UserInfo uCopy = u.Clone();
 Console.WriteLine("{0},{1},{2}", uCopy.UserId.ToString(), uCopy.UserName, uCopy.Pet.PetName);
            Console.WriteLine("{0},{1},{2}", uCopy.UserId.ToString(), uCopy.UserName, uCopy.Pet.PetName);
 UserInfo uDeepCopy = u.DeepCopy();
            UserInfo uDeepCopy = u.DeepCopy(); uDeepCopy.Pet.PetName = "sasa";
            uDeepCopy.Pet.PetName = "sasa"; Console.WriteLine("{0},{1},{2}", uDeepCopy.UserId.ToString(), uDeepCopy.UserName, uDeepCopy.Pet.PetName);
            Console.WriteLine("{0},{1},{2}", uDeepCopy.UserId.ToString(), uDeepCopy.UserName, uDeepCopy.Pet.PetName);
 Console.WriteLine(u.Pet.PetName);
            Console.WriteLine(u.Pet.PetName);

 }
        } }
    } }
}
Output
 
 
 
                    
                     
                    
                 
                    
                 


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号