Click here to Skip to main content
15,906,285 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have 2 classes called Employee and Student derived from single abstract class.In those 2 classes has static method called 'Load' to create objects of its own. Now I want to call Load method from those classes by using a single common method. How can I implement this method to create object from given class.
Posted
Updated 25-Apr-11 17:47pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 0:02am    
Pretty interesting question, my 5.
--SA

1 solution

If you make is a single regular static method, it will abuse OOP, because it will require a case or if statement checking for some parameter of this method and calling different constructors.

If is possible to do automatically in some other languages supporting true meta-classes and virtual static classes; a very powerful way of making the class factory (example: Delphi Pascal). In .NET it is not possible.

You can do it through generic method though:

C#
abstract class Person {
    internal DERIVED Factory<DERIVED>()
        where DERIVED : Person, new() {
        //most important thing is the constraints; see the above line
            return new DERIVED();
    }
    //...
}

class Employee : Person {
    internal protected Employee() { /*...*/ }
    //...
}
class Student : Person {
    internal protected Student() { /*...*/ }
    //...
}

//...

Student myStudent = Person.Factory<Student>();
Empoyee myEmpoyee = Person.Factory<Empoyee>();


On problem of this approach is: factory assumes the constructor used in the factory is private. In this case it won't work as it is needed for the factory implemented in the base class. So, the constructors can be only internal protected.

Another possibility is using Reflection. You can pass the parameter of System.Type and instantiate it using System.Reflection.ConstructorInfo. It will take some effort and cannot be very useful. Why? Because there is not a meta-class, there is only one type called System.Type for all you classes. Therefore, you can not apply constraints like DERIVED : Person, new(). You won't be able to constrant the set of types to a classes derived from Person. This fact defeats the purpose of Reflection-based method.

After comparing these possibilities I would recommend to give up on a single method. Having a separate factory method in each derived class is much more practical and maintainable:

C#
abstract class Person { /*...*/ }
class Employee : Person {
   internal static Employee MakeInstance(/*...*/) { return new Employee(/*...*/); }
   private Employee(/*...*/) { /*...*/ } //important to make it private
}


Second practical method would be using generics as show above.


—SA
 
Share this answer
 
v5
Comments
Tarakeshwar Reddy 26-Apr-11 0:03am    
Good explanation
Sergey Alexandrovich Kryukov 26-Apr-11 0:04am    
Thank you, Tarakeshwar.
I just updated it, by the way.
--SA
Sandeep Mewara 26-Apr-11 0:40am    
My 5+++

Was good to read/know. Thanks.
Sergey Alexandrovich Kryukov 26-Apr-11 0:50am    
Thank you Sandeep.
Generated on the fly, by the way. Maybe, there could be other ideas?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900