CSharp: Flyweight 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 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | /// <summary> /// The 'Flyweight' interface ///享元模式 Flyweight Pattern /// geovindu,Geovin Du edit /// 车辆 /// </summary> interface IVehicle { /* * Client will supply the color. * It is extrinsic state. */ /// <summary> /// /// </summary> /// <param name="color">输入颜色</param> void AboutMe( string color); } /// <summary> /// A 'ConcreteFlyweight' class called Car /// 汽车 /// </summary> class Car : IVehicle { /* * It is intrinsic state and * it is independent of flyweight context. * this can be shared.So, our factory method will supply * this value inside the flyweight object. */ /// <summary> /// 描述 /// </summary> private string description; /* * Flyweight factory will supply this * inside the flyweight object. */ /// <summary> /// /// </summary> /// <param name="description">输入描述</param> public Car( string description) { this .description = description; } //Client will supply the color /// <summary> /// /// </summary> /// <param name="color">输入颜色</param> public void AboutMe( string color) { Console.WriteLine($ "{description} 为 {color} ." ); } } /// <summary> /// A 'ConcreteFlyweight' class called Bus /// 公交车 /// </summary> class Bus : IVehicle { /* * It is intrinsic state and * it is independent of flyweight context. * this can be shared.So, our factory method will supply * this value inside the flyweight object. */ /// <summary> /// 描述 /// </summary> private string description; /// <summary> /// /// </summary> /// <param name="description">输入描述</param> public Bus( string description) { this .description = description; } //Client will supply the color /// <summary> /// /// </summary> /// <param name="color">输入颜色</param> public void AboutMe( string color) { Console.WriteLine($ "{description} 为 {color} ." ); } } /// <summary> /// A 'ConcreteFlyweight' class called FutureVehicle /// 未来车辆 /// </summary> class FutureVehicle : IVehicle { /* * It is intrinsic state and * it is independent of flyweight context. * this can be shared.So, our factory method will supply * this value inside the flyweight object. */ /// <summary> /// 描述 /// </summary> private string description; /// <summary> /// /// </summary> /// <param name="description">输入描述</param> public FutureVehicle( string description) { this .description = description; } //Client cannot choose color for FutureVehicle //since it's unshared flyweight,ignoring client's input /// <summary> /// /// </summary> /// <param name="color">输入颜色</param> public void AboutMe( string color) { Console.WriteLine($ "{description} 为 {color} ." ); } } /// <summary> /// The factory class for flyweights implemented as singleton. /// 车辆工厂类 /// </summary> class VehicleFactory { /// <summary> /// /// </summary> private static readonly VehicleFactory Instance = new VehicleFactory(); /// <summary> /// /// </summary> private Dictionary< string , IVehicle> vehicles = new Dictionary< string , IVehicle>(); /// <summary> /// /// </summary> private VehicleFactory() { vehicles.Add( "小汽车" , new Car( "一辆小汽车制造" )); vehicles.Add( "公交车" , new Bus( "一辆公交车制造" )); vehicles.Add( "概念车" , new FutureVehicle( "概念车 T2050 制造" )); } /// <summary> /// /// </summary> public static VehicleFactory GetInstance { get { return Instance; } } /* * To count different type of vehicle * in a given moment. */ /// <summary> /// 数量 /// </summary> public int TotalObjectsCreated { get { return vehicles.Count; } } /// <summary> /// /// </summary> /// <param name="vehicleType"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public IVehicle GetVehicleFromVehicleFactory( string vehicleType) { IVehicle vehicleCategory = null ; if (vehicles.ContainsKey(vehicleType)) { vehicleCategory = vehicles[vehicleType]; return vehicleCategory; } else { throw new Exception( "Currently, the vehicle factory can have cars and buses only." ); } } } |
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 | /// <summary> ///享元模式 Flyweight Pattern /// geovindu,Geovin Du edit /// </summary> public class FormattedText { /// <summary> /// /// </summary> private readonly string plainText; /// <summary> /// /// </summary> /// <param name="plainText"></param> public FormattedText( string plainText) { this .plainText = plainText; capitalize = new bool [plainText.Length]; } /// <summary> /// /// </summary> /// <param name="start"></param> /// <param name="end"></param> public void Capitalize( int start, int end) { for ( int i = start; i <= end; ++i) capitalize[i] = true ; } /// <summary> /// /// </summary> private readonly bool [] capitalize; /// <summary> /// 大写 /// </summary> /// <returns></returns> public override string ToString() { var sb = new StringBuilder(); for ( var i = 0; i < plainText.Length; i++) { var c = plainText[i]; sb.Append(capitalize[i] ? char .ToUpper(c) : c); } return sb.ToString(); } } /// <summary> /// /// </summary> public class BetterFormattedText { private readonly string plainText; private readonly List<TextRange> formatting = new List<TextRange>(); /// <summary> /// /// </summary> /// <param name="plainText"></param> public BetterFormattedText( string plainText) { this .plainText = plainText; } /// <summary> /// /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public TextRange GetRange( int start, int end) { var range = new TextRange { Start = start, End = end }; formatting.Add(range); return range; } /// <summary> /// 大写 /// </summary> /// <returns></returns> public override string ToString() { var sb = new StringBuilder(); for ( var i = 0; i < plainText.Length; i++) { var c = plainText[i]; foreach ( var range in formatting) if (range.Covers(i) && range.Capitalize) c = char .ToUpperInvariant(c); sb.Append(c); } return sb.ToString(); } /// <summary> /// /// </summary> public class TextRange { public int Start, End; public bool Capitalize, Bold, Italic; public bool Covers( int position) { return position >= Start && position <= End; } } } |
调用:
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 | //享元模式 Flyweight Pattern Console.WriteLine( "***享元模式 Flyweight Pattern Demo.***\n" ); //VehicleFactory vehiclefactory = new VehicleFactory(); VehicleFactory vehiclefactory = VehicleFactory.GetInstance; IVehicle vehicle; /* * Now we are trying to get the 3 cars. * Note that:we need not create additional cars if * we have already created one of this category */ for ( int i = 0; i < 3; i++) { vehicle = vehiclefactory.GetVehicleFromVehicleFactory( "小汽车" ); vehicle.AboutMe( "绿色" ); } int numOfDistinctRobots = vehiclefactory.TotalObjectsCreated; Console.WriteLine($ "\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n" ); /*Here we are trying to get the 5 more buses. * Note that: we need not create * additional buses if we have * already created one of this category */ for ( int i = 0; i < 5; i++) { vehicle = vehiclefactory.GetVehicleFromVehicleFactory( "公交车" ); vehicle.AboutMe( "红色" ); } numOfDistinctRobots = vehiclefactory.TotalObjectsCreated; Console.WriteLine($ "\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n" ); /*Here we are trying to get the 2 future vehicles. * Note that: we need not create * additional future vehicle if we have * already created one of this category */ for ( int i = 0; i < 2; i++) { vehicle = vehiclefactory.GetVehicleFromVehicleFactory( "概念车" ); vehicle.AboutMe( "白色" ); } numOfDistinctRobots = vehiclefactory.TotalObjectsCreated; Console.WriteLine($ "\n Now, total numbers of distinct vehicle object(s) is = {numOfDistinctRobots}\n" ); #region test for in-built flyweight pattern Console.WriteLine( "**Testing String interning in .NET now.**" ); string firstString = "A simple string" ; string secondString = new StringBuilder().Append( "A" ).Append( " simple" ).Append( " string" ).ToString(); string thirdString = String.Intern(secondString); Console.WriteLine((Object)secondString == (Object)firstString); // Different references. Console.WriteLine((Object)thirdString == (Object)firstString); // The same reference. #endregion //大写 var ft = new FormattedText( "This is a geovindu new world" ); ft.Capitalize(10, 17); Console.WriteLine(ft); //大写 var bft = new BetterFormattedText( "This is a lukfook new world" ); bft.GetRange(10, 16).Capitalize = true ; Console.WriteLine(bft); Console.ReadKey(); |
输出:
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 | ***享元模式 Flyweight Pattern Demo.*** 一辆小汽车制造 为 绿色 . 一辆小汽车制造 为 绿色 . 一辆小汽车制造 为 绿色 . Now, total numbers of distinct vehicle object (s) is = 3 一辆公交车制造 为 红色 . 一辆公交车制造 为 红色 . 一辆公交车制造 为 红色 . 一辆公交车制造 为 红色 . 一辆公交车制造 为 红色 . Now, total numbers of distinct vehicle object (s) is = 3 概念车 T2050 制造 为 白色 . 概念车 T2050 制造 为 白色 . Now, total numbers of distinct vehicle object (s) is = 3 **Testing String interning in .NET now.** False True This is a GEOVINDU new world This is a LUKFOOK new world |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!