Tying a method to implementation classes
I smell a little something...
If your classes all implement IDisplayable, they should each implement their own logic to display themselves. That way your loop would change to something much cleaner:
public interface IDisplayable
{
void Display();
string GetInfo();
}
public class Human : IDisplayable
{
public void Display() { return String.Format("The person is {0}",
GetInfo());
// Rest of Implementation
}
public class Animal : IDisplayable
{
public void Display() { return String.Format("The animal is {0}",
GetInfo());
// Rest of Implementation
}
public class Building : IDisplayable
{
public void Display() { return String.Format("The building is {0}",
GetInfo());
// Rest of Implementation
}
public class Machine : IDisplayable
{
public void Display() { return String.Format("The machine is {0}",
GetInfo());
// Rest of Implementation
}
Then you can change your loop to something much cleaner (and allow the classes to implement their own display logic):
foreach(IDisplayable item in displayableItems)
summary.Append(item.Display());
Yes.
Why not have each class implement a method from IDisplayable
that shows their type:
interface IDisplayable
{
void GetInfo();
public string Info;
}
class Human : IDisplayable
{
public string Info
{
get
{
return "";//your info here
}
set;
}
public void GetInfo()
{
Console.WriteLine("The person is " + Info)
}
}
Then just call your method as follows:
foreach(IDisplayable item in displayableItems)
{
Console.WriteLine(item.GetInfo());
}
seems like IDisplayable should have a method for the display name so you can reduce that method to something like
summary.Append("The " + item.displayName() + " is " + item.getInfo());