2

我有几个与模式有关的问题,如果你们能提供帮助,那就太好了。
我在下面有一个工厂模式代码的示例(底部的代码)-
我的问题是-

  • 问题 1。
  • 例如,如果我需要为 IPeople 实现通用功能怎么办?

    bool GotLegs()
    {
        return true; //Always returns true for both People types
    }
    

    所以如果我想为村民和城市人实现这个通用方法,我可以实现另一种模式吗?

  • 问题2
  • 有没有一种方法可以直接创建 IPeople 类型,而不是实例化对象。例如 -

    IPeople people = Factory.GetPeople(PeopleType.URBAN);
    

    我知道静态不是接口的选项,而只是检查是否有出路。

    实际的 C# 控制台参考代码 -

    //Main Prog
    class Program
    {
        static void Main(string[] args)
        {
            Factory fact = new Factory();
            IPeople people = fact.GetPeople(PeopleType.URBAN);
        }
    }
    
    //Empty vocabulary of Actual object
    public interface IPeople
    {
        string GetName();
    }
    
    public class Villagers : IPeople
    {
    
        #region IPeople Members
    
        public string GetName()
        {
            return "Village Guy";
        }
    
        #endregion
    }
    
    public class CityPeople : IPeople
    {
    
        #region IPeople Members
    
        public string GetName()
        {
            return "City Guy";
        }
    
        #endregion
    }
    
    public enum PeopleType
    {
        RURAL,
        URBAN
    }
    
    /// <summary>
    /// Implementation of Factory - Used to create objects
    /// </summary>
    public class Factory
    {
        public IPeople GetPeople(PeopleType type)
        {
            IPeople people = null;
            switch (type)
            {
                case PeopleType.RURAL:
                    people = new Villagers();
                    break;
                case PeopleType.URBAN:
                    people = new CityPeople();
                    break;
                default:
                    break;
            }
            return people;
        }
    }
    
    4

    2 回答 2

    2

    问题一:

    有几种选择:

    1. 为做GotLegs一个扩展方法IPerson

      public static class PersonExtensions
      {
          public static bool GotLegs(this IPerson person)
          {
              return true;
          }
      }
      

      在那种情况下,IPerson不应该定义GotLegs自己。

    2. 添加GotLegsIPerson接口并创建一个PersonBase实现此方法的基类,并从该基类生成CityPeople和派生。Villagers

    问题2:

    简单的制作GetPeopleFactory静态:

    public static class Factory
    {
        public static IPeople GetPeople(PeopleType type)
        {
            ...
        }
    }
    

    用法就像您展示的那样:

    IPeople people = Factory.GetPeople(PeopleType.URBAN);
    
    于 2013-04-26T15:52:15.613 回答
    0

    如果要添加通用功能,可以使用抽象类而不是接口。在您的情况下,它将是:

    public abstract class People
    {
        public bool GotLegs()
        {
            return true; //Always returns true for both People types
        }
    
        public abstract string GetName();
    }
    
    public class CityPeople : People
    {
    
        #region IPeople Members
    
        public override string GetName()
        {
            return "City Guy";
        }
    
        #endregion
    }
    
    public class Villagers : People
    {
    
        #region IPeople Members
    
        public override string GetName()
        {
            return "Village Guy";
        }
    
        #endregion
    }
    
    于 2013-04-26T19:18:01.800 回答