CSharp: Prototype Pattern in donet 6

 

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// <summary>
 /// 原型模式  Prototype Pattern
 /// </summary>
 public interface IColorPrototype
 {
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     IColorPrototype Clone();
 }
 
 
/// <summary>
 /// 原型模式  Prototype Pattern
 /// </summary>
 public class Color : IColorPrototype
 {
 
     /// <summary>
     ///
     /// </summary>
     /// <param name="red"></param>
     /// <param name="green"></param>
     /// <param name="blue"></param>
     public Color(int red, int green, int blue)
     {
         Red = red;
         Green = green;
         Blue = blue;
     }
     /// <summary>
     ///
     /// </summary>
     public int Red { get; set; }
     /// <summary>
     ///
     /// </summary>
     public int Green { get; set; }
     /// <summary>
     ///
     /// </summary>
     public int Blue { get; set; }
     /// <summary>
     ///
     /// </summary>
     /// <param name="red"></param>
     /// <param name="green"></param>
     /// <param name="blue"></param>
     public void Customize(int red, int green, int blue)
     {
         Red = red;
         Green = green;
         Blue = blue;
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public IColorPrototype Clone()
     {
         Console.WriteLine($"\n克隆的颜色 RGB: {Red},{Green},{Blue}");
         return (MemberwiseClone() as IColorPrototype)!;
     }
 }
 
 
 /// <summary>
 /// 原型模式  Prototype Pattern
 /// </summary>
 public class ColorRegistry
 {
 
 
     /// <summary>
     ///
     /// </summary>
     private readonly Dictionary<string, IColorPrototype> _colors = new();
     /// <summary>
     ///
     /// </summary>
     /// <param name="key"></param>
     /// <returns></returns>
     public IColorPrototype this[string key]
     {
         get => _colors[key];
         set => _colors[key] = value;
     }
     /// <summary>
     ///
     /// </summary>
     public void List()
     {
         Console.WriteLine("注册表中可用的颜色...");
 
         foreach (var color in _colors)
         {
             Console.WriteLine($"名称: {color.Key}");
         }
     }
 }
 
 /// <summary>
 ////原型模式  Prototype Pattern
 /// </summary>
 public static class ColorRegistryExecutor
 {
     public static void Execute()
     {
         ConsoleExtension.WriteSeparator("颜色注册示例 ");
 
         // Initialize registry with standard colors.
         var registry = new ColorRegistry
         {
             ["red"] = new Color(255, 0, 0),
             ["green"] = new Color(0, 255, 0),
             ["blue"] = new Color(0, 0, 255)
         };
         registry.List();
 
         // Clone red color and store it in the registry.
         var clonedColor = (registry["red"].Clone() as Color)!;
         clonedColor.Customize(clonedColor.Red, clonedColor.Green, 33);
         registry["custom"] = clonedColor;
         registry.List();
 
         // Clone custom color without storing it in the registry.
         _ = (registry["custom"].Clone() as Color)!;
         registry.List();
     }
 }

  

调用:

1
Geovin.Du.DuPrototype.ColorRegistry.ColorRegistryExecutor.Execute();

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
颜色注册示例
--------------------------------------------------
注册表中可用的颜色...
名称: red
名称: green
名称: blue
 
克隆的颜色 RGB: 255,0,0
注册表中可用的颜色...
名称: red
名称: green
名称: blue
名称: custom
 
克隆的颜色 RGB: 255,0,33
注册表中可用的颜色...
名称: red
名称: green
名称: blue
名称: custom

  

 

 

 

 

图片来源于联想集团2022联想创新科技大会

 

posted @   ®Geovin Du Dream Park™  阅读(19)  评论(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
点击右上角即可分享
微信分享提示