【问题标题】:c# class factory newbiec# 类工厂新手
【发布时间】:2012-11-28 09:26:50
【问题描述】:

这可能是我正在寻找的错误标题,但我认为它归结为一个类工厂。

我有三个班级:

   class Horse : Animal
   class Cow :   Animal

我想要创建的是 Animal 中的一个方法,它是伪编码的,会像这样工作:

   List<Horse or Cow> (Animal horseOrCow)
   {
      if (horseOrCow is of type Horse)
         return a list of 10 Horse objects; 
      else
         return a list of 10 Cow objects;
   }

当然是简化,但是一旦我掌握了如何做到这一点,我应该能够弄清楚其余的。

(编辑:错字已修复)。

【问题讨论】:

  • 这太模糊了。更大更具体的图景是什么?

标签: c# class factory


【解决方案1】:

您可以使用is-operatorEnumerable.OfType + Enumerable.Take

public List<Animal> getMammals(Mammal horseOrCow)
{
    if (horseOrCow is Horse)
        return allAnimals.OfType<Horse>().Take(10).Cast<Animal>().ToList();
    else if (horseOrCow is Cow)
        return allAnimals.OfType<Cow>().Take(10).Cast<Animal>().ToList();
    else
        throw new ArgumentException("Invalid Mammal", "horseOrCow");
}

假设有一个List&lt;Animal&gt; allAnimalssomewhere。

编辑Mammal 也必须是 Animal,因为 HorseCowMammals,它们应该继承自它。

class Horse : Mammal{ }
class Cow : Mammal { }
class Animal { }
class Mammal : Animal { }

【讨论】:

    【解决方案2】:

    您可以使用generic method of type

    List<T> YourMethod<T>(T horseOrCow) where  T : Mamal
    {
       // your code
    }
    

    【讨论】:

      【解决方案3】:

      这应该适合你:

      abstract class Animal
      abstract class Mammal : Animal  
      class Horse : Mammal 
      class Cow :   Mammal 
      
      List<T> (T mammal) where  T : Mammal  //mammal is cow or horse
      {
         if (horseOrCow is Horse)
            return new List<Horse>(); //add the horses to your list 
         else
            return new List<Cow>(); //add the cows to your list
      }
      

      【讨论】:

        【解决方案4】:
        public class Animal
        {
            public List<Mammal> GetMammalList(Mammal mammal)
            {
                List<Mammal> list = new List<Mammal>();
                if (mammal.GetType() == typeof(Horse))
                    for (int x = 0; x < 10; x++)
                        list.Add(new Horse());
                else if (mammal.GetType() == typeof(Cow))
                    for (int x = 0; x < 10; x++)
                        list.Add(new Cow());
                else
                    throw new ArgumentOutOfRangeException();
        
                return list;
            }
        }
        
        public class Mammal : Animal { }
        
        public class Horse : Mammal { }
        
        public class Cow : Mammal { }
        

        【讨论】:

          【解决方案5】:

          我不建议使用is 进行类型检查。而是使用dynamic 来确定对象的运行时类型。

          public void List<T> GetMammals<T>(T mammal) where  T : Mammal
          {
             GetMammalsSpecialization(mammal as dynamic);
          }
          
          private void List<Horse> GetMammalsSpecialization(Horse horse)
          {
              // Specialized code to return list of horses
          }
          
          private void List<Cow> GetMammalsSpecialization(Cow cow)
          {
              // Specialized code to return list of cows
          }
          

          这是double dispatch问题的更好解决方案。

          【讨论】:

          • 拥有一个泛型类工厂一开始并不是一个好主意。
          【解决方案6】:
          public class Animal
          { 
             public enum AnimalType
             {
               CowType,
               HorseType
             }
          
          
             ....
             public Animal Create(AnimalType type)
             {
               Animal result = null;
                switch (type)
                {
                 case HorseType: result = new Horse();
                 case CowType : ....   
                }
               return result;
             }
          }
          

          【讨论】:

          • 我认为他可能需要传递一匹真正的马或牛。 OP确认?
          • 这是一个类工厂,这是它的一种实现方式。
          • 超类永远不应该知道他们的子类。
          • 这不是超类这是抽象工厂,抽象工厂方法。所以,请在标记答案之前处理问题。
          猜你喜欢
          • 1970-01-01
          • 2014-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多