Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
SOLID
Design Principles
By Divanshu Nandwani
Agenda
History Definition
Examples Q&A
History
● 80% of software projects fails
● The theory of SOLID principles was
introduced by Robert C. Martin in his 2000
paper “Design Principles and Design
Patterns”
● The SOLID acronym itself was introduced
later by Michael Feathers
Definition
● Single responsibility principle
A class should have only a single responsibility (i.e. changes to only one part of the software's
specification should be able to affect the specification of the class).
● Open/closed principle
"software entities … should be open for extension, but closed for modification."
● Liskov substitution principle
"objects in a program should be replaceable with instances of their subtypes without altering the
correctness of that program."
● Interface segregation principle
"many client-specific interfaces are better than one general-purpose interface.”
● Dependency inversion principle
one should "depend upon abstractions, [not] concretions."
Example of Single responsibility principle
Without “S”
class UserService {
void changePassword(User user) {
if(checkAccess(user)) {
//Grant option to change
}
}
boolean checkAccess(User user) {
//Verify if the user is valid.
}
}
With “S”
class UserService {
void changePassword(User user) {
if(SecurityService.checkAccess(user)) {
//Grant option to change
}
}
}
class SecurityService {
static boolean checkAccess(User user) {
//check the access.
}
}
Example of Open/closed principle
Without “O”
class LoanApprovalHandler {
void approve(PersonalLoanValidator
validator) {
if ( validator.isValid()) {
//Process the loan.
}
}
}
class PersonalLoanValidator {
boolean isValid() {
//Validation logic
}
}
With “O”
interface Validator {boolean isValid();}
class PersonalLoanValidator implements
Validator {
boolean isValid() {}
}
class HomeLoanValidator implements Validator {
boolean isValid() {}
}
class LoanApprovalHandler {
void approve(Validator validator) {
if ( validator.isValid()) {
//Process the loan.
}
}
}
Example of Liskov substitution principle
Without “L”
class Bird {
public void fly(){}
public void eat(){}
}
class Sparrow extends Bird {}
class Ostrich extends Bird{
fly(){
throw new
UnsupportedOperationException();
}
}
With “L”
class Bird {
void eat(){}
}
class FlightBird extends Bird {
void fly(){}
}
class NonFlightBird extends Bird {}
class Sparrow extends FlightBird {}
class Ostrich extends NonFlightBird {}
Example of Interface segregation principle
Without “I”
interface IUser{
changePassword();
checkUserRole();
assignRole();
}
With “I”
interface IUser{
changePassword();
}
interface IRole{
assignRole();
}
interface IUserRole{
checkUserRole();
}
Example of Dependency inversion principle
Without “D”
Class Payments {
CreditCardPaymentMethod ccpm = new
CreditCardPaymentMethod();
void makePayments(){
ccpm.transact();
}
With “D”
Class Payments {
PaymentMethod pm;
Payments(PaymentMethod pm_provided) {
pm = pm_provided;
}
void makePayments(){
pm.transact();
}
Q & A

More Related Content

Solid Principles

  • 3. History ● 80% of software projects fails ● The theory of SOLID principles was introduced by Robert C. Martin in his 2000 paper “Design Principles and Design Patterns” ● The SOLID acronym itself was introduced later by Michael Feathers
  • 4. Definition ● Single responsibility principle A class should have only a single responsibility (i.e. changes to only one part of the software's specification should be able to affect the specification of the class). ● Open/closed principle "software entities … should be open for extension, but closed for modification." ● Liskov substitution principle "objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program." ● Interface segregation principle "many client-specific interfaces are better than one general-purpose interface.” ● Dependency inversion principle one should "depend upon abstractions, [not] concretions."
  • 5. Example of Single responsibility principle Without “S” class UserService { void changePassword(User user) { if(checkAccess(user)) { //Grant option to change } } boolean checkAccess(User user) { //Verify if the user is valid. } } With “S” class UserService { void changePassword(User user) { if(SecurityService.checkAccess(user)) { //Grant option to change } } } class SecurityService { static boolean checkAccess(User user) { //check the access. } }
  • 6. Example of Open/closed principle Without “O” class LoanApprovalHandler { void approve(PersonalLoanValidator validator) { if ( validator.isValid()) { //Process the loan. } } } class PersonalLoanValidator { boolean isValid() { //Validation logic } } With “O” interface Validator {boolean isValid();} class PersonalLoanValidator implements Validator { boolean isValid() {} } class HomeLoanValidator implements Validator { boolean isValid() {} } class LoanApprovalHandler { void approve(Validator validator) { if ( validator.isValid()) { //Process the loan. } } }
  • 7. Example of Liskov substitution principle Without “L” class Bird { public void fly(){} public void eat(){} } class Sparrow extends Bird {} class Ostrich extends Bird{ fly(){ throw new UnsupportedOperationException(); } } With “L” class Bird { void eat(){} } class FlightBird extends Bird { void fly(){} } class NonFlightBird extends Bird {} class Sparrow extends FlightBird {} class Ostrich extends NonFlightBird {}
  • 8. Example of Interface segregation principle Without “I” interface IUser{ changePassword(); checkUserRole(); assignRole(); } With “I” interface IUser{ changePassword(); } interface IRole{ assignRole(); } interface IUserRole{ checkUserRole(); }
  • 9. Example of Dependency inversion principle Without “D” Class Payments { CreditCardPaymentMethod ccpm = new CreditCardPaymentMethod(); void makePayments(){ ccpm.transact(); } With “D” Class Payments { PaymentMethod pm; Payments(PaymentMethod pm_provided) { pm = pm_provided; } void makePayments(){ pm.transact(); }
  • 10. Q & A