I want to implement an interface which can be used in both Projct and Employe classes. How should I create Add() and ViewAll() methods in the interface, so that I don't have to overload the methods in classes while declaring as the method name is same, but the parameters are different.
public class Employe
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void Add(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> ViewAll()
{
return Employees;
}
}
public class Projct
{
public List<Project> Projects { get; set; } = new List<Project>();
public void Add(Project project)
{
Projects.Add(project);
}
public List<Project> ViewAll()
{
return Projects;
}
}
I understand that Interface is like a contract and it doesn't make sense to change the parameters. But the question is regarding implementation. Also, I have seen majority of threads related to this topic, saw answers related to declaring parameter class or using params and even tried doing that. I still can't figure it out, so if someone can explain from a simple perspective, that would be welcome.
CodePudding user response:
You can create an interface with a generic type parameter:
interface ItemCollection<T>
{
void Add(T item);
List<T> ViewAll();
}
Then you can declare that your classes implement that interface where the generic parameter is replaced by a specific type:
public class Employe : ItemCollection<Employee> { ... }
public class Projct : ItemCollection<Project> { ... }
You still have to implement the methods though, just as you did.
If you actually want to avoid the code redundancy, you should create a class (perhaps using the abstract
keyword) with a generic type parameter instead.
public class ItemCollection<T>
{
private List<T> items = new List<T>();
public void Add(T item)
{
items.Add(item);
}
public List<T> ViewAll()
{
return items;
}
}
You should then let your original classes extend that base class and remove the common code from them.
If this is not just an example you should review the contract defined by the interface / abstract class:
- do you really need to return a (modifiable)
List<T>
or is anIReadOnlyList<T>
,ICollection<T>
,IEnumerable<T>
or one of theImmutable*
types enough ; preferable - Should the object returned by
ViewAll()
change according to modifications applied to theItemCollection
afterwards or should it stay the same? - What's special about your classes and the interface, compared to the existing collection types available which makes them necessary?
CodePudding user response:
Interfaces can be generic:
public interface IMyInterface<T> {
void Add(T t);
List<T> ViewAll();
}
CodePudding user response:
define the interface
public interface IAddAndDisplay{
void Add(T employee);
List<T> ViewAll();
}
now implement it
public class Employee : IAddAndDisplay<Employee>
{
}
public class Project : IAddAndDisplay<Project>
{
}
You already have the correct methods
Having said all that I dont think this will help you very much. Generic interfaces work but dont provide the 'plug and play ' that you are after.