在本文中,我们将介绍 go 中停车场系统的低级设计 (lld) 实现。我们将探索系统的不同方面,并了解每个组件如何与其余组件交互。此实现侧重于清晰度和现实世界的实用性,因此如果您想添加更多车辆类型、多种付款选项或现货预订等功能,您可以轻松扩展它。
该系统处理诸如管理停车楼层和停车位、停放车辆和处理付款等任务。我们还将确保它对于并发访问是线程安全的,因此如果我们需要将其扩展到更大的系统,它不会在压力下崩溃。
我们的设计包括六个主要组成部分:
我们的停车场使用单例模式。这意味着停车场只有一个实例,该实例创建一次并在整个应用程序中重复使用。这是使其正常工作的代码:
var ( parkinglotinstance *parkinglot once sync.once ) type parkinglot struct { name string floors []*parkingfloor } func getparkinglotinstance() *parkinglot { once.do(func() { parkinglotinstance = &parkinglot{} }) return parkinglotinstance }
使用sync.once,我们确保只创建一个实例,即使被多个 goroutine 访问也是如此。
停车场有多层,每层都有针对不同车辆类型(例如汽车、货车、卡车和摩托车)的指定停车位。要向停车场添加楼层,我们使用 addfloor 方法:
func (p *parkinglot) addfloor(floorid int) { p.floors = append(p.floors, newparkingfloor(floorid)) }
每个楼层都是使用 newparkingfloor 函数创建的,该函数按车辆类型组织停车位。
每个 parkingspot 都与特定的车辆类型(例如汽车或摩托车)相关联。这使得系统能够管理和限制每个停车位可以停放的车辆。这是 parkingspot 结构和 parkvehicle 方法:
type parkingspot struct { spotid int vehicletype vehicles.vehicletype currentvehicle *vehicles.vehicleinterface lock sync.mutex } func (p *parkingspot) parkvehicle(vehicle vehicles.vehicleinterface) error { p.lock.lock() defer p.lock.unlock() if vehicle.getvehicletype() != p.vehicletype { return fmt.errorf("vehicle type mismatch: expected %s, got %s", p.vehicletype, vehicle.getvehicletype()) } if p.currentvehicle != nil { return fmt.errorf("parking spot already occupied") } p.currentvehicle = &vehicle return nil }
我们使用互斥锁来确保同一时间只能停放一辆车。
每辆车都会收到一张票,上面有进入时间、离开时间、停车位和总费用。此票会在车辆退出时更新,并根据停车时间计算费用。
type parkingticket struct { entrytime time.time exittime time.time vehicle vehicles.vehicleinterface spot *parkingspot totalcharge float64 } func newparkingticket(vehicle vehicles.vehicleinterface, spot *parkingspot) *parkingticket { return &parkingticket{entrytime: time.now(), exittime: time.time{}, vehicle: vehicle, spot: spot, totalcharge: 0.00} }
calculatetotalcharge 方法根据车辆类型和持续时间计算停车费。
paymentsystem 类处理付款,根据是否支付所需金额更新付款状态:
type paymentsystem struct { status status amount float64 parkingticket *parkingticket } func (p *paymentsystem) processpayment() error { if p.parkingticket == nil { return fmt.errorf("payment failed: no parking ticket found") } if p.parkingticket.totalcharge < p.amount { p.status = paymentstatusfailed return fmt.errorf("payment failed: insufficient funds") } p.status = paymentstatuscompleted return nil }
processpayment 函数检查金额并将付款状态更新为“已完成”或“失败”。
我们的系统支持不同类型的车辆(汽车、货车、卡车和摩托车)。每种类型的每小时费用不同。这是通过在单独的车辆包中设置 vehicletype 和 vehicleinterface 来实现的:
package vehicles type VehicleType string const ( CarType VehicleType = "Car" VanType VehicleType = "Van" TruckType VehicleType = "Truck" MotorcycleType VehicleType = "Motorcycle" ) type VehicleInterface interface { GetLicenceNumber() string GetVehicleType() VehicleType GetVehicleCost() float64 }
我们可以通过调用newcar、newvan、newtruck等来创建新的车辆,它们都实现了vehicleinterface。
让我们看看各个部分如何在流程中组合在一起:
这个停车场系统是构建更复杂系统的简化起点。我们介绍了楼层和停车位管理、车辆停车和出库以及基本付款流程的基础知识。
有关完整代码实现,请检查以下存储库:
欢迎来到go 中的低级系统设计存储库!该存储库包含各种低级系统设计问题及其在 go 中实现的解决方案。主要目的是通过实际示例展示系统的设计和架构。
底层系统设计涉及理解系统架构的核心概念以及设计可扩展、可维护和高效的系统。该存储库将尝试涵盖使用 go 的各种问题和场景的解决方案。
此存储库中的第一个项目是停车场系统。该系统模拟一个可以停放车辆和出库车辆的停车场。它演示了: