先附上Main()下的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            double numberA;
            double numberB;
            string oper;
            //为什么要声明基类Operatcion??????
            Operation a = null;            Console.WriteLine("请输入第一个数字:");
            numberA = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入操作运算符: + - * / ");
            oper = Console.ReadLine();
            Console.WriteLine("请输入第二个数字:");
            numberB = double.Parse(Console.ReadLine());
            //A被赋值工厂是为了实现多态
            a = OperationFactory.CrateFactory(oper);
            a.NumberA = numberA;
            a.NumberB = numberB;            Console.WriteLine(a.GetResult());
            
            
        }
    }
}
以上代码我不理解为什么要声明基类Operatcion附上创建工厂的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
    class OperationFactory
    {
        /// <summary>
        /// 变量OP用来接前台传来的运算符在下边Switch中进行判断从而使基类指向不同的派生类对象
        /// 为什么要用Operatcion类类型???????
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>        public static Operation CrateFactory(string op)
        {
            //声明基类是为了可以指向不同的派生类
            Operation oper = null;
            switch (op)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
            }
            return oper;
        }
    }
}public static Operation CrateFactory(string op)
为什么在创建工厂的时候要用Operatcion类类型?其它的方法不可以吗?等待解答!~~谢谢了!~

解决方案 »

  1.   


    谢谢Fangxinggood的回答,不过对我这样的初学者来说,不太直白!~我有些不太好理解!~~呵呵!~~
      

  2.   

    楼主OO中继承的作用是什么?是不是为了实现多态。所以如果工厂类不返回基类,返回object,你怎么使用呢?
      

  3.   

    通用
    比如你要找个老婆,是女人("Operation")就行了,管她是春哥("+")还是曾哥("-")
      

  4.   

    这个程序只是演示下工厂怎么实现。而且这个程序写的很糟糕。(按照作者的思路还没有写完)我觉得
    (1)学习设计模式,找本好书,找个好例子。
    (2)重思想方法,而不是编码技巧。为什么用到工厂,工厂的本质是什么是重点。不理解这些看这些为了模式而模式的例子没有意义。
    (3)设计模式的实现随着时间的推移,已经没有用了。事实上现在的编程语言,几乎用不到设计模式。
    比如说C#的委托,代替了策略模式。
    装箱拆箱,代替了享元。
    工厂模式、适配器模式被动态方法以及表达式API取代。
    迭代器模式被自动迭代器取代。
    模板模式几乎没用了。
    纯静态类取代了单例。
    事件取代了观察者模式……
    所以设计模式只是一种思维方法,不要去钻那些没用的代码。
      

  5.   

    简单工厂模式(SimpleFactory)可以这样概述:
    用户请求一个产品,该工厂创建不同的子类,然后作为父类返回。
      

  6.   

    以下分别为student类和teacher类,分别继承自person类
    person类public class person
    {
       public person(){}
       public person(string name,int age)
       {
          this.Name = name;
          this.Age = age;
       }
       public string Name{get;set;}
       public int Age{get;set;}
    }student类public class student : person
    {
        public student(){}
        public student(string name,int age,string hobby)
                      :base(name,age)
        {
            this.Hobby = hobby; 
        }
        public string hobby{get;set;}   //student类特有属性
    }teacher类public class teacher : person
    {
        public teacher(){}
        public teacher(string name,int age,int yearsOfService)
                      :base(name,age)
        {
               this.YearsOfService = yearsOfService; 
        }  
        public int YearsOfService{get;set;}   //teacher类特有属性
        
    }
    简单工厂(SimpleFactory)代码示例public class SimpleFactory
    {
         public static person CreateProduct(string type,string name,int age,
                                            string hobby,int yearsOfService)
         {
               //定义一个person类对象
                person per = null;          switch(type)    //看用户请求哪个子类对象,判断后,传入对应参数,后作为父类返回
              {
                  case"student":
                         per = new student(name,age,hobby);   //向上传递
                         break;
                  case"teacher":
                         per = new teacher(name,age,yearsOfService);
                         break;
              }
              return per;
         }
    }