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联想创新科技大会
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
desgin patterns
, 设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!