Reputation: 49
I'm trying to figure out if there is a design pattern for the below scenario.
I have two types of objects say List<JobApplicant>
jobApplicants and List<Job>
jobs, I wanted to select all jobs for which a JobApplicant
is eligible, eligibility criteria can be multiple like JobApplicant
and job skills are matching, experience, location, etc. it can be multiple.
After running all kinds of eligibility criteria I can get all job lists for which a JobApplicant
is eligible. I tried implementing this with the simple eligibility condition, but the condition will keep increasing when there is new eligibility comes.
Are there any design patterns that can describe such a system and/or help on this to make it generic so that we can keep adding more eligibility criteria to filter desired jobs for an applicant.
Upvotes: 2
Views: 1544
Reputation: 17500
This is a rather broad question, but let's see if I understood correctly. So you want to apply a set of criteria to two Lists right?
You can achieve this with a filter chain. Imagine that you have the following interface:
interface EligibilityCriterion {
fun apply(jobs: List<Job>, applicant: JobApplicant): List<Job>
}
Now you would need to implement whatever eligibility criteria you have, being each one implementation of the EligibilityCriterion
interface. The single method should return the Job
s to which the applicant is still eligible.
In another class, you would get all your implementations of EligibilityCriterion
as follows (I am assuming Spring for simplicity but you can get them any other way that applies to your case):
@Service
class JobsService(private val eligibilityCriteria: List<EligibilityCriterion>) {
fun findJobsForApplicant(jobs: List<Job>, applicant: JobApplicant): List<Job>{
eligibilityCriteria.fold(jobs) { jobsFold, eligibilityCriterion ->
eligibilityCriterion.apply(jobsFold, applicant)
}
}
}
The idea here is that you apply one EligibilityCriterion
at a time and the filtered List<Job>
is then passed to the following EligibilityCriterion
until all have been applied. In the end, you have all the Job
s to which the applicant
is eligible.
Given that you have a list of JobApplicant
s you would need to iterate over them and apply the criteria to each and every one of them.
You can read more about this in the following online resources:
P.S.: sorry for the Kotlin + Spring code, but I feel it is easier to explain and understand these things with code and not only words.
Upvotes: 1