コード例 #1
0
        static void Main(string[] args)
        {
            ShapeObjectFactory sof = new ShapeObjectFactory();

            IShape shape = sof.GetShape("Triangle");

            shape.Print();
            shape = sof.GetShape("Triangle");
            shape.Print();
            shape = sof.GetShape("Triangle");
            shape.Print();

            shape = sof.GetShape("Square");
            shape.Print();
            shape = sof.GetShape("Square");
            shape.Print();
            shape = sof.GetShape("Square");
            shape.Print();

            int total = sof.TotalObjectsCreated;

            Console.WriteLine($"\n Number of objects created = {total}");

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //first, create the shape factory.
            ShapeObjectFactory sof = new ShapeObjectFactory();

            //create some shapes (or get them if they already exist)
            IShape shape = sof.GetShape("Triangle");

            shape.Print();
            shape = sof.GetShape("Triangle");
            shape.Print();
            shape = sof.GetShape("Triangle");
            shape.Print();

            shape = sof.GetShape("Square");
            shape.Print();
            shape = sof.GetShape("Square");
            shape.Print();
            shape = sof.GetShape("Square");
            shape.Print();
            //result of all of that: Triangle and Square will print three times.
            //BUT total objects are only two (one of each).
            //ie, methods will run on the created objects, it just runs the methods
            //on the version of the shape that already exists.

            int total = sof.TotalObjectsCreated;

            Console.WriteLine($"\n Number of objects created = {total}");

            Console.ReadKey();
        }