public IImage GetImage(string path) { var item = images.SingleOrDefault(x => x.GetPath() == path); if (item == null) { if (path.Contains(".png")) { item = new PngImage(path); } else if (path.Contains(".gif")) { item = new GifImage(path); } images.Add(item); } return(item); }
static void Main(string[] args) { ImageFactory image = new ImageFactory(); PngImage pngImage = image.GetImage(@"C:\user\p.png") as PngImage; PngImage npng = image.GetImage(@"C:\user\p.png") as PngImage; PngImage npng2 = image.GetImage(@"C:\user\pppp.png") as PngImage; GifImage gifImage = image.GetImage(@"C:\user\123g.gif") as GifImage; Console.WriteLine(pngImage.GetHashCode()); Console.WriteLine(npng.GetHashCode()); Console.WriteLine(npng2.GetHashCode()); Console.WriteLine(gifImage.GetHashCode()); GifImage gifImage2 = image.GetImage(@"C:\user\123g.gif") as GifImage; Console.WriteLine(gifImage2.GetHashCode()); GifImage gifImage3 = image.GetImage(@"C:\user\1234g.gif") as GifImage; Console.WriteLine(gifImage3.GetHashCode()); }