Los 5 principios SOLID

Los principios SOLID explicados en detalle para el desarrollo de aplicaciones mantenibles y robustas

Image for post
Image for post
Photo by Dave Webb on Unsplash

Single Responsibility Principle

Image for post
Image for post
Photo by Stephanie Harvey on Unsplash
class Vehicle {
constructor(identifier: string){ }
getVehicleIdentifier() { }
saveVehicle(v: Vehicle) { }
getVehicle(identifier: string): Vehicle { }
}
class Vehicle {
constructor(identifier: string){ }
getVehicleIdentifier() { }
}
class VehicleDB {
saveVehicle(v: Vehicle) { }
getVehicle(identifier: string): Vehicle { }
}

Open-Closed Principle

Image for post
Image for post
Photo by Evan Wise on Unsplash
class Vehicle {
constructor(identifier: string){ }
getVehicleIdentifier() { }
}
const vehicles: Array<Vehicle> = [
new Vehicle('car'),
new Vehicle('bike')
];
function vehicleWheels(a: Array<Vehicle>) {
for(int i = 0; i <= a.length; i++) {
if(a[i].getVehicleIdentifier() == 'car')
log('4');
if(a[i].getVehicleIdentifier() == 'bike')
log('2');
}
}
vehicleWheels(vehicles);
const vehicles: Array<Vehicle> = [
new Vehicle('car'),
new Vehicle('bike'),
new Vehicle('quad')
];
function vehicleWheels(a: Array<Vehicle>) {
for(int i = 0; i <= a.length; i++) {
if(a[i].getVehicleIdentifier() == 'car')
log('4');
if(a[i].getVehicleIdentifier() == 'bike')
log('2');
if(a[i].getVehicleIdentifier() == 'quad')
log('4');
}
}
vehicleWheels(vehicles);
class Vehicle {
constructor(identifier: string){ }
getVehicleIdentifier() { }
abstract getWheels();
}
class Car extends Vehicle {
getWheels() { return 4; }
}
class Bike extends Vehicle {
getWheels() { return 2; }
}
class Quad extends Vehicle {
getWheels() { return 4; }
}
const vehicles: Array<Vehicle> = [
new Car(),
new Bike(),
new Quad()
];
function vehicleWheels(a: Array<Vehicle>) {
for(int i = 0; i <= a.length; i++) {
log(vehicle.getWheels();
}
}
vehicleWheels(vehicles);
class VehicleDBManager {
construct(mysqlConnection: MySQLConnection) {};
saveVehicle(v: Vehicle) {}
}
class VehicleDBManager {
construct(storageConnection: StorageInterface) {};
saveVehicle(v: Vehicle) {}
}

Liskov Substitution Principle

Image for post
Image for post
Photo by Pierre Bamin on Unsplash
// ..function maxVehicleSpeed(a: Array<Vehicle>) {
for(int i = 0; i <= a.length; i++) {
if(typeof a[i] === Car)
getCarSpeed(100);
if(typeof a[i] === Bike)
getBikeSpeed(20);
}
}
maxVehicleSpeed(vehicles);
function maxVehicleSpeed(a: Array<Vehicle>) {
for(int i = 0; i <= a.length; i++) {
a[i].getMaxSpeed();
}
}
maxVehicleSpeed(vehicles);
class Vehicle {
constructor(identifier: string){ }
getVehicleIdentifier() { }
abstract getWheels(); abstract getMaxSpeed();
}
class Car extends Vehicle {
getWheels() { return 4; }
getMaxSpeed() { return 100; }
}
class Bike extends Vehicle {
getWheels() { return 2; }
getMaxSpeed() { return 15; }
}

Interface Segregation Principle

Image for post
Image for post
Photo by Marvin Meyer on Unsplash
interface VehicleService {
repairWheels();
repairLights();
repairAirbags();
}
interface VehicleService {
repairWheels();
repairLights();
repairAirbags();
repairAC();
}
interface WheelsService {
repairWheels();
}
interface LightsService {
repairLights();
}
interface AirbagsService {
repairAirbags();
}

Dependency Inversion Principle

Image for post
Image for post
Photo by James Peacock on Unsplash
class VehicleFetcher {
constructor() {
$this->httpService = new HTTPService();
$this->serializer = new Serializer();
}
// rest of code
}
interface HttpServiceInterface {
function get();
function post();
// etc
}
class HttpService implements HttpServiceInterface {
// implementations
}
interface SerializerInterface {
function serialize();
function unserialize();
}
class Serializer implements SerializerInterface {
// implementations
}
class VehicleFetcher {
constructor(HttpServiceInterface $httpService, SerializerInterface $serializer) {
$this->httpClient = $httpService;
$this->serializer = $serializer;
}
// rest of code
}

Conclusión

¿Quieres recibir más artículos como este?

Written by

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store