λ°μν
SOLID, μ’μ κ°μ²΄ μ§ν₯ μ€κ³μ 5κ°μ§ μμΉ
ν΄λ¦°μ½λλ‘ μ λͺ ν λ‘λ²νΈ λ§ν΄μ΄ μ’μ κ°μ²΄ μ§ν₯ μ€κ³μ 5κ°μ§ μμΉμ μ 리
Martin and Feathers' design principles encourage us to create more maintainable, understandable, and flexible software.
Martinκ³Ό Featherμ SOLID μμΉμ μ’ λ μ μ§ κ΄λ¦¬νκΈ° μ½κ³ μ΄ν΄νκΈ° μ¬μ°λ©° μ μ°ν μννΈμ¨μ΄λ₯Ό λ§λ€λλ‘ λμμ€λ€.
λ¨μΌ μ± μ μμΉ
SRP, Single responsibility principle
- ν ν΄λμ€λ νλμ μ± μλ§ κ°μ ΈμΌ νλ€.
- νλμ μ± μμ΄λΌλ κ²μ λͺ¨νΈνλ€. μ€μν κΈ°μ€μ λ³κ²½μ΄ μμ λ νκΈ ν¨κ³Όκ° μ μΌλ©΄ λ¨μΌ μ± μ μμΉμ μ λ°λ₯Έ κ²μ΄λ€.
- μλ₯Όλ€μ΄, ν΄λΌμ΄μΈνΈ κ°μ²΄κ° μ§μ ꡬν κ°μ²΄λ₯Ό μμ±νκ³ , μ°κ²°νκ³ , μ€ννλ λ€μν μ±
μμ κ°μ§λ€λ©΄
- ꡬν κ°μ²΄λ₯Ό μμ±νκ³ μ°κ²°νλ μ± μμ κ°μ§ ν΄λμ€(ex:AppConfig), μ€ννλ μ± μλ§ λ΄λΉνλ ν΄λμ€λ‘ λλλ€.
- μ₯μ
- Testing – μ± μμ΄ νλμΈ ν΄λμ€λ λ μ μ ν μ€νΈ μΌμ΄μ€λ₯Ό κ°μ§λ€.
- Lower coupling – λ¨μΌ ν΄λμ€μ κΈ°λ₯μ΄ μ μ μλ‘, μ’ μμ±μ΄ μ€μ΄λ λ€.
- Organization – κ²μνκΈ° μ½λ€.
public class Book {
private String name;
private String author;
private String text;
//constructor, getters and setters
// methods that directly relate to the book properties
public String replaceWordInText(String word){
return text.replaceAll(word, text);
}
public boolean isWordInText(String word){
return text.contains(word);
}
// **μ΄ λ©μλλ ν
μ€νΈ μΈμλ§ μ²λ¦¬νλ λ³λμ ν΄λμ€λ₯Ό ꡬνν΄μ λΆλ¦¬ν΄μΌ νλ€. λ¨μΌ μ±
μ μμΉ μλ°!**
void printTextToConsole(){
// our code for formatting and printing the text
}
}
public class BookPrinter {
// methods for outputting text
// μΈμνλ κΈ°λ₯ λΆλ¦¬
void printTextToConsole(String text){
//our code for formatting and printing the text
}
void printTextToAnotherMedium(String text){
// code for writing to any other location..
}
}
κ°λ°©-νμ μμΉ
OCP, Open/closed principle
- Open for Extension, Closed for Modification
- μννΈμ¨μ΄ μμλ νμ₯μλ μ΄λ € μμΌλ λ³κ²½μλ λ«ν μμ΄μΌ νλ€.
- λ€νμ±μ νμ©ν΄μ μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€λ₯Ό νλ λ§λ€μ΄μ μλ‘μ΄ κΈ°λ₯μ ꡬννλ μ (μν κ³Ό ꡬνμ λΆλ¦¬)
- λ¬Έμ μ
- ν΄λΌμ΄μΈνΈκ° ꡬν ν΄λμ€λ₯Ό λ³κ²½ν λ μ§μ μ ννλ©΄μ λ³κ²½νλ€λ©΄ κ·Έ κ³Όμ μμ ν΄λΌμ΄μΈνΈ μ½λλ₯Ό λ³κ²½ν΄μΌ νλ€.
- λ€νμ±μ μ¬μ©νμ§λ§ OCP μμΉμ μ§ν¬ μ μλ€. => κ°μ²΄λ₯Ό μμ±νκ³ μ°κ΄κ΄κ³λ₯Ό λ§Ίμ΄μ£Όλ λ³λμ 쑰립/μ€μ μ νμ
- λ¬Έμ μ
public class Guitar {
private String make;
private String model;
private int volume;
//Constructors, getters & setters
}
// OCPλ₯Ό μ€μνλ©° κΈ°λ₯ νμ₯.
public class SuperCoolGuitarWithFlames extends Guitar {
private String flameColor;
//constructor, getters + setters
}
리μ€μ½ν μΉν μμΉ
LSP, Liskov substitution principle
- νλ‘κ·Έλ¨μ κ°μ²΄λ νλ‘κ·Έλ¨μ μ νμ±μ κΉ¨λ¨λ¦¬μ§ μμΌλ©΄μ νμ νμ μ μΈμ€ν΄μ€λ‘ λ°κΏ μ μμ΄μΌ νλ€.
- λ¨μν μ»΄νμΌμ μ±κ³΅νλ κ²μ λμ΄μμ νμ ν΄λμ€λ μΈν°νμ΄μ€ κ·μ½μ λͺ¨λ μ§μΌμΌ νλ€λ λ»(μλμ°¨ μΈν°νμ΄μ€μ μμ μ μμΌλ‘ κ°λΌλ κΈ°λ₯μ ꡬνν΄μΌ ν¨. λ€λ‘ κ°κ² ꡬννλ©΄ μλ°)
μΈν°νμ΄μ€ λΆλ¦¬ μμΉ
ISP, Interface segregation principle
- νΉμ ν΄λΌμ΄μΈνΈλ₯Ό μν μΈν°νμ΄μ€ μ¬λ¬ κ°κ° λ²μ© μΈν°ν μ΄μ€ νλλ³΄λ€ λ«λ€
- μΈν°νμ΄μ€κ° λͺ νν΄μ§κ³ λ체 κ°λ₯μ±μ΄ λμμ§λ€.
public interface BearKeeper {
void washTheBear();
void feedTheBear();
void petTheBear();
}
// μΈν°νμ΄μ€ λΆλ¦¬
public interface BearCleaner {
void washTheBear();
}
public interface BearFeeder {
void feedTheBear();
}
public interface BearPetter {
void petTheBear();
}
// μνλ κΈ°λ₯λ§ κ³¨λΌμ κ°λ° κ°λ₯
public class BearCarer implements BearCleaner, BearFeeder {
public void washTheBear() {
//I think we missed a spot...
}
public void feedTheBear() {
//Tuna Tuesdays...
}
}
μμ‘΄κ΄κ³ μμ μμΉ
DIP, Dependency inversion principle
- νλ‘κ·Έλλ¨Έλ μΆμνμ μμ‘΄ν΄μΌμ§, ꡬ체νμ μμ‘΄νλ©΄ μλλ€.
- ꡬν ν΄λμ€μ μμ‘΄νμ§ λ§κ³ μΈν°νμ΄μ€μ μμ‘΄νλΌλ λ»
MemberRepository m = new MemoryMemberRepository()
λ μΈν°νμ΄μ€μ μμ‘΄νμ§λ§ ꡬν ν΄λμ€μλ λμμ μμ‘΄νλ νν. DIP μλ° μ¬λ‘μ΄λ€
public class Windows98Machine {
private final StandardKeyboard keyboard;
private final Monitor monitor;
public Windows98Machine() { // μΈν°νμ΄μ€κ° ꡬν ν΄λμ€μ μμ‘΄νκ³ μλ€.
monitor = new Monitor();
keyboard = new StandardKeyboard();
}
}
public class Windows98Machine{
private final Keyboard keyboard;
private final Monitor monitor;
public Windows98Machine(Keyboard keyboard, Monitor monitor) { // μμ‘΄μ± μ£Όμ
μ¬μ©
this.keyboard = keyboard;
this.monitor = monitor;
}
}
π Reference
λ°μν