nterface InterfaceToimplement
{
string Name { get; set; }
}
Implementing Classes
class Student:InterfaceToimplement
{
public string Name { get; set; }
public int ID { get; set; }
public int RollNumber { get; set; }
}
class Room : InterfaceToimplement
{
public string Name { get; set; }
public int Length { get; set; }
public int Breadth { get; set; }
public int Height { get; set; }
}
class Account : InterfaceToimplement
{
public string Name { get; set; }
public string AccountholderName { get; set; }
public int AccountID { get; set; }
public int Amount { get; set; }
}
Factory Class
class ClassFactory
{
public InterfaceToimplement GetClass(string classname)
{
InterfaceToimplement ObjectClass = null;
switch (classname)
{
case "Student":
ObjectClass = new Student();
break;
default:
break;
}
return ObjectClass;
}
}
Calling Method
static void Main(string[] args)
{
string classname = "Student";
ClassFactory clsfactory = new ClassFactory();
InterfaceToimplement GetObjIntrface = null;
GetObjIntrface = clsfactory.GetClass(classname);
}
When I use GetObjIntrface. Then i use only properties of interface but i want all the properties of class Student. Suggest right approch
Loading
VulpesPosted Jul 19, 2014, 4:12 AM
If you need access to all the members of a particular type, then - as you say - you might as well just define methods or properties which return objects of that type.
Upkar SrivastavaPosted Jul 19, 2014, 12:52 AM
If i typecast interface as class
Student student = GetObjIntrface as Student;
then what is use of making interface.I can simply create instance of class without use of interface
Student student = new Student();
I use interface as i return any one of the class at runtime.and it will be loosly coupled
VulpesPosted Jul 18, 2014, 4:03 PM
}