C# 设计模式(12)享元模式
享元模式
1.解决对象复用问题
2.不破坏原本类的封装
3.池化资源管理:避免对象重复创建和销毁(尤其是非托管资源)
4.字符串 内存分配使用是享元模式(排除部分分配)
代码实现:
namespace FlyWeightPattern { public interface ILetter { public string GetLetter(); } class LetterA:ILetter { private string name = "A"; public LetterA() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } } class LetterE:ILetter { private string name = "E"; public LetterE() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } } class LetterN:ILetter { private string name = "N"; public LetterN() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } } class LetterP:ILetter { private string name = "P"; public LetterP() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } } class LetterR:ILetter { private string name = "R"; public LetterR() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } } private string name = "T"; public LetterT() { Console.WriteLine($"Letter {name} has been Created"); } public string GetLetter() { return name; } }
享元封装:
public class FlyWeight { private static Dictionary<LetterType, ILetter> _letterDictionary = new Dictionary<LetterType, ILetter>(); private static object _flyWeightLock = new object(); public static ILetter CreateWordPattern(LetterType letterType) { if (!_letterDictionary.ContainsKey(letterType)) { lock (_flyWeightLock) { if (!_letterDictionary.ContainsKey(letterType)) { switch (letterType) { case LetterType.A: _letterDictionary.Add(letterType, new LetterA()); break; case LetterType.P: _letterDictionary.Add(letterType, new LetterP()); break; case LetterType.T: _letterDictionary.Add(letterType, new LetterT()); break; case LetterType.E: _letterDictionary.Add(letterType, new LetterE()); break; case LetterType.R: _letterDictionary.Add(letterType, new LetterR()); break; case LetterType.N: _letterDictionary.Add(letterType, new LetterN()); break; default: throw new ArgumentOutOfRangeException(nameof(letterType), letterType, null); } } } } return _letterDictionary[letterType]; } public enum LetterType { P, A, T, E, R, N } }
代码调用:
class Program { static void Main(string[] args) { Task.Run(() => { for (int i = 0; i < 3; i++) { ILetter letter_P = FlyWeight.CreateWordPattern(FlyWeight.LetterType.P); ILetter letter_A = FlyWeight.CreateWordPattern(FlyWeight.LetterType.A); ILetter letter_T = FlyWeight.CreateWordPattern(FlyWeight.LetterType.T); ILetter letter_E = FlyWeight.CreateWordPattern(FlyWeight.LetterType.E); ILetter letter_R = FlyWeight.CreateWordPattern(FlyWeight.LetterType.R); ILetter letter_N = FlyWeight.CreateWordPattern(FlyWeight.LetterType.N); Console.WriteLine( $"{letter_P.GetLetter().ToUpper()}{letter_A.GetLetter().ToLower()}{letter_T.GetLetter().ToLower()}{letter_T.GetLetter().ToLower()}{letter_E.GetLetter().ToLower()}{letter_R.GetLetter().ToLower()}{letter_N.GetLetter().ToLower()}" ); } }); Console.ReadLine(); } }
结果: