Applying Abstract Factory to a Vehicle Parking System
Consider a parking lot system that supports different types of parking facilities: a standard parking lot and a valet parking service. Each facility type has its own ticket generation and fee calculation strategy, and these components must work together consistently.
Domain Requirements
First, standard parking uses printed tickets and calculates fees based on hourly rates. Second, valet parking uses digital tokens and includes additional service charges. Third, the system must support adding new parking facility types (like airport parking) without modifying existing client code.
Product Families
Factory Implementation
Client Usage
The ParkingLot class works with any factory type. To switch from standard to valet parking, simply pass ValetParkingFactory instead of StandardParkingFactory during initialization. The client code remains unchanged because it depends only on abstract interfaces.
PrintedTicket and HourlyFeeCalculator) are designed to work together, while mixing products from different families would create inconsistencies.Extensibility
To add airport parking with monthly passes and flat-rate fees, create AirportParkingFactory, MonthlyPass, and FlatRateFeeCalculator. Existing code requires no modification. However, if you need to add a new product type (like ParkingReceipt), you must modify the abstract factory interface and all concrete factories, which is a known limitation of this pattern.