solid 是计算机编程中五个良好原则(规则)的缩写。 solid 允许程序员编写更易于理解和稍后更改的代码。 solid 通常与使用面向对象设计的系统一起使用。
让我们使用车辆示例来解释 solid 原理。想象一下,我们正在设计一个系统来管理不同类型的车辆,例如汽车和电动汽车,以提供运输服务。
车辆示例:想象你有一辆汽车。它负责驾驶,但不应该负责处理自己的维护(例如换油或轮胎旋转)。相反,由一名单独的机械师负责。
说明:在我们的代码中,vehicle 类应该只处理与车辆本身相关的事情,比如存储其品牌和型号。如果我们需要管理维护,我们会为此创建一个单独的维护类。这样,每个类都有一个工作或职责,使代码更易于管理。
class vehicle def initialize(make, model) @make = make @model = model end end class maintenance def initialize(vehicle) @vehicle = vehicle end def perform_maintenance puts "performing maintenance on #{@vehicle.make} #{@vehicle.model}" end end
车辆示例:假设您有一辆基本型汽车,现在您想在系统中添加一辆电动汽车。您不必修改现有的汽车类别即可添加电动汽车的功能。相反,您可以通过创建新的电动汽车类来扩展现有功能。
说明:vehicle类是开放扩展的(你可以创建新类型的车辆,如electricvehicle),但它是封闭修改的(你不需要更改vehicle类本身来添加新类型)。
class vehicle def initialize(make, model) @make = make @model = model end def description "#{@make} #{@model}" end end class electricvehicle < vehicle def initialize(make, model, battery_range) super(make, model) @battery_range = battery_range end def description "#{super} with #{@battery_range} miles battery range" end end
车辆示例:假设您有一支车队,并且您可以毫无问题地用电动汽车替换任何普通汽车。两者都应该能够在不破坏系统的情况下执行其基本功能 - 驾驶 - 。
说明:任何子类(如 electricvehicle)都应该能够替换其父类(vehicle),而不改变程序的行为。这确保我们的代码可以以相同的方式处理不同类型的车辆。
class vehicle def initialize(make, model) @make = make @model = model end def drive puts "driving the #{@make} #{@model}" end end class electricvehicle < vehicle def drive puts "driving the electric #{@make} #{@model} quietly" end end def test_drive(vehicle) vehicle.drive end car = vehicle.new("toyota", "corolla") ev = electricvehicle.new("tesla", "model 3") test_drive(car) # driving the toyota corolla test_drive(ev) # driving the electric tesla model 3 quietly
车辆示例:想象一下您有不同类型的车辆:有些可以充电(如电动汽车),有些只能驾驶(如汽油车)。你不希望汽油车必须处理充电相关的方法。
说明:类应该只实现它们需要的接口(或行为)。例如,电动车辆可能同时实现可驾驶和可充电接口,而普通车辆仅实现可驾驶。
module drivable def drive raise notimplementederror, "this #{self.class} cannot drive" end end module chargeable def charge raise notimplementederror, "this #{self.class} cannot be charged" end end class vehicle include drivable def initialize(make, model) @make = make @model = model end def drive puts "driving the #{@make} #{@model}" end end class electricvehicle < vehicle include chargeable def initialize(make, model, battery_range) super(make, model) @battery_range = battery_range end def drive puts "driving the electric #{@make} #{@model} quietly" end def charge puts "charging the #{@make} #{@model}" end end
车辆示例:想象一辆汽车可以有不同类型的发动机:燃气发动机或电动发动机。汽车不应该直接依赖于特定的引擎类型,而应该依赖于更通用的引擎接口,这样它就可以使用任何类型的引擎。
说明:高级模块(如车辆)不应依赖于低级模块(如 gasengine 或 electricengine)。两者都应该依赖于抽象(如引擎接口)。这使得系统更加灵活并且更容易更改。
class Engine def start raise NotImplementedError, "This #{self.class} cannot start" end end class GasEngine < Engine def start puts "Starting gas engine" end end class ElectricEngine < Engine def start puts "Starting electric engine" end end class Vehicle def initialize(engine) @engine = engine end def start @engine.start end end gas_engine = GasEngine.new electric_engine = ElectricEngine.new gas_car = Vehicle.new(gas_engine) electric_car = Vehicle.new(electric_engine) gas_car.start # Starting gas engine electric_car.start # Starting electric engine
通过遵循此车辆示例中的 solid 原则,我们可以构建一个易于维护、扩展和适应新需求的系统。
领英:https://www.linkedin.com/in/anandsoni11/
以上就是SOLID 原则使用一些有趣的类比与车辆示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号