public Chars GetChar(string str) { var chars = charFactory[str] as Chars; if (chars == null) { switch (str) { case "A":chars = new CharA();break; case "B":chars = new CharB(); break; } charFactory.Add(str, chars); } return chars; }
public FlyweightPattern() { CharFactory charFactory=new CharFactory(); var ca = charFactory.GetChar("A"); ca.SetSize(10); ca.Write(); CharB cb1 = (CharB)charFactory.GetChar("B"); cb1.SetSize(12); cb1.Write(); CharB cb2 = (CharB)charFactory.GetChar("B"); cb2.SetSize(12); cb2.Write(); Console.WriteLine(object.ReferenceEquals(cb1, cb2)); CharB cb3 = new CharB(); cb3.SetSize(12); Console.WriteLine(object.ReferenceEquals(cb1, cb3)); }