public static void FlyweightDemo() { int countOfIterations = 1000; Stopwatch sw = new Stopwatch(); string[] pictures = { "wall", "bomb", "box" }; var graphics = new GraphicsRepository <Bitmap, Color>(); var picturesFlyweight = new PictureFlyweight <Bitmap, Color>(); sw.Start(); Color[,] temp; for (int i = 0; i < countOfIterations; i++) { GraphicsAdapter <Bitmap, Color> pic = graphics.CreateGraphicsObject(25, 25); pic.SetImage(Images.wall); temp = pic.GetColorArray(); pic.SetImage(Images.bomb); temp = pic.GetColorArray(); pic.SetImage(Images.box); temp = pic.GetColorArray(); } sw.Stop(); Console.WriteLine("Time to read " + countOfIterations + " elements without flyweight " + sw.ElapsedMilliseconds + "ms"); sw.Reset(); sw.Start(); for (int i = 0; i < countOfIterations; i++) { for (int j = 0; j < pictures.Length; j++) { temp = picturesFlyweight.GetPictureArray(pictures[j]); } } sw.Stop(); Console.WriteLine("Time to read " + countOfIterations + " elements with flyweight " + sw.ElapsedMilliseconds + "ms"); }
public void WrongTypesTest() { GraphicsRepository <Color, Bitmap> graphics = new GraphicsRepository <Color, Bitmap>(); bool exceptionThrown = false; try { var bgColor = graphics.GetBackgroundColor(); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.CreateGraphicsObject(10, 10); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.ColorFromArgb(10, 10, 10, 10); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.ColorA(new Bitmap(10, 10)); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.ColorB(new Bitmap(10, 10)); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.ColorR(new Bitmap(10, 10)); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { var bgColor = graphics.ColorG(new Bitmap(10, 10)); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); }
public void CreateGraphicsObjectTest() { GraphicsRepository <Bitmap, Color> graphics = new GraphicsRepository <Bitmap, Color>(); var gObject = graphics.CreateGraphicsObject(10, 10); }