Model products for filtering (hotels/flights/activities)
viaLeetCode
Problem Model three product types — Hotels, Flights, Activities — each with price, rating, and location, as classes usable by a single generic filter (e.g. "all products under $200 rated 4+").
Requirements
- One filtering pathway over heterogeneous products; type-specific attributes must remain accessible; adding a fourth product type shouldn't touch the filter.
Core design
- Abstract base class (or interface) Product{price, rating, location} with Hotel/Flight/Activity extending it (their extra fields stay on the subclass); ProductFilter operating on List<Product> with predicate composition — filter(list, Predicate<Product>) or a small spec/criteria object (PriceUnder, MinRating, InLocation, And/Or composites — specification pattern lite).
- This is a polymorphism litmus test: program to the abstraction, keep the filter closed to modification / open to new types and new criteria.
Discussion points
- Interface vs abstract class choice (shared state → abstract class here); composition of criteria vs a parameter-explosion filter method.
- Type-specific filtering (e.g. flights by airline) without instanceof chains — visitor or generics-bounded filters; where it's pragmatic to just downcast.
- Immutability of product objects, comparator-based sorting as the natural sibling feature.
asked …