I have a system that handles many types of worker roles (around 30) in the company where I work. They all have data in common (such as personal data). What I would like to make more efficient is to reuse method code for many types of workers. Some of the classes are: Manager
, Director
and Employee
each one must check its input time and I do that with another class called Company
, so I have three methods for the class:
public class Company
{
public void CheckIn(Director Person)
{
}
public void CheckIn(Employee Person)
{
}
public void CheckIn(Manager Person)
{
}
}
Depending on the role that enters the company, different things happen at the entrances to the entire building. I hope I made myself understood.
You can use an inheritance class or an interface
Persona
where all the common members of all the classes you mention contain itSo you would only have one method
CheckIn
:For example:
with interface
Note on properties in interfaces
and with a class
Either way you need to reference them in the "children", in your class
Director
you would have to haveeither
Either way you choose, you'll speed up your development a lot.
Sure, what you can do is use Interfaces. The interfaces allow you to establish rules for the classes (attributes and methods that by law they must have). Here is a code example:
And when you call your check input method you do it like this:
based on what you say
In addition to the option of using interfaces, perhaps it is enough to use some property that gives you the role of your class
Person
, for examplePerson.Rol
, in addition to there being a type of check by role, for exampleCheckingType
, to group the different roles with similar behavior .Since you do not specify the code that is inside the CheckIn methods, I will try to answer in the most general way possible:
In the first place, the first thing you should try is to create a worker interface that contains the fields and methods that must be common to all. For example:
Then each type of worker can implement these fields and methods
In this way assuming for example that we have the following class
Your method could be reduced to:
But this is not always possible.
What if you can't modify the classes for some reason so that you can't make them implement the same interface. And even what if the classes don't have the same fields?
This can be fixed with the Adapter pattern
This pattern is used to change the "interface" (term used generically, not like
interface
C#) of the class you need to adapt it to a particular APISay for example we have the following classes:
In this case we can create an adapter that standardizes the interface
And then we can create a subtype for each Role
Where each implementation can have its own logic
Then the class
Company
would be like thisThis way you can make the CheckIn method always depend on a single class or interface and not have one for each type of employee.
De todas maneras estas solo son algunas ideas, tu caso puede variar considerablemtente pero la idea es la misma, estandarizar la interfaz para poderla hacer única para todo tipo de clases.
To respect the concepts
SOLID
, you should not use any if or swith that apply logic according to the person you must validate.That's why I thought to pose something like this
As you will see with inheritance you can make the class implement its concrete validation. would you invoke it like this
In this case use a
virtual
to define a base implementation, so you only have to override when necessary, but if you don't want to you could define a method likeabstract
I want to add my answer, although I suggest (like the others) to use an interface on the object, however another option would be the following:
Greetings and here you have the reference to "is"
También se puede utilizar una definición de una clase con parámetros genéricos