コード例 #1
0
        public static void Main(String[] args)
        {
            //get shape factory
            AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

            //get an object of Shape Circle
            IShape shape1 = shapeFactory.getShape("CIRCLE");

            //call draw method of Shape Circle
            shape1.draw();

            //get an object of Shape Rectangle
            IShape shape2 = shapeFactory.getShape("RECTANGLE");

            //call draw method of Shape Rectangle
            shape2.draw();

            //get an object of Shape Square
            IShape shape3 = shapeFactory.getShape("SQUARE");

            //call draw method of Shape Square
            shape3.draw();

            //get color factory
            AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

            //get an object of Color Red
            IColor color1 = colorFactory.getColor("RED");

            //call fill method of Red
            color1.fill();

            //get an object of Color Green
            IColor color2 = colorFactory.getColor("Green");

            //call fill method of Green
            color2.fill();

            //get an object of Color Blue
            IColor color3 = colorFactory.getColor("BLUE");

            //call fill method of Color Blue
            color3.fill();

            Console.ReadKey();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: ideal521/csharp-utility
        static void Main(string[] args)
        {
            //获取形状工厂
            AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");

            //获取形状为 Circle 的对象
            Shape shape1 = shapeFactory.getShape("AbstractFactoryPattern.Circle");

            //调用 Circle 的 draw 方法
            shape1.draw();

            //获取形状为 Rectangle 的对象
            Shape shape2 = shapeFactory.getShape("AbstractFactoryPattern.Rectangle");

            //调用 Rectangle 的 draw 方法
            shape2.draw();

            //获取形状为 Square 的对象
            Shape shape3 = shapeFactory.getShape("AbstractFactoryPattern.Square");

            //调用 Square 的 draw 方法
            shape3.draw();

            //获取颜色工厂
            AbstractFactory colorFactory = FactoryProducer.getFactory("Color");

            //获取颜色为 Red 的对象
            Color color1 = colorFactory.getColor("AbstractFactoryPattern.Red");

            //调用 Red 的 fill 方法
            color1.fill();

            //获取颜色为 Green 的对象
            Color color2 = colorFactory.getColor("AbstractFactoryPattern.Green");

            //调用 Green 的 fill 方法
            color2.fill();

            //获取颜色为 Blue 的对象
            Color color3 = colorFactory.getColor("AbstractFactoryPattern.Blue");

            //调用 Blue 的 fill 方法
            color3.fill();

            Console.ReadKey();
        }