0

私はパターンを設計するのが初めてです。現在のアプリケーションでファクトリ メソッド デザイン パターンを使用しようとしています。アプリケーションには 2 つのグループ (つまり、group1 と group2) があり、各グループには異なるメソッドがあります。アプリケーション ロールをクラスとして使用し、製品 (つまり、group2) から継承することをお勧めします。次のサンプル コードが正しいか間違っているかを誰か教えてください。

class Creator
{
    static void Main(string[] args)
    {
        if (args[0] == "GroupHead")
        {
            IGroup2 gh = new GroupHead();
        }
        else if (args[0] == "ProjectIncharge")
        {
            IGroup2 gh = new ProjectIncharge();
        }
    }
}


interface IGroup1
{
    List<Employee> GetEmployees();
}

interface IGroup2
{
    List<Projects> GetProjects();
}

public class Group : IGroup1
{
    public List<Employee> GetEmployees()
    {
        //Code here
    }
}

public class GroupHead : IGroup2
{
    public List<Projects> GetProjects()
    {
        //Code here
    }
}

public class ProjectIncharge : IGroup2
{
    public List<Projects> GetProjects()
    {
        //Code here
    }
}

public class ProjectManager : IGroup2
{
    public List<Projects> GetProjects()
    {
        //Code here
    }
}
4

1 に答える 1

0

次のパターンを使用して、検索オプションに従ってオブジェクトを初期化するためにこれを返すことができます。

        interface IGroup2
        {
            string SearchOption { get; set; }
            List<Projects> GetProjects();
        }

        public class GroupHead : IGroup2
        {
            public string SearchOption { get; set; }

            public GroupHead()
            {
                SearchOption = "GroupHead";
            }

            public List<Projects> GetProjects()
            {   
                //Code here
                return null;
            }
        }

        public class ProjectIncharge : IGroup2
        {
            public string SearchOption { get; set; }

            public ProjectIncharge()
            {
                SearchOption = "ProjectIncharge";
            }

            public List<Projects> GetProjects()
            {
                //Code here
                return null;
            }
        }

        public class ProjectManager : IGroup2
        {
            public string SearchOption { get; set; }

            public ProjectManager()
            {
                SearchOption = "ProjectManager";
            }

            public List<Projects> GetProjects()
            {
                //Code here
                return null;
            }
        }

public class Test
{
        private static List<IGroup2> searchRuleList = new List<IGroup2>()
        {
          new GroupHead(),
          new ProjectIncharge(),
          new ProjectManager(),
        };

        public static void Main(string[] args)
        {
         IGroup2 searchOptionRule = searchRuleList.Find(delegate(IGroup2 searchRule) { return searchRule.SearchOption.Equals(args[0]); });

        } 
}
于 2013-09-12T07:36:12.720 に答える