CSharp: Prototype Pattern in donet core 3
/// <summary> /// Prototype Pattern原型设计模式 /// </summary> public abstract class BasicCar { /// <summary> /// /// </summary> public int basePrice = 0, onRoadPrice = 0; /// <summary> /// 车型 /// </summary> public string ModelName { get; set; } /* We'll add this price before the final calculation of onRoadPrice. */ /// <summary> /// 随机数的价格 /// </summary> /// <returns></returns> public static int SetAdditionalPrice() { Random random = new Random(); int additionalPrice = random.Next(200000, 500000); return additionalPrice; } /// <summary> /// /// </summary> /// <returns></returns> public abstract BasicCar Clone(); } }
/// <summary> /// 塔塔Nano /// Prototype Pattern原型设计模式 /// </summary> public class Nano : BasicCar { /// <summary> /// /// </summary> /// <param name="m">车型</param> public Nano(string m) { ModelName = m; // Setting a base price for Nano. basePrice = 100000; } /// <summary> /// /// </summary> /// <returns></returns> public override BasicCar Clone() { // Creating a shallow copy and returning it. return this.MemberwiseClone() as Nano; } }
/// <summary> /// 福特 /// Prototype Pattern原型设计模式 /// </summary> public class Ford : BasicCar { /// <summary> /// /// </summary> /// <param name="m"></param> public Ford(string m) { ModelName = m; // Setting a basic price for Ford. basePrice = 500000; } /// <summary> /// /// </summary> /// <returns></returns> public override BasicCar Clone() { // Creating a shallow copy and returning it. return this.MemberwiseClone() as Ford; } }
/// <summary> /// Prototype Pattern原型设计模式 /// </summary> public class Address { /// <summary> /// /// </summary> public string StreetAddress, City, Country; /// <summary> /// /// </summary> /// <param name="streetAddress"></param> /// <param name="city"></param> /// <param name="country"></param> public Address(string streetAddress, string city, string country) { StreetAddress = streetAddress; City = city; Country = country; } /// <summary> /// /// </summary> /// <param name="other"></param> public Address(Address other) { StreetAddress = other.StreetAddress; City = other.City; Country = other.Country; } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $"{nameof(StreetAddress)}: {StreetAddress}, {nameof(City)}: {City}, {nameof(Country)}: {Country}"; } } /// <summary> /// /// </summary> public class Person { /// <summary> /// /// </summary> public string Name; /// <summary> /// /// </summary> public Address Address; /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="address"></param> public Person(string name, Address address) { Name = name; Address = address; } /// <summary> /// /// </summary> /// <param name="other"></param> public Person(Person other) { Name = other.Name; Address = new Address(other.Address); } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $"{nameof(Name)}: {Name}, {nameof(Address)}: {Address}"; } }
/// <summary> /// Prototype Pattern原型设计模式 /// </summary> public class Address { public string StreetAddress, City; public int Suite; /// <summary> /// /// </summary> /// <param name="streetAddress"></param> /// <param name="city"></param> /// <param name="suite"></param> public Address(string streetAddress, string city, int suite) { StreetAddress = streetAddress; City = city; Suite = suite; } public Address(Address other) { StreetAddress = other.StreetAddress; City = other.City; Suite = other.Suite; } public override string ToString() { return $"{nameof(StreetAddress)}: {StreetAddress}, {nameof(City)}: {City}, {nameof(Suite)}: {Suite}"; } } /// <summary> /// Prototype Pattern原型设计模式 /// </summary> public partial class Employee { public string Name; public Address Address; public Employee(string name, Address address) { Name = name ?? throw new ArgumentNullException(paramName: nameof(name)); Address = address ?? throw new ArgumentNullException(paramName: nameof(address)); } public Employee(Employee other) { Name = other.Name; Address = new Address(other.Address); } public override string ToString() { return $"{nameof(Name)}: {Name}, {nameof(Address)}: {Address.ToString()}"; } //partial class EmployeeFactory {} } /// <summary> /// Prototype Pattern原型设计模式 /// </summary> public class EmployeeFactory { private static Employee main = new Employee(null, new Address("123 East Dr", "London", 0)); private static Employee aux = new Employee(null, new Address("188 Geovin Du Dr", "JiangXi", 0)); /// <summary> /// /// </summary> /// <param name="proto"></param> /// <param name="name"></param> /// <param name="suite"></param> /// <returns></returns> private static Employee NewEmployee(Employee proto, string name, int suite) { var copy = proto.DeepCopy(); copy.Name = name; copy.Address.Suite = suite; return copy; } public static Employee NewMainOfficeEmployee(string name, int suite) => NewEmployee(main, name, suite); public static Employee NewAuxOfficeEmployee(string name, int suite) => NewEmployee(aux, name, suite); } }
// ICloneable is ill-specified /// <summary> /// Prototype Pattern原型设计模式 /// </summary> /// <typeparam name="T"></typeparam> interface IDeepCopyable<T> { T DeepCopy(); } /// <summary> /// /// </summary> public class Address : ICloneable { public readonly string StreetName; public int HouseNumber; public Address(string streetName, int houseNumber) { StreetName = streetName; HouseNumber = houseNumber; } public override string ToString() { return $"{nameof(StreetName)}: {StreetName}, {nameof(HouseNumber)}: {HouseNumber}"; } public object Clone() { return new Address(StreetName, HouseNumber); } } /// <summary> /// /// </summary> public class Person : ICloneable { /// <summary> /// /// </summary> public readonly string[] Names; /// <summary> /// /// </summary> public readonly Address Address; /// <summary> /// /// </summary> /// <param name="names"></param> /// <param name="address"></param> public Person(string[] names, Address address) { Names = names; Address = address; } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $"{nameof(Names)}: {string.Join(",", Names)}, {nameof(Address)}: {Address}"; } /// <summary> /// /// </summary> /// <returns></returns> public object Clone() { return MemberwiseClone(); //return new Person(Names, Address); } }
/// <summary> /// Prototype Pattern原型设计模式 /// </summary> public static class ExtensionMethods { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self"></param> /// <returns></returns> public static T DeepCopy<T>(this T self) { using (var stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, self); stream.Seek(0, SeekOrigin.Begin); object copy = formatter.Deserialize(stream); return (T)copy; } } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="self"></param> /// <returns></returns> public static T DeepCopyXml<T>(this T self) { using (var ms = new MemoryStream()) { XmlSerializer s = new XmlSerializer(typeof(T)); s.Serialize(ms, self); ms.Position = 0; return (T)s.Deserialize(ms); } } } //[Serializable] // this is, unfortunately, required /// <summary> /// /// </summary> public class Foo { /// <summary> /// /// </summary> public uint Stuff; /// <summary> /// /// </summary> public string Whatever; /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $"{nameof(Stuff)}: {Stuff}, {nameof(Whatever)}: {Whatever}"; } }
调用:
//原型设计模式 geovindu Console.WriteLine("***Prototype Pattern Demo***\n"); //Base or Original Copy BasicCar nano = new Nano("Green Nano"); BasicCar ford = new Ford("Ford Yellow"); //Console.WriteLine("Before clone, base prices:"); //Console.WriteLine($"Car is: {nano.ModelName}, and it's base price is Rs. {nano.basePrice}"); //Console.WriteLine($"Car is: {ford.ModelName}, and it's base price is Rs. {ford.basePrice}"); BasicCar basicCar; // Nano basicCar = nano.Clone(); // Working on cloned copy basicCar.onRoadPrice = basicCar.basePrice + BasicCar.SetAdditionalPrice(); Console.WriteLine($"Car is: {basicCar.ModelName}, and it's price is Rs. {basicCar.onRoadPrice} basic price:{basicCar.basePrice}"); // Ford basicCar = ford.Clone(); // Working on cloned copy basicCar.onRoadPrice = basicCar.basePrice + BasicCar.SetAdditionalPrice(); Console.WriteLine($"Car is: {basicCar.ModelName}, and it's price is Rs. {basicCar.onRoadPrice} basic price:{basicCar.basePrice}"); ///Console.ReadLine(); var geovindu= new Person("Geovin Du ",new PrototypePattern.Du.Address("1088 ShenNan Road", "Shenzhen", "China")); var jane = new Person(geovindu); jane.Name = "涂聚文"; Console.WriteLine(" " + geovindu.Name + " " + geovindu.Address.StreetAddress + " " + geovindu.Address.City + " " + geovindu.Address.Country); // Console.WriteLine( geovindu.ToString()); Console.WriteLine(" " + jane.Name+" "+jane.Address.StreetAddress + " " + geovindu.Address.City + " " + jane.Address.Country); Console.WriteLine(jane.ToString()); var geovindu1 = new Employee("geovindu", new PrototypePattern.Address("布心路", "深圳市",1288)); Console.WriteLine(geovindu1.ToString()); var jane1 = new Employee(geovindu1); jane1.Name ="涂年生"; Console.WriteLine(jane1.ToString()); Foo foo = new Foo { Stuff = 42, Whatever = "abc" }; Foo foo2 = foo.DeepCopyXml(); foo2.Whatever = "xyz"; Console.WriteLine(foo); Console.WriteLine(foo2);
输出:
Car is: Green Nano, and it's price is Rs. 594932 basic price:100000 Car is: Ford Yellow, and it's price is Rs. 926319 basic price:500000 Geovin Du 1088 ShenNan Road Shenzhen China Name: Geovin Du , Address: StreetAddress: 1088 ShenNan Road, City: Shenzhen, Country: China 涂聚文 1088 ShenNan Road Shenzhen China Name: 涂聚文, Address: StreetAddress: 1088 ShenNan Road, City: Shenzhen, Country: China Name: geovindu, Address: StreetAddress: 布心路, City: 深圳市, Suite: 1288 Name: 涂年生, Address: StreetAddress: 布心路, City: 深圳市, Suite: 1288 Stuff: 42, Whatever: abc Stuff: 42, Whatever: xyz
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)