2. Agenda
● S — Single Responsibility Principle(S.R.P)
● O — Open-Closed Principle
● L — Liskov Substitution Principle
● I — Interface Segregation Principle
● D — Dependency Inversion Principle
3. Single Responsibility
A class should have one, and only one, reason to change.
Don’t be Swiss Army Knife!
In the context of the Single Responsibility Principle (SRP) we define a
responsibility as “a reason for change”. If you can think of more than one
motive for changing a class, then that class has more than one responsibility.
7. @Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Movie movie = movies.get(position);
holder.itemView.setTag(movie);
holder.title.setText(movie.getTitle());
holder.rating.setText(movie.getRating());
holder.genre.setText(
ArraysUtil.convertArrayListToString(movie.getGenre()));
holder.releaseYear.setText(movie.getYear());
Glide.with(holder.thumbNail.getContext())
.load(movies.get(position)
.getThumbnailUrl())
.into(holder.thumbNail);
}
11. @Test
public void testScenarioOne() {
//...
assertEqual(resultOne, mUnderTest.getScenarioOne());
}
@Test
public void testScenarioTwo() {
//...
assertEqual(resultTwo, mUnderTest.getScenarioTwo());
}
12. Open/Closed Principle
You should be able to extend a classes behavior, without modifying it.
This principle is the foundation for building code that is maintainable and
reusable.
Robert C. Martin
16. public interface Media {
String getPath();
}
public class Music implements Media{
private String title;
private String uri;
// getters/setters ...
@Override
public getPath() {
return //...;
}
}
// Image.java
public class Image implements Media{
private String title;
// getters/setters ...
@Override
public getPath() {
return //...;
}
}
17. public class DataFactory {
public ArrayList<String> fetchUri(ArrayList<Media>... medias) {
ArrayList<String> uris = new ArrayList<String>();
for (Media media : medias) {
uris.add(shape.getArea());
}
return area;
}
}
18. The Liskov Substitution Principle (LSP)
Child classes should never break the parent class’ type definitions.
27. Button valid = (Button)findViewById(R.id.valid);
valid.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
// TODO: do some stuff...
}
public void onLongClick(View v) {
// we don't need to it
}
public void onTouch(View v, MotionEvent event) {
// we don't need to it
}
});
29. public interface OnClickListener {
void onClick(View v);
}
public interface OnLongClickListener {
void onLongClick(View v);
}
public interface OnTouchListener {
void onTouch(View v, MotionEvent event);
}
30. The Dependency Inversion Principle
1. High-level modules should not depend on low-level modules. Both should
depend on abstractions.
2. Abstractions should not depend upon details. Details should depend upon
abstractions.
Robert C. Martin
33. class Program {
public void work() {
// ....code
}
}
class Engineer{
Program program;
public void setProgram(Program p) {
program = p;
}
public void manage() {
program.work();
}
}