๐ฏ 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 | ๊ตฌํ์ด ์๋๋ผ ์ถ์ํ์ ์์กดํ์ |
๋๊ธ๋จ๊ธฐ๊ธฐ