jeudi 8 février 2018

Design pattern to extract information from a complex object

Suppose a class Company that includes a list of Departments. Each department has a list of employees.

Now I want to extract various information from a Company object. For example, I want to extract all of the Male employees. Or extract all of the departments in Canada. Whatever new need that comes up, I want to create a new object to extract this information without having to modify my business object (Company, Department, Employee). I want to respect the open/closed principle.

What would be the best design pattern in this case?

I looked at the visitor pattern. Here is what I found.

1- Visit method returns void. That means my visitor will need to keep all extracted object internally. This means I cannot use the visitor as a singleton.

2- Each visitor needs to know how to loop through the Departments/Employees. It leads to a good amount of duplicated code. I could move that code into an abstract class. I feel the abstract might grow ugly and big.

Aucun commentaire:

Enregistrer un commentaire