CSharp: Prototype Pattern in donet core 3

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <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();
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <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;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <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;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// <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}";
       }
   }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    /// <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);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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);
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// <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}";
     }
 }

  

 

 

 

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//原型设计模式 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);

  

 

 

输出:

 

1
2
3
4
5
6
7
8
9
10
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

  

 

posted @   ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示