When developing interfaces, should they be kept as generic as possible or should you try to put as many methods, properties in an interface to keep the number of interfaces low: As an example, which is better 1 or 2:
1) Customer and Rental split into 2 interfaces (Data only relevant to a rental is in Rental interface and data only relevant to a customer is in the Customer interface)
interface ICustomer
{
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
interface IRental: ICustomer
{
string Title { get; set; }
decimal Cost{ get; set; }
void Rent();
}
2) Put all data into one interface.
interface IRental
{
string Title { get; set; }
decimal Cost{ get; set; }
void Rent();
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
Also regarding the first approach, is there a benefit to extending the ICustomer interface or should there just be an ICustomer property in IRental like the following:
interface IRental
{
ICustomer customer {get;set;}
string Title { get; set; }
decimal Cost{ get; set; }
void Rent();
}
What are the advantages/disadvantages of the approaches above? and is there a preferred way (one that is more scalable and maintainable).
Look into the Interface Segregation Principle of SOLID. Fat interfaces can be problematic, implementers and consumers are forced to care about more things than they need. Keep your interfaces thin and highly focused. An example used is often the concept of a modem
interface Modem
{
void Dial();
void Hangup();
void Send();
void Receive();
}
Implementers of Modem
have to provide implementations for dialing and hanging up, which are connection state issues. And then provide implementations for sending and receiving, which are data transfer issues. These should possibly be two unique interfaces, they are two different groups of responsibilities, which also goes into the Single Responsibility Principle. And not all modems might need both sets of responsibilities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With