Está en la página 1de 13

SOLID

S Single responsibility

O Open closed

L Liskov’s substitution

I Interface segregation

D Dependency Inversion
Single
responsibility

Only one job to do


class Jedi {
public MidiLevel calculateM idiclorians () {...}
// Batch
public void save () {...}
// Database
public String describeTheJediO n Pdf () {...}
// Reporting
}
Open
closed

Open for extension but closed for modification


void check Out( Receipt receipt) {
Double total = 0;
// .... process things
Payment p = processCash ( total ); // Only cash
receipt. add Payment( p);
}
Open
closed

Payment p;
if ( credit)
p = acceptCredit( total );
else
p = acceptCash ( total );
receipt. add Payment( p);
Open
closed

public interface PaymentMethod {


void acceptPayment ( Double total );
}

void check Out( Receipt receipt , PaymentMethod pm ) {


Payment p = pm . acceptPayment ( total );
receipt. add Payment( p);
}
Liskov Substitution

Objects can be substituted by their subclasses

public class Rectangle {


private double height;
private double width ;

public void setHeight( double height );


public void setWidth ( double width );

}
Liskov Substitution

public class Square extends Rectangle {


public void setHeight ( double height) {
super. setHeight( height );
super. setWidth ( height );
}

public void setWidth ( double width ) {


setHeight( width );
}
}
Interface
Segregation

Don’t depend on things you won’t need

public interface Jedi {


train Kids ();
traing ToBecomeA Master ();
saveTatooinePlanet ();
plantATree ();
lead ARebellion AgainstThe Empire ();
}
Interface
Segregation

public interface MasterJedi { train


Kids (); saveTatooinePlanet ();
lead Rebellion AgainstTheEm pire ();
}
public interface Padawan Jedi {
traing ToBecomeA Master ();
}

public interface Gardener {


plantATree ();
}
Dependency
Inversion
High-level modules should not depend upon low-level
modules
public interface Reader { String getText (); } public
interface Writer { void putText( String c)}

class TheCopier {
void do Copy ( Reader reader , Writer writer) {
int c;
while (( c = reader. getText ()) != EOF ) {
writer. putText ();
}
}
}
public Keyboard implements Reader {...} // Webcam
public Printer implements Reader {...} // Monitor

También podría gustarte