Skip to the content.

#SOLID Design Principles

-

What we’ll cover

-

What we’ll cover (continued)

- -

Design Principles

-

DRY Principle

-

WET Principle

-

Rule of Three

-

SOLID Principle

-

Single Responsibility Principle (SRP)

-

Single Responsibility Principle (SRP)
Example

-

Open/closed principle (OCP)

-

Open/closed principle (OCP)
Example

-

Liskov substitution principle (LSP)

-

Liskov substitution principle (LSP)
Example (Coupled)

public class Classroom {
    private ArrayList<Student> students;
    public Classroom(ArrayList<Student> student) {
        this.students = students;
    }
}
public void demo() {
	ArrayList<Student> students = new ArrayList<>();
	Classroom classroom = new Classroom(students);
}

-

Liskov substitution principle (LSP)
Example (Decoupled)

public class Classroom {
    private List<Student> students;
    public Classroom(List<Student> student) {
        this.students = students;
    }
}
public void demo() {
	List<Student> studentArrayList = new ArrayList<>();
	List<Student> studentLinkedList = new LinkedList<>();

	Classroom classroom1 = new Classroom(studentArrayList);
	Classroom classroom2 = new Classroom(studentLinkedList);
}

-

Interface segregation principle (ISP)

-

Interface segregation principle (ISP)
Example Part 1

public class Person {
	private Long id;
	private String serial;
	public Person(Long id, String serial) {
		this.id = id;
		this.serial = serial;
	}

	public Long identify() { return id; }
	public String serialize() { return serial; }
}

-

Interface segregation principle (ISP)
Example Part 2

public interface Identifiable { Long identify(); }
public interface Serializable { String serialize(); }

-

Interface segregation principle (ISP)
Example Part 3

public class Person implements Identifiable {
	private Long id;
	public Person(Long id) {
			this.id = id;
	}

	public Long identify() { return id; }
}

-

Interface segregation principle (ISP)
Example Part 4

public class Person implements Serializable {
	private String serial;
	public Person(String serial) {
			this.serial = serial;
	}

	public String serialize() { return serial; }
}

-

Interface segregation principle (ISP)
Example Part 5

public class IdentifiablePerson implements Identifiable, Serializable {
	private Long id;
	public IdentifiablePerson(Long id, String serial) {
			this.id = id;
			this.serial = serial;
	}

	public Long identify() { return id; }
	public String serialize() { return serial; }
}

-

Interface segregation principle (ISP)
Example Part 6

public void demo() {
	IdentifiablePerson person = new IdentifiablePerson(0L, "Blah Blah Serial");
	Identifiable identifiable = person;
	Serializable serializable = person;
}

-

Dependency inversion principle (DIP)