Where should I put contextual data related to an object that is not actually a property of the object?

I have a Car class. It has three properties: id, color, and model.

In a specific query, I want to return all the cars and their properties, and also want to return a true / false field called "searcherKnowsOwner", which is the field that I calculate in the query of my database, based on whether the owner knows the search. I have a database function that takes a crawler id and a car id and returns a boolean.

My car class looks like this (pseudo code):

class Car{
  int id;
  Color color;
  Model model;
}

I have a screen where I want to display all the cars, but I also want to display a flag next to each car if the person viewing the page knows the owner of this car.

Should I add a field to the Car class, a boolean searcherKnowsOwner? This is not a property of the car, but is actually the property of the user conducting the search. But this seems to be the most effective place to post this information.

+3
source share
2 answers

Let the Car and Owner object remain unchanged. Define 2 classes SearchCriteria and SearchResults

SerachCriteria will conduct the search criteria provided by the end user, and your application logic will return an object of type SearchResults, which is a collection of Cars.

You can save the flag in SearchResult corresponding to each object of the car as a result, if the owner is known.

+3
source

I would simulate it like this:

class Car{
  int id;
  Color color;
  Model model;
  Owner owner;
}

class Owner {
  Boolean knowsSearcher;
}

Car Owner ( , ), Owner , , .

+1

Source: https://habr.com/ru/post/1743053/


All Articles