I am new to object oriented design and learning about interfaces and design patterns. In this example, I am trying to create class for cars. My question:
Is it good practice to use base class and then inherit from it. And then use interfaces for implementing different functions? If not, then what would be the best way to implement this
Base Class:
class BaseCar
{
public string color { get; set; }
public double price { get; set; }
public string carType { get; set; }
}
Interface:
interface CarFunctions
{
void brakeSystem();
void drivingModes(string mode);
void entertainmentSystem();
}
Now I am trying to create concrete classes
class BmwCar : BaseCar, CarFunctions
{
public void brakeSystem()
{
Console.WriteLine("Brake in less than 0.02ms");
}
public void drivingModes(string mode)
{
switch(mode)
{
case "mountain":
{
Console.WriteLine("Switching to 4x4 mode");
break;
}
default:
{
Console.WriteLine("Normal Mode");
break;
}
}
}
public void entertainmentSystem()
{
Console.WriteLine("Music, Navigation");
}
}
Now my question is: Is it good practice to use base class and then inherit from it. And then use interfaces for implementing different functions?
My second question is that should interface have only one function(Single responsibility principle) or it can have multiple functions to be implemented.
Aucun commentaire:
Enregistrer un commentaire