π― SOLID μμΉ νλ°© μ 리 + μ€λ¬΄ μ μ© λ° λ¦¬ν©ν λ§ μμ
π― SOLID μμΉ νλ°© μ 리 + μ€λ¬΄ μ μ© λ° λ¦¬ν©ν λ§ μμ
π SOLIDλ?
κ°μ²΄μ§ν₯ μ€κ³μ 5λ μμΉ
μ μ§λ³΄μμ±, νμ₯μ±, μ μ°μ±μ κΈ°λ°μ΄ λλ μ€κ³ μμΉμ λλ€.
Solid μμΉμ μλμ²λΌ ꡬμ±λ©λλ€:
- Single Responsibility Principle
- Open-Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
1οΈβ£ SRP - λ¨μΌ μ± μ μμΉ
ν΄λμ€λ λ¨ νλμ μ± μλ§ κ°μ ΈμΌ νλ€
π§ 리ν©ν λ§ μ
class UserService {
public void registerUser() {
// νμκ°μ
λ‘μ§
}
public void sendWelcomeEmail() {
// μ΄λ©μΌ μ μ‘ λ‘μ§
}
}
β‘ νμ κ°μ κ³Ό μ΄λ©μΌ μ μ‘μ΄ λμμ μ± μμ§κ³ μμ
β 리ν©ν λ§ ν
class UserService {
private EmailService emailService;
public void registerUser() {
// νμκ°μ
λ‘μ§
emailService.sendWelcomeEmail();
}
}
class EmailService {
public void sendWelcomeEmail() {
// μ΄λ©μΌ μ μ‘
}
}
β‘ κ°κ°μ μ± μμ λΆλ¦¬ν¨μΌλ‘μ¨ λ³κ²½ ν¬μΈνΈκ° μ€μ΄λ¦
2οΈβ£ OCP - κ°λ°© νμ μμΉ
νμ₯μλ μ΄λ € μκ³ , λ³κ²½μλ λ«ν μμ΄μΌ νλ€
π§ 리ν©ν λ§ μ
class DiscountService {
public int calculateDiscount(String grade, int price) {
if (grade.equals("VIP")) return price - 1000;
else return price - 500;
}
}
β‘ λ±κΈμ΄ μΆκ°λ λλ§λ€ if-else
μμ νμ
β 리ν©ν λ§ ν
interface DiscountPolicy {
int discount(int price);
}
class VIPDiscount implements DiscountPolicy {
public int discount(int price) {
return price - 1000;
}
}
class BasicDiscount implements DiscountPolicy {
public int discount(int price) {
return price - 500;
}
}
class DiscountService {
public int calculate(DiscountPolicy policy, int price) {
return policy.discount(price);
}
}
β‘ μλ‘μ΄ μ μ± μΆκ° μ ν΄λμ€λ§ μΆκ°νλ©΄ λ¨
3οΈβ£ LSP - 리μ€μ½ν μΉν μμΉ
μμ ν΄λμ€λ μΈμ λ λΆλͺ¨ ν΄λμ€λ‘ λ체 κ°λ₯ν΄μΌ νλ€
π§ 리ν©ν λ§ μ
class Rectangle {
protected int width, height;
public void setWidth(int w) { width = w; }
public void setHeight(int h) { height = h; }
public int getArea() { return width * height; }
}
class Square extends Rectangle {
@Override
public void setWidth(int w) {
width = height = w;
}
@Override
public void setHeight(int h) {
width = height = h;
}
}
β‘ Square
λ Rectangle
μ κΈ°λ λμμ κΉ¨νΈλ¦Ό
β 리ν©ν λ§ ν
interface Shape {
int getArea();
}
class Rectangle implements Shape {
private int width, height;
Rectangle(int w, int h) {
this.width = w;
this.height = h;
}
public int getArea() {
return width * height;
}
}
class Square implements Shape {
private int side;
Square(int s) {
this.side = s;
}
public int getArea() {
return side * side;
}
}
β‘ λ μ΄μ Rectangle
μ μ΅μ§ μμνμ§ μκ³ , μν λ§ κ³΅μ
4οΈβ£ ISP - μΈν°νμ΄μ€ λΆλ¦¬ μμΉ
ν΄λΌμ΄μΈνΈλ μμ μ΄ μ¬μ©νμ§ μλ λ©μλμ μμ‘΄νμ§ μμμΌ νλ€
π§ 리ν©ν λ§ μ
interface Animal {
void fly();
void walk();
}
class Dog implements Animal {
public void fly() {
// μ무 λμ μν¨
}
public void walk() {
// κ±·κΈ°
}
}
β‘ Dog
λ fly()
λ₯Ό μΈ μΌμ΄ μμμλ ꡬνν΄μΌ ν¨
β 리ν©ν λ§ ν
interface Walkable {
void walk();
}
interface Flyable {
void fly();
}
class Dog implements Walkable {
public void walk() {
// κ±·κΈ°
}
}
β‘ νμν μΈν°νμ΄μ€λ§ ꡬννκ² λΆλ¦¬
5οΈβ£ DIP - μμ‘΄μ± μμ μμΉ
μμ λͺ¨λμ νμ λͺ¨λμ μμ‘΄νμ§ μκ³ , μΆμνμ μμ‘΄ν΄μΌ νλ€
π§ 리ν©ν λ§ μ
class MySQLRepository {
public void save() {
// DB μ μ₯
}
}
class UserService {
private MySQLRepository repo = new MySQLRepository();
public void register() {
repo.save();
}
}
①ꡬν체μ μ§μ μμ‘΄ β κ΅μ²΄, ν μ€νΈ λΆκ°
β 리ν©ν λ§ ν
interface Repository {
void save();
}
class MySQLRepository implements Repository {
public void save() {
// DB μ μ₯
}
}
class UserService {
private final Repository repo;
public UserService(Repository repo) {
this.repo = repo;
}
public void register() {
repo.save();
}
}
β‘ μΈν°νμ΄μ€λ₯Ό ν΅ν΄ μμ‘΄μ± μ£Όμ β μ μ°μ± μ¦κ°
π‘ SOLID νμ€ μκΈ° ν
π μμ€λ¦¬μΈν°μ
β λ¨μΌ μ±
μ, κ°λ°© νμ, 리μ€μ½ν μΉν, μΈν°νμ΄μ€ λΆλ¦¬, μμ‘΄μ± μμ
β λ§λ¬΄λ¦¬ μμ½
μμΉ | ν΅μ¬ λͺ©μ |
---|---|
SRP | νλμ μ± μλ§ κ°μ§μ |
OCP | λ³κ²½ μμ΄ κΈ°λ₯ νμ₯ |
LSP | μμμ λΆλͺ¨μ²λΌ μ¬μ© |
ISP | μΈν°νμ΄μ€λ μκ² μͺΌκ°μ |
DIP | ꡬνμ΄ μλλΌ μΆμνμ μμ‘΄νμ |
λκΈλ¨κΈ°κΈ°