I am writing a simple console application and i want to be able to print to the screen.
public class Car{
public void Drive(){
//Here I want to print that the car is driving
}
}
I could just use Console.Writline() in my Drive() function but I don't think that is a good design because in the future i may want to log that to a file instead. What is the best design to achieve this multi-output logging feature? is there a common interface for Console and File output (Iwritable) that I can perhaps pass to the Driver() so i can handle both types of output. Or is there an even better design to achieve this?
It's good to be thinking about things like this. It will definitely help you out with more complex projects. You could certainly create an IWritable interface or an abstract class with concrete implementations. Something like:
public interface IWritable
{
void Write(string text);
}
public class ConsoleWriter : IWritable
{
public void Write(string text)
{
Console.WriteLine(text);
}
}
And then your Drive()
method can accept an IWritable
parameter.
public class Car
{
public void Drive(IWritable writable)
{
writable.Write("I'm driving!");
}
}
new Car().Drive(new ConsoleWriter());
Of course, if your class has several methods that need to do something similar, you might consider changing the constructor to accept an IWritable
parameter and storing it in a field:
public class Car
{
private readonly IWritable _writer;
public Car(IWritable writer)
{
_writer = writer;
}
public void Drive()
{
_writer.Write("I'm driving!");
}
}
Another benefit of doing things this way is it makes unit testing your class much simpler. You can pass it some innocuous writer that won't mess with your log file or update your database, but still let's you test the class.