Exemple #1
0
        static void Main(string[] args)
        {
            MouseFactory mouseFactory = null;

            Console.WriteLine("Enter the number corresponding to the type of mouse you want:\n 1 - Gamer\n 2 - Office");
            string typeMouseEnter = Console.ReadLine();

            switch (typeMouseEnter)
            {
            case "1":
                mouseFactory = new GamerFactory(8000, "USB");
                break;

            case "2":
                mouseFactory = new OfficeFactory(1000, "Wireless");
                break;

            default:
                Console.WriteLine("Enter a valid number.");
                break;
            }
            Mouse mouse = mouseFactory.SearchMouse();

            Console.WriteLine("\n These are the details of your mouse: ");
            Console.WriteLine($"Mouse Type: {mouse.MouseType}\nDPI: {mouse.Dpi}\nConnection: {mouse.Connection} ");
        }
        /// <summary>
        /// 参数化工厂方法(简单工厂)
        /// </summary>
        static void SimpleFactory()
        {
            //创建工厂
            MouseFactory factory = new MouseFactory();

            //通过工厂生产实体
            //IMouse mouseA = factory.CreateMouse("罗技");
            //IMouse mouseB = factory.CreateMouse("雷蛇");
            IMouse mouseA = factory.CreateMouse(MouseBrandConst.Logitech);
            IMouse mouseB = factory.CreateMouse(MouseBrandConst.Raze);

            Console.WriteLine($"参数化工厂方法(简单工厂){Environment.NewLine}-------------------------------------------");
            Console.WriteLine($"MouseA的品牌是:{mouseA.GetBrand()}");
            Console.WriteLine($"MouseB的品牌是:{mouseB.GetBrand()}");
            Console.ReadKey();
        }