C#简单工厂模式

     #region 简单工厂模式具体实现

        public class Operation
        {
            private double _numberA = 0;
            private double _numberB = 0;
            public double numberA
            {
                get { return _numberA; }
                set { _numberA = value; }
            }

            public double numberB
            {
                get { return _numberB; }
                set { _numberB = value; }
            }

            public virtual double GetResult()
            {
                double result = 0;
                return result;
            }
        }

        public class OpeartionAdd : Operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = numberA + numberB;
                return result;
            }
        }

        public class OpeartionSub : Operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = numberA - numberB;
                return result;
            }
        }

        public class OpeartionMutiply : Operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = numberA * numberB;
                return result;
            }
        }

        public class OpeartionDivide : Operation
        {
            public override double GetResult()
            {
                double result = 0;
                if (0 == numberB)
                {
                    throw new Exception("除数不能为0");
                }
                result = numberA / numberB;
                return result;
            }
        }

        #endregion

        /// <summary>
        /// 工厂创建
        /// </summary>
        public class OpeartionFactory
        {
            public static Operation CreateOperation(string operate)
            {
                Operation operation = null;
                switch (operate)
                {
                    case "+":
                        operation = new OpeartionAdd();
                        break;
                    case "-":
                        operation = new OpeartionAdd();
                        break;
                    case "*":
                        operation = new OpeartionAdd();
                        break;
                    case "/":
                        operation = new OpeartionAdd();
                        break;
                    default:
                        break;
                }
                return operation;
            }
        }


        static void Main(string[] args)
        {
            Operation oper;
            oper = OpeartionFactory.CreateOperation("-3");
            oper.numberA = 1;
            oper.numberB = 3;
            Console.WriteLine(oper.GetResult());
            Console.ReadKey();
        }
    #region 工厂方法模式


        public interface IProduct
        {
            void ProductMethod();
        }
        public class ProductItem : IProduct
        {
            public void ProductMethod()
            {
                Console.WriteLine("产品");
            }
        }

        public interface IFactory
        {
            IProduct CreteProduct();
        }

        public class FactoryItem : IFactory
        {
            public IProduct CreteProduct()
            {
                return new ProductItem();
            }
        }
        #endregion

      static void Main(string[] args)
        {
            IFactory factory = new FactoryItem();
            IProduct product = factory.CreteProduct();
            product.ProductMethod();
            Console.ReadKey();
        }

 

 

namespace 工厂方法模式
{
    public abstract class Product
    {
        public string _ProductName;
    }


    public class ProductOne : Product
    {
        public ProductOne()
        {
            base._ProductName = "ProductOne";
        }
    }

    public class ProductTwo : Product
    {
        public ProductTwo()
        {
            base._ProductName = "ProductTwo";
        }
    }

    public class ProductThree : Product
    {
        public ProductThree()
        {
            base._ProductName = "ProductThree";
        }
    }

    public class ProductFour : Product
    {
        public ProductFour()
        {
            base._ProductName = "ProductFour";
        }
    }

    public class ProductFactoryOne : ProductFactory
    {
        public override Product CreateProduct(string productName)
        {
            switch (productName)
            {
                case "A":
                    return new ProductOne();
                case "B":
                    return new ProductTwo();
                case "C":
                    return new ProductThree();
                case "D":
                    return new ProductFour();
            }
            return null;
        }
    }

    public class ProductFactoryTwo : ProductFactory
    {
        public override Product CreateProduct(string productName)
        {
            switch (productName)
            {
                case "C":
                    return new ProductThree();
                case "D":
                    return new ProductFour();
            }
            return null;
        }
    }
}



namespace 工厂方法模式
{
    public abstract class ProductFactory
    {
        public Product FactoryMethod(string productTypeName)
        {
            Product product;
            product = CreateProduct(productTypeName);
            return product;
        }

        public abstract Product CreateProduct(string productName);
    }
}


namespace 工厂方法模式
{
    class Program
    {
        static void Main(string[] args)
        {
            ProductFactory myProductFactory = new ProductFactoryOne();
            Product myProduct = myProductFactory.FactoryMethod("A");
            Console.WriteLine(myProduct._ProductName);

            myProduct = myProductFactory.FactoryMethod("C");
            Console.WriteLine(myProduct._ProductName);


            ProductFactory myProductFactory2 = new ProductFactoryTwo();
            Product myProduct2 = myProductFactory2.FactoryMethod("C");
            Console.WriteLine(myProduct2._ProductName);

            myProduct2 = myProductFactory2.FactoryMethod("D");
            Console.WriteLine(myProduct2._ProductName);
            Console.ReadKey();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值