Factory Pattern without a Switch or If/Then

前端 未结 4 1705
离开以前
离开以前 2021-01-30 14:29

I\'m looking for a simple example of how to implement a factory class, but without the use of a Switch or an If-Then statement. All the examples I can find use one. F

4条回答
  •  渐次进展
    2021-01-30 15:15

    How about this (no Dictionary required and note that you will get an syntax error if your try to Create()):

    EDIT - Updated to use an IPosition interface implemented explicitly. Only instances of IPosition can access the member functions (e.g. .Title will not compile).

    EDIT #2 Factory.Create should return an IPosition not T when using the interface properly.

    using System;
    using System.Collections.Generic;
    
    class Program
    {
        interface IPosition
        {
            string Title { get; }
            bool RequestVacation();
        }
    
        class Manager : IPosition
        {
             string IPosition.Title
            {
                get { return "Manager"; }
            }
    
            bool IPosition.RequestVacation()
            {
                return true;
            }
        }
    
        class Clerk : IPosition
        {
            int m_VacationDaysRemaining = 1;
    
            string IPosition.Title
            {
                get { return "Clerk"; }
            }
    
            bool IPosition.RequestVacation()
            {
                if (m_VacationDaysRemaining <= 0)
                {
                    return false;
                }
                else
                {
                    m_VacationDaysRemaining--;
                    return true;
                }
            }
        }
    
        class Programmer : IPosition
        {
            string IPosition.Title
            {
                get { return "Programmer"; }
            }
    
            bool IPosition.RequestVacation()
            {
                return false;
            }
        }
    
        static class Factory
        {
            public static IPosition Create() where T : IPosition, new ()
            {
                return new T();
            }
        }
    
        static void Main(string[] args)
        {
            List positions = new List(3);
            positions.Add(Factory.Create());
            positions.Add(Factory.Create());
            positions.Add(Factory.Create());
    
            foreach (IPosition p in positions) { Console.WriteLine(p.Title);  }
            Console.WriteLine();
    
            Random rnd = new Random(0);
            for (int i = 0; i < 10; i++)
            {
                int index = rnd.Next(3);
                Console.WriteLine("Title: {0}, Request Granted: {1}", positions[index].Title, positions[index].RequestVacation());
            }
    
            Console.ReadLine();
        }
    }
    

提交回复
热议问题