开发者

Is there another way to create an instance without use new?

开发者 https://www.devze.com 2023-03-30 19:17 出处:网络
This is my class employee, and another ones from Employee (it\'s only an example classes): public class Employee

This is my class employee, and another ones from Employee (it's only an example classes):

public class Employee
{
    public int EmployeeID { get; set; }
    public Type type { get; set; }
    public string Name { get; set; }
}

public class Manager : Employee
{
    public void DoSomething()
    {
        // Bla bla
    }
}

public class Salesman : Employee
{
    public void DoAnotherSomething()
    {
        // Bla bla
    }
}

As you can see, my Employee class has a Type property, that contains Manager or Salesman. I don't want to use many switch cases, so is there way to create the object knowing the t开发者_如何学JAVAype variable?

UPDATE 1:

All right, I mean. In my real program. I'm using many switch cases to create the object:

        Employee employee = new Employee();
        // I do somethings to get the values to employee I store it in a Employee list

        // Then I need to create the particular object of each one, I need to travel all 
        // the list an create its instance
        switch (employee.Type)
        {
            case Manager:
                employee = new Manager();

                // Some stuff
                break;
            case Salesman:
                employee = new Salesman();

                // Some stuff
                break;
        }

In my real code, I need to use 130 cases, more or less. So I want to implement another way to avoid this one.


If you are trying to avoid switching to set the Type property, you could define a constructor on Employee which takes type as a parameter:

protected Employee(Type type) { 
    this.type = type;
}

And call it from each subclass's constructor:

public Manager() : base(typeof(Manager)) { }

This pattern also usually calls for Employee to be declared as abstract, so that you can't accidentally create an Employee that isn't a known type. If you want Employees that are just Employees, you could leave it as a concrete (non-abstract) class, and define a constructor that calls the new constructor I suggested, as follows:

public Employee() : this(typeof(Employee)) { }


Instead of inheriting class Employee, why not create an interface IEmployee and have each "Employee" type class you want to create implement that interface?


You never want to switch-on-types. That is a code smell that you are using OOP incorrectly. Instead, you should always strive to use your classes polymorphically. If I understand your question correctly, you have an Employee, and want to make another of the same type. This is called the Virtual Constructor idiom, and is usually handled with a virtual Clone method:

class Employee
{
  public virtual Employee Clone();
};

class Manager
{
  public override Manager Clone()
  {
     return new Manager(this);
  }
};

or something similar.


It sounds here like you want a Factory Pattern to take some descriminator and create an instance and return it through a static method:

class EmployeeFactory
{
  public static Employee NewEmployee(EmployeeType type)
  {
     Employee emp = null;
     switch (type)
     {
        case EmployeeType.Manager :
           emp = new Manager();
           break;
        case EmployeeType.Salesman :
           emp = new Salesman();
           break;
     }
     return emp;
  }
}


You could do a

var factory = new Dictionary<Type, Func<Employee>> {
    Type.Mgr, ()=>new Manager(),
    Type.Sales, ()=>new Salesman()
};

And use it like

var salesman = factory[Type.Sales](); //creates a salesman

(Assuming this is what your question is asking for). But I agree with the other posters, if this is what you're asking for, it's generally considered bad practice anyway.


As you're already having the type, you can use System.Activator?

Type type = typeof(Manager);  
var manager = (Manager) Activator.CreateInstance(type);

Understanding the business case will help answering specifically. It's not clear why you want to instantiate an object where you already know the Type. Instead of passing in the Type-parameter to your factory method, why not pass in a Func or the object directly?

0

精彩评论

暂无评论...
验证码 换一张
取 消