728x90
๊ฐ์ฒด์งํฅ ์ค๊ณ 5๊ฐ์ง ์์น
- SRP(Single Responsibility Principle): ๋จ์ผ ์ฑ ์ ์์น
- OCP(Open/Closed Principle): ๊ฐ๋ฐฉ-ํ์ ์์น
- LSP(Liskov Substitution Principle): ๋ฆฌ์ค์ฝํ ์นํ ์์น
- ISP(Interface Segregation Principle): ์ธํฐํ์ด์ค ๋ถ๋ฆฌ ๋ฒ์น
- DIP(Dependency Inversion Principle): ์์กด๊ด๊ณ ์ญ์ ์์น
1. ๋จ์ผ ์ฑ ์ ์์น
ํ๋์ ํด๋์ค๋ ํ๋์ ์ฑ ์๋ง ๊ฐ์ ธ์ผ ํ๋ค.
โก ๋ณ๊ฒฝ์ด ์์ ๋ ํ๊ธ ํจ๊ณผ๊ฐ ์ ์ผ๋ฉด ๋จ์ผ ์ฑ ์ ์์น์ ์ ๋ฐ๋ฅธ ๊ฒ
2. ๊ฐ๋ฐฉ-ํ์ ์์น
์ํํธ์จ์ด ์์๋ ํ์ฅ์๋ ์ด๋ ค ์์ผ๋ ๋ณ๊ฒฝ์๋ ๋ซํ ์์ด์ผ ํ๋ค.
- ๋คํ์ฑ์ ํ์ฉ
๋ฌธ์ ์
MemberService ํด๋ผ์ด์ธํธ๊ฐ ์ธํฐํ์ด์ค๋ฅผ ๋ฐ๋ผ๋ณด๊ณ ์๊ธด ํ์ง๋ง ๊ตฌํ ํด๋์ค๋ฅผ ์ง์ ์ ํ โก ํด๋ผ์ด์ธํธ์ ๋ณ๊ฒฝ์ด ํ์
MemoryMemberRepository m = new MemoryMemberRepository();
MemoryMemberRepository m = new JdbcMemberRepository();
๋คํ์ฑ์ ์งํค๊ธด ํ์ง๋ง OCP๋ฅผ ๋ง์กฑํ์ง ๋ชปํจ
3. ๋ฆฌ์ค์ฝํ ์นํ ์์น
ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด๋ ํ๋ก๊ทธ๋จ์ ์ ํ์ฑ์ ๊นจ๋จ๋ฆฌ์ง ์์ผ๋ฉด์ ํ์ ํ์ ์ ์ธ์คํด์ค๋ก ๋ฐ๊ฟ ์ ์์ด์ผ ํ๋ค.
์์ ๊ฐ์ฒด๋ ๋ถ๋ชจ ๊ฐ์ฒด๋ฅผ ์์ ํ ๋์ฒดํ ์ ์์ด์ผ ํ๋ค.
์ง์ฌ๊ฐํ๊ณผ ์ ์ฌ๊ฐํ
public class Main
{
public static void main(String[] args)
{
// 1)
Rectangle rectangle = new Rectangle();
// 2)
Rectangle rectangle = new Square();
rectangle.setWidth(5);
rectangle.setHeight(10);
// 1) 50, 2) 100
System.out.println(rectangle.getArea());
}
}
- ์ ์ฌ๊ฐํ์ ์ง์ฌ๊ฐํ์ด๋ค. โก ์ ์ฌ๊ฐํ์ ์ง์ฌ๊ฐํ์ ์์ํ๋ค.
- ์ ์ฌ๊ฐํ์ด ์ง์ฌ๊ฐํ์ ์์ ํ ๋์ฒดํ ์ ์์ผ๋ฏ๋ก ๋ฆฌ์ค์ฝํ ์นํ ์์น์ ์๋ฐฐ! โก ์์ ๊ฐ์ฒด(์ ์ฌ๊ฐํ)๊ฐ ๋ถ๋ชจ ๊ฐ์ฒด(์ง์ฌ๊ฐํ)๋ฅผ ์์ ํ ๋์ฒดํ ์ ์์!
- ์ง์ฌ๊ฐํ๊ณผ ์ ์ฌ๊ฐํ์ ์์๊ด๊ณ๊ฐ ์ ํ ๋ ์ ์๋ค.
4. ์ธํฐํ์ด์ค ๋ถ๋ฆฌ ์์น
ํด๋ผ์ด์ธํธ๋ ์์ ์ด ์ฌ์ฉํ๋ ๋ฉ์๋์๋ง ์์กดํด์ผ ํ๋ค.
= ์ธํฐํ์ด์ค๋ ๊ทธ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ ํด๋ผ์ด์ธํธ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํด์ผ ํ๋ค.
5. ์์กด๊ด๊ณ ์ญ์ ์์น
๊ตฌํ ํด๋์ค์ ์์กดํ์ง ๋ง๊ณ ์ธํฐํ์ด์ค์ ์์กดํด์ผ ํ๋ค.
728x90