Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- offset
- DB생성
- Postman
- NullPointerException
- intellij
- 이클립스
- appleM1
- MYSQL에러
- MySQL시작하기
- restful api
- 스프링에러
- spring
- pagination
- VUE
- 우분투에war배포
- windows10
- Java
- frontend
- SpringBoot
- String
- Lombok
- CloutNative
- wappalyzer
- Seek_Keyset
- springMVC
- MySQL
- SQL
- minikube
- K8S
- gradle
Archives
- Today
- Total
미운 오리 새끼의 우아한 개발자되기
[Java] 객체지향 5원칙 - Single Responsibility Principle 본문
객체지향 5원칙 (SOLID)
1. Single Responsibility Principle
2. Open /Closed Principle
3. Liskov Substitution Principle
4. Interface Segregation Principle
5. Dependency Inversion Principle
Single Responsibility Principle(SRP) states that a software component (in general, a class)
must have only one responsibility.
A class should have only one reason to change.
1. Before applying SRP
Product.java
public class Product {
String productName;
Integer productId;
public String getProductName(){
return productName;
}
public void setProductName(String productName){
this.productName = productName;
}
public Integer getProductId(){
return productId;
}
public void setProductId(Integer productId){
this.productId = productId;
}
public void searchProduct(Product product){
//search product logic goes here
}
}
2. After applying SRP
Product.java
public class Product {
String productName;
Integer productId;
public String getProductName(){
return productName;
}
public void setProductName(String productName){
this.productName = productName;
}
public Integer getProductId(){
return productId;
}
public void setProductId(Integer productId){
this.productId = productId;
}
}
ProductRepositoryView.java
public class ProductRepositoryView {
Product product;
public ProductRepositoryView(Product product){
this.product = product;
}
public void searchProduct(){
//logic to search product goes here
}
}
3. Benefits of SRP
1. Easier to understand
2. Easier to maintain
3. Fewer defects
4. Easier to test
'Java > Java 기본기' 카테고리의 다른 글
[Java] 진법 변환 (0) | 2022.02.23 |
---|---|
[자바의 정석] Instant, LocalDateTime과 ZonedDateTime 차이 (0) | 2022.02.20 |
[자바의 정석]MessageFormat (0) | 2022.02.20 |
[자바의 정석] ChoiceFormat (0) | 2022.02.20 |
[Java] String 사용할 때 주의점 (4) | 2020.09.19 |