Exemple #1
0
        static void Main(string[] args)
        {
            var factory = new ShapeFactory();

               var square= factory.GetShape("square");

            square.Draw();
        }
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            IShape shape1 = shapeFactory.getShape("Circle");

            shape1.draw();

            IShape shape2 = shapeFactory.getShape("Rectangle");

            shape2.draw();

            IShape shape3 = shapeFactory.getShape("Square");

            shape3.draw();
            Console.ReadKey();
        }
Exemple #3
0
        static void Main(string[] args) {
            ShapeFactory shapeFactory = new ShapeFactory();

            //获取 Circle 的对象,并调用Circle的 draw 方法
            IShape shape1 = shapeFactory.GetShape("CIRCLE");
            shape1.Draw();

            //获取 Rectangle 的对象,并调用Rectangle的 draw 方法
            IShape shape2 = shapeFactory.GetShape("RECTANGLE");
            shape2.Draw();

            //获取 Square 的对象,并调用Square的 draw 方法
            IShape shape3 = shapeFactory.GetShape("SQUARE");
            shape3.Draw();

            Console.ReadLine();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();

            Shape shape1 = shapeFactory.getShape("CIRCLE");

            shape1.draw();

            Shape shape2 = shapeFactory.getShape("RECTANGLE");

            shape2.draw();

            Shape shape3 = shapeFactory.getShape("SQUARE");

            shape3.draw();

            Console.ReadLine();
        }
        public MainWindow()
        {
            InitializeComponent();
            this.Height           = 950;
            this.Width            = 925;
            this.GuiUC.DefineText = "In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.";

            StringBuilder stringBuilder = new StringBuilder();

            ShapeFactory shapeFactory = new ShapeFactory();

            // get an object of Circle and call its draw method.
            Factory.IShape shape1 = shapeFactory.GetShape("Circle");

            // call draw method of Circle
            stringBuilder.Append(shape1.Draw() + "\n");

            // get an object of Rectangle and call its draw method.
            IShape shape2 = shapeFactory.GetShape("Rectangle");

            // call draw method of Rectangle
            stringBuilder.Append(shape2.Draw() + "\n");

            // get an object of Square and call its draw method.
            IShape shape3 = shapeFactory.GetShape("Square");

            // call draw method of circle
            stringBuilder.Append(shape3.Draw() + "\n");

            this.GuiUC.TextBlockText        = stringBuilder.ToString();
            this.GuiUC.TextBlockConsequence = @" Provides hooks for subclasses
• Creating objects inside a class with a factory method is always more flexible than creating an object directly
• Gives subclasses a hook for providing an extended version of an object
 Connects parallel class hierarchies
• In the examples we've considered so far, the factory method is only called by Creators. But this doesn't have to be the case;
clients can find factory methods useful, especially in the case of parallel class hierarchies";
        }