SOLID Principle (Quick Read)
The famous design principle comprises 5 design strategies that a developer should follow for a successful application development
- Single Responsibility Principle
- Open/ Close Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion
Single Responsibility Principle(SRP)
No code unit(function/class/package) should have more than one responsibility.
Bad Ex:Class Bird{String result;;public fly(Bird bird){if(bird==piegeon)result="flys 20 meter hight";else if(bird==hen)result="flys 5 meter";elseresult="not measured yet";}}
This class should design such a way that it should work well for all the types of birds current design is hectic to maintain for the following reasons
- difficult to test.
- difficult in parallel programming
- understanding the code is difficult
solution: Break the code into multiple packages or classes
How to identify SRP is violated(Litmus test to SRP)?
- Multiple if-else ->huge chance to violate SRP principle
- Monster Method->Doing more than what it states to do( eg: save() method should do only data save, it should not do any connection factory instantiations or other data manipulations)
- Utility classes-> utility packages are strictly bad don't write junk utility. if need a date functionalities to create Date utility separately.
Open/ Close Principle
Whenever we have to add new features, our existing codebase should not require any modifications. Our code should be open for extensions mostly by adding new classes/methods. which means our code should be open for extensions and closed for modifications.
for example, in the above method, we want to add a new bird parrot then we need to add a new if-else block, to avoid that we can follow the below architecture by creating a Bird class separately and add new Bird varieties by extending the Base Bird class
Liskov Substitution Principle
The object of parent class is completely replaceable by any object of the child class.
For Eg:
class Bird{fly(){}makeSound(){}}class Peacock extends Bird{fly(){}makeSound(){}}class Hen extends Peacock{fly(){}makeSound(){}}
now create an Object like below
obj.fly();obj.makeSound();Negative scenarioclass Ostrich extends Bird{fly(){throws new CantFlyException(); // This is against Liskov's principle which gives different implementaion for subclass}makeSound(){}}
But Liskov substitution fails in 2 places
- When a subclass throws an exception.(for this we need a separate try-catch in the calling code like try{ obj.fly()} catch(Exception ex){}
- when subclass gives a different meaning to the method of the parent class
Interface Segregation Principle
Interfaces should be designed as lite as possible, Better to focus on a single job. For eg: in java Runnable interface has a single run method. (Like Python Lambda & Java Lambda)
Dependency Inversion
Any 2 concrete class should depend on each other via a layer of abstractions.
For eg: PayTm have two payment agent like RazorPay & JustPay the implemetations can be done using one PaymentLayerAbstractions.
Interface PaymentInterface{void pay(){}}class RazorPay implements PaymentInterface{void pay(){System.out.println("Payed using Razor pay");}}class JustPay implements PaymentInterface{void pay(){System.out.println("Payed using JustPay ");}}class PayTm{PaymentInterface pay;PaymentInterface(PaymentInterface pay){this.pay=pay}public static void main(String args[]){PaymentInterface payment= newJustPay(); // can substitute easily with RazorPay no further cahnges requiredpayment.pay();}}
We injected the dependency using constructor like below
PaymentInterface(PaymentInterface pay){this.pay=pay}
Spring Boot helps to inject using Annotation based injections also.
Happy reading π
In fact, some of the the} finest casinos are recognized for offering all types of roulette versions. Even although they have unique features, they have the same premise. The second casino sport with the very best probability of successful is Craps. But if you want to|if λ¨Ήνμ¬μ΄νΈ λ¨Ήννλ μ¦ you would like to} take massive gamble|a chance} with Craps in Europe, it is best to go surfing.
ReplyDeleteWith this new cooperation, Swintt goals to ascertain itself as a number one} provider of https://choegomachine.com/ on-line gaming products in Sweden by inserting its video games in entrance of a wide new viewers. The partnership between Glitnor and Swintt promises to be long and worthwhile. James Warner is a content supervisor and editor of onlinecasinohex.ph with 10 years of experience. Thanks to my experience within the gambling business, I can share my data with my readers. From me, have the ability to|you probably can} all the time find reliable and in-depth casino reviews.
ReplyDelete