Posts

Showing posts with the label SOLID

01. What is SOLID?

Image
01. What is SOLID? What is SOLID? SOLID  is a set of   five design principles  in object-oriented programming that make your code cleaner, modular, flexible, and maintainable S – Single Responsibility Principle (SRP) O – Open/Closed Principle (OCP) L – Liskov Substitution Principle (LSP) I – Interface Segregation Principle (ISP) D – Dependency Inversion Principle (DIP) 1. Single Responsibility Principle (SRP)  A class, ethos or veritable should responsible to handle only one thing (have only one reason to change). Bad: class Report { void generateReport () {} void saveToFile () {} } Good: class ReportGenerator { void generate () {} } class ReportSaver { void saveToFile () {} } 2. Open/Closed Principle (OCP) Software entities should be open for extension but closed for modification .  Bad: class Payment { void process (String type) { if (type.equals( "credit" )) {} else if (type.equals( "paypal" )) {} } } Good: interface PaymentMethod { ...

02. Interface Segregation

Image
  EXAMPLE 01 Issue: interface Vehicle { void dive () ; void fly () ; } class Car implements Vehicle { @Override public void dive () {System. out .println( "InterfaceSegregation.Car dive" ) ; } @Override public void fly () { //This not needed to a bike } } Fixing That issue: interface DrivingVehicle { void dive () ; } interface FlyingVehicle { void fly () ; } class Car implements DrivingVehicle { @Override public void dive () {System. out .println( "InterfaceSegregation.Car dive" ) ; } } class Plane implements DrivingVehicle , FlyingVehicle { @Override public void dive () {System. out .println( "InterfaceSegregation.Plane dive" ) ; } @Override public void fly () {System. out .println( "InterfaceSegregation.Plane fly" ) ; } } EXAMPLE 02 Issue: class PetrolEngine{} class Vehicle{ private PetrolEngine engine ; Vehicle (PetrolEngine engine){ this . engine =engine ; } } Fixing That issue: interface Engine{}...