Esempio n. 1
0
        public static ITile GetTile(string tileType)
        {
            switch (tileType)
            {
            case "Ceramic": if (!tiles.ContainsKey("Ceramic"))
                {
                    tiles["Ceramic"] = new CeramicTile();
                }
                return(tiles["Ceramic"]);

            case "Stone":
                if (!tiles.ContainsKey("Stone"))
                {
                    tiles["Stone"] = new StoneTile();
                }
                return(tiles["Stone"]);

            default:
                break;
            }
            return(null);
        }
Esempio n. 2
0
        public void Run()
        {
            // Not using Flyweight
            for (int i = 0; i < 20; i++)
            {
                ITile ceramicTile = new CeramicTile(GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
                ceramicTile.Draw("CeramicTile");
                ITile stoneTile = new StoneTile(GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
                stoneTile.Draw("StoneTile");
            }
            Console.WriteLine($"Total of files created not using Flyweight pattern and TileFactory factory : {CeramicTile.objectCounter} + {StoneTile.objectCounter} created");


            // Using Flyweight
            for (int i = 0; i < 20; i++)
            {
                ITile ceramicTile = TileFactory.GetTile("Ceramic");
                ceramicTile.Draw("CeramicTile", GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
                ITile stoneTile = TileFactory.GetTile("Stone");
                stoneTile.Draw("StoneTile", GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
            }
            Console.WriteLine($"Total of files created using Flyweight pattern using TileFactory factory : {CeramicTile.objectCounter} + {StoneTile.objectCounter} created");
            Console.ReadKey();
        }