Single Responsibility
Zasada mówi o tym, że każda klasa powinna być odpowiedzialna za jedną konkretną rzecz. Oznacza to, że klasa powinna mieć co najwyżej jeden powód do zmiany. Gdyby klasa miała dwa powody czy więcej powinniśmy podzielić funkcjonalność na mniejsze klasy. Jeśli to zrobimy to gdy zajdzie potrzeba zmiany to dokonamy jej tylko w jednej klasie, która daną funkcjonalność obsługuje.
Przykład złamania tej zasady:
package pl.agatarachanska.SingleResponsibility.Before;
public class Account {
private String name;
private String lastName;
private String city;
private String street;
private String number;
public Account(String name, String lastName, String city, String street, String number) {
this.name = name;
this.lastName = lastName;
this.city = city;
this.street = street;
this.number = number;
}
public String showInformation() {
return name + " " + lastName + ", adres: " + street + " " + number + ", " + city;
}
}
Jak widać w klasie mamy pola związane bezpośrednio z użytkownikiem, jak i z adresem oraz prostą metodę, która wyświetli nam informację o danym koncie. Zatem nasza klasa Account oprócz tworzenia konta zajmuje się też wyświetlaniem informacji o koncie. Wg zasady pojedynczej odpowiedzialności powinniśmy te klasę rozbić na mniejsze, które będą odpowiedzialne za jedną funkcjonalność.
Przykład zastosowania zasady:
Rozbiłam klasę Account na trzy mniejsze klasy: User, Addres oraz ShowInformation Zerknijmy do każdej z nich: Klasa User przechowuje nam informacje o imieniu, nazwisku oraz o adresie, ale tym razem wykorzystujemy do jego tworzenia klasę Addres. Wyglądają one następująco:
public class Address {
private String city;
private String street;
private String number;
public Address(String city, String street, String number) {
this.city = city;
this.street = street;
this.number = number;
}
public String getCity() {
return city;
}
public String getStreet() {
return street;
}
public String getNumber() {
return number;
}
}
public class User {
private String name;
private String lastName;
private Address address;
public User(String name, String lastName, Address address) {
this.name = name;
this.lastName = lastName;
this.address = address;
}
public String getName() {
return name;
}
public String getLastName() {
return lastName;
}
}
public class ShowInformation {
private Address address;
private User user;
public ShowInformation(Address address, User user) {
this.address = address;
this.user = user;
}
public void showInformations() {
System.out.println(getInformation());
}
private String getInformation() {
String info = user.getName() + " " + user.getLastName() + ", adres: "
+ address.getStreet() + " " + address.getNumber() + ", " + address.getCity();
return info;
}
}
A zatem każda klasa jest odpowiedzialna za jedną rzecz.
Zobaczmy jak wygląda main:
public class App {
public static void main(String[] args) {
Address address = new Address("Lublin", "X", "7/37");
User user = new User("Agata", "Rachańska", address);
ShowInformation showInformation = new ShowInformation(address, user);
showInformation.showInformations();
}
}