简体   繁体   中英

Factory Method with adding objects to List

I would like to ask for help, I need to fill out the projects, employees sheets with objects of the Project and Employee classes. I would be very grateful for your help.

class Menu
{
    public void menu()
    {
        List<Project> projects = new List<Project>();
        List<Employee> employees = new List<Employee>();
       // Manipulate selection of a menu item
        switch (menu)
        {
            case 1:
                CreateFunc(new EmployeeType());
                break;
            case 2:
                CreateFunc(new ProjectType());
                break;
        }
    }
    static void CreateFunc(CreateType create)
    {
        create.NewObject();
    }
}
abstract class CreateType
{
    public abstract ICreate Add();

    public void NewObject()
    {
        var createtype = Add();
        createtype.Adding();
    }
}

class ProjectType : CreateType
{
    public override ICreate Add()
    {
        return new Project();
    }
}

class EmployeeType : CreateType
{
    public override ICreate Add()
    {
        return new Employee();
    }
}

Interface and Project & Employee classes

Your question seems a bit unclear, but looking at my crystal ball it seems that you need method Activator.CreateInstance

https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8

Static type creation

public class StaticFactory
{
  public static T StaticInstance<T>() where T : class, new()
  {
    var entityType = typeof(T);
    return (T) Activator.CreateInstance(entityType);
  }
}

Usage

var model1 = StaticFactory.StaticInstance<Boss>();
var model2 = StaticFactory.StaticInstance<Boss>();
var models = new List<dynamic> { model1, model2 };

Dynamic type creation

public class DynamicFactory
{
  public static dynamic DynamicInstance(Type someType)
  {
    return Activator.CreateInstance(someType);
  }
}

Usage

var model1 = DynamicFactory.DynamicInstance(typeof(Boss));
var model2 = DynamicFactory.DynamicInstance(someEmployeeVariable.GetType());
var models = new List<dynamic> { model1, model2 };

Interface creation

public interface SomeInterface
{
  void DoWork();
}

public enum EnumType 
{
  Boss, 
  Employee
}

public class InterfaceFactory
{
  public static SomeInterface InterfaceInstance(EnumType enumType)
  {
    switch (enumType) 
    {
      case SomeType.Boss: return new Boss();
      case SomeType.Employee: return new Employee();
    }

    return null;
  }
}

Usage

var model1 = InterfaceFactory.InterfaceInstance(EnumType.Boss);
var model2 = InterfaceFactory.InterfaceInstance(EnumType.Boss);
var models = new List<SomeInterface> { model1, model2 };

You can also extract underlying type from a parent type, eg int in List<int>

List<int> myList = ...
Type myListElementType = myList.GetType().GetGenericArguments().Distinct(); // all types in the list, in this case int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM