...

κ°λ°© νμ μμΉ - OCP (Open Closed Principle)
κ°λ°© νμμ μμΉ(OCP)μ΄λ κΈ°μ‘΄μ μ½λλ₯Ό λ³κ²½νμ§ μμΌλ©΄μ, κΈ°λ₯μ μΆκ°ν μ μλλ‘ μ€κ³κ° λμ΄μΌ νλ€λ μμΉμ λ§νλ€.
λ³΄ν΅ OCPλ₯Ό νμ₯μ λν΄μλ κ°λ°©μ (open)μ΄κ³ , μμ μ λν΄μλ νμμ (closed)μ΄μ΄μΌ νλ€λ μλ―Έλ‘ μ μνλ€.
μ¬κΈ°μ νμ₯μ΄λ μλ‘μ΄ κΈ°λ₯μ΄ μΆκ°λ¨μ μλ―Ένλ€.
λ°λΌμ ν΄μνμλ©΄, κΈ°λ₯ μΆκ° μμ²μ΄ μ€λ©΄ ν΄λμ€λ₯Ό νμ₯μ ν΅ν΄ μμ½κ² ꡬννλ©΄μ, νμ₯μ λ°λ₯Έ ν΄λμ€ μμ μ μ΅μν νλλ‘ νλ‘κ·Έλ¨μ μμ±ν΄μΌ νλ μ€κ³ κΈ°λ²μ λ§νλ€κ³ 보면 λλ€.
[ νμ₯μ μ΄λ €μλ€ ]
- λͺ¨λμ νμ₯μ±μ 보μ₯νλ κ²μ μλ―Ένλ€.
- μλ‘μ΄ λ³κ²½ μ¬νμ΄ λ°μνμ λ μ μ°νκ² μ½λλ₯Ό μΆκ°ν¨μΌλ‘μ¨ μ ν리μΌμ΄μ μ κΈ°λ₯μ ν° νμ λ€μ΄μ§ μκ³ νμ₯ν μ μλ€.
[ λ³κ²½μ λ«νμλ€ ]
- κ°μ²΄λ₯Ό μ§μ μ μΌλ‘ μμ νλ건 μ νν΄μΌ νλ€λ κ²μ μλ―Ένλ€.
- μλ‘μ΄ λ³κ²½ μ¬νμ΄ λ°μνμ λ κ°μ²΄λ₯Ό μ§μ μ μΌλ‘ μμ ν΄μΌ νλ€λ©΄ μλ‘μ΄ λ³κ²½μ¬νμ λν΄ μ μ°νκ² λμν μ μλ μ ν리μΌμ΄μ μ΄λΌκ³ λ§νλ€.
- μ΄λ μ μ§λ³΄μμ λΉμ© μ¦κ°λ‘ μ΄μ΄μ§λ λ§€μ° μμ’μ μμμ΄λ€.
- λ°λΌμ κ°μ²΄λ₯Ό μ§μ μμ νμ§ μκ³ λ λ³κ²½μ¬νμ μ μ©ν μ μλλ‘ μ€κ³ν΄μΌ νλ€. κ·Έλμ λ³κ²½μ λ«νμλ€κ³ ννν κ²μ΄λ€.
μ΄λ ΅κ² μκ°ν νμμμ΄, OCP μμΉμ μ°λ¦¬κ° κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ νλ©΄μ μ§λ¦¬λλ‘ λ°°μ λ μΆμνλ₯Ό μλ―Ένλ κ²μΌλ‘ 보면 λλ€.

μ¦, OCPλ λ€νμ±κ³Ό νμ₯μ κ°λ₯μΌ νλ κ°μ²΄μ§ν₯μ μ₯μ μ κ·Ήλννλ μ€κ³ μμΉμΌλ‘μ¨, μ°λ¦¬λ μ½λ©ν λ κ°μμμ λ°°μ΄λλ‘ κ°μ²΄λ₯Ό μΆμνν¨μΌλ‘μ¨, νμ₯μ μ΄λ €μκ³ λ³κ²½μ λ«νμλ μ μ°ν ꡬ쑰λ₯Ό λ§λ€μ΄ μ¬μ©ν΄μ€λ©° κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ OCP μμΉμ ν¨κ³Όλ₯Ό μ΄μ©ν΄μλ κ²μ΄λ€.
κ·Έλμ ν΄λμ€λ₯Ό μΆκ°ν΄μΌνλ€λ©΄ κΈ°μ‘΄ μ½λλ₯Ό ν¬κ² μμ ν νμμμ΄, μ μ νκ² μμ κ΄κ³μ λ§μΆ° μΆκ°λ§ νλ€λ©΄ μ μ°νκ² νμ₯μ ν μ μμλ κ²μ΄λ€.
OCP μμΉ μλ° μμ μ μμ νκΈ°
λ€μ Animal ν΄λμ€κ° μκ³ , Animal νμ μ λ°μ κ³ μμ΄ νΉμ κ°λ©΄ κ°κ° λλ¬Όμ μ리μ λ§μΆ° μΆλ ₯νλ HelloAnimal ν΄λμ€κ° μλ€.
λ©μΈ λ©μλμμ catκ³Ό dog λλ¬Ό κ°μ²΄λ₯Ό λ§λ€κ³ HelloAnimal ν΄λμ€μ hello() λ©μλλ₯Ό ν΅ν΄ μ€νν΄λ³΄λ©΄ μ€λ₯μμ΄ μ λμλ¨μ νμΈ ν μ μλ€.
class Animal {
String type;
Animal(String type) {
this.type = type;
}
}
// λλ¬Ό νμ
μ λ°μ κ° λλ¬Όμ λ§μΆ° μΈμμ리λ₯Ό λ΄κ² νλ ν΄λμ€ λͺ¨λ
class HelloAnimal {
void hello(Animal animal) {
if(animal.type.equals("Cat")) {
System.out.println("λμΉ");
} else if(animal.type.equals("Dog")) {
System.out.println("λ©λ©");
}
}
}
public class Main {
public static void main(String[] args) {
HelloAnimal hello = new HelloAnimal();
Animal cat = new Animal("Cat");
Animal dog = new Animal("Dog");
hello.hello(cat); // λμΉ
hello.hello(dog); // λ©λ©
}
}
λμ μ체λ λ¬Έμ κ° μμ§λ§ λ¬Έμ λ κΈ°λ₯ μΆκ° μ΄λ€.
λ§μΌ 'κ³ μμ΄'μ 'κ°' μΈμ 'μ'μ΄λ 'μ¬μ'λ₯Ό μΆκ°νκ² λλ€λ©΄ μ΄λ»κ² λ κΉ?
λΉμ°ν HelloAnimal ν΄λμ€λ₯Ό μμ ν΄μ£Όμ΄μΌ νλ€. κ° κ°μ²΄μ νλ λ³μμ λ§κ² ifλ¬Έμ λΆκΈ°νμ¬ κ΅¬μ±ν΄μ€μΌ νλ€.
public class Main {
public static void main(String[] args) {
HelloAnimal hello = new HelloAnimal();
Animal cat = new Animal("Cat");
Animal dog = new Animal("Dog");
Animal sheep = new Animal("Sheep");
Animal lion = new Animal("Lion");
hello.hello(cat); // λμΉ
hello.hello(dog); // λ©λ©
hello.hello(sheep);
hello.hello(lion);
}
}
class HelloAnimal {
// κΈ°λ₯μ νμ₯νκΈ° μν΄μλ ν΄λμ€ λ΄λΆ ꡬμ±μ μΌμΌν μμ ν΄μΌ νλ λ²κ±°λ‘μμ΄ μκΈ΄λ€.
void hello(Animal animal) {
if (animal.type.equals("Cat")) {
System.out.println("λμΉ");
} else if (animal.type.equals("Dog")) {
System.out.println("λ©λ©");
} else if (animal.type.equals("Sheep")) {
System.out.println("λ©μμ");
} else if (animal.type.equals("Lion")) {
System.out.println("μ΄ν₯");
}
// ...
}
}
μ΄λ°μμΌλ‘ μ½λλ₯Ό ꡬμ±νλ€λ©΄, λλ¬Όμ΄ μΆκ°λ λλ§λ€ κ³μ μ½λλ₯Ό μΌμΌν λ³κ²½ν΄μ€μΌ νλ λ²κ±°λ‘μ΄ μμ μ΄ μκΈ°κ² λλ€.
μ΄λ μ²μλΆν° μ€κ³μλΆν° μλͺ»λμκΈ° λλ¬Έμ μΌμ΄λλ νμμ΄λ€.
λ°λΌμ μ²μμ OCP μ€κ³ μμΉμ λ°λΌ μ μ ν μΆμν ν΄λμ€λ₯Ό ꡬμ±νκ³ μ΄λ₯Ό μμνμ¬ νμ₯μν€λ κ΄κ³λ‘ ꡬμ±νλ©΄ λ³κ²½μλ λ«νκ³ (closed) μΆκ°μλ μ΄λ €μλ(opened) νλ‘κ·Έλ¨μ λ§λ€μ μλ€.
μ΄λ μ΄λ€μμΌλ‘ OCP λλ‘ μΆμν μ€κ³λ₯Ό ν κ²μΈκ°μ λν΄μλ λ€μ κ·μΉλλ‘ μ΄ννλ©΄ λλ€.
- λ¨Όμ λ³κ²½(νμ₯)λ κ²κ³Ό λ³νμ§ μμ κ²μ μ격ν ꡬλΆνλ€.
- μ΄ λ λͺ¨λμ΄ λ§λλ μ§μ μ μΆμν(μΆμν΄λμ€ or μΈν°νμ΄μ€)λ₯Ό μ μνλ€.
- ꡬν체μ μμ‘΄νκΈ°λ³΄λ€ μ μν μΆμνμ μμ‘΄νλλ‘ μ½λλ₯Ό μμ± νλ€.

// μΆμν
abstract class Animal {
abstract void speak();
}
class Cat extends Animal { // μμ
void speak() {
System.out.println("λμΉ");
}
}
class Dog extends Animal { // μμ
void speak() {
System.out.println("λ©λ©");
}
}
class HelloAnimal {
void hello(Animal animal) {
animal.speak();
}
}
public class Main {
public static void main(String[] args) {
HelloAnimal hello = new HelloAnimal();
Animal cat = new Cat();
Animal dog = new Dog();
hello.hello(cat); // λμΉ
hello.hello(dog); // λ©λ©
}
}
μμ κ°μ΄ ꡬμ±νκ² λλ©΄ κΈ°λ₯ μΆκ°κ° λ¬μλμλ μ½λ μμ μμ΄ νμ₯μ΄ κ°λ₯νκ² λλ€.
λ°λΌμ λ€μκ³Ό κ°μ΄ μ ν΄λμ€μ μ¬μ ν΄λμ€λ₯Ό μΆκ°ν λ HelloAnimal ν΄λμ€μ μ½λ μμ μμ΄ μ μμ μΌλ‘ κΈ°λ₯ νμ₯μ΄ λλ κ²μ 보μ¬μ£Όκ² λλ€.
// μΆμν΄λμ€λ₯Ό μμλ§ νλ©΄ λ©μλ κ°μ ꡬν κ·μΉμΌλ‘ κ·κ²©νλ§ νλ©΄ νμ₯μ μ ν μλ€ (opened)
class Sheep extends Animal {
void speak() {
System.out.println("λ§€μμ");
}
}
class Lion extends Animal {
void speak() {
System.out.println("μ΄ν₯");
}
}
// κΈ°λ₯ νμ₯μΌλ‘ μΈν ν΄λμ€κ° μΆκ°λμ΄λ, λμ΄μ μμ ν νμκ° μμ΄μ§λ€ (closed)
class HelloAnimal {
void hello(Animal animal) {
animal.speak();
}
}
public class Main {
public static void main(String[] args) {
HelloAnimal hello = new HelloAnimal();
Animal cat = new Cat();
Animal dog = new Dog();
Animal sheep = new Sheep();
Animal lion = new Lion();
hello.hello(cat); // λμΉ
hello.hello(dog); // λ©λ©
hello.hello(sheep); // λ§€μμ
hello.hello(lion); // μ΄ν₯
}
}
OCP μμΉμ λ°λ₯Έ JDBC

OCP μμΉμ κ°μ₯ μ λ°λ₯΄λ μμκ° λ°λ‘ μλ°μ λ°μ΄ν°λ² μ΄μ€ μΈν°νμ΄μ€μΈ JDBCμ΄λ€.
λ§μΌ μλ° μ ν리μΌμ΄μ μμ μ¬μ©νκ³ μλ λ°μ΄ν°λ² μ΄μ€λ₯Ό MySQLμμ Oracleλ‘ λ°κΎΈκ³ μΆλ€λ©΄, 볡μ‘ν νλ μ½λ© μμ΄ κ·Έλ₯ connection κ°μ²΄ λΆλΆλ§ κ΅μ²΄ν΄μ£Όλ©΄ λλ€.
μ¦, μλ° μ ν리μΌμ΄μ μ λ°μ΄ν°λ² μ΄μ€λΌκ³ νλ μ£Όλ³μ λ³νμ λ«ν(closed) λμ΄ μλ κ²μ΄λ€. λ°λλ‘ λ°μ΄ν°λ² μ΄μ€λ₯Ό μμ½κ² κ΅μ²΄νλ€λ κ²μ λ°μ΄ν°λ² μ΄μ€κ° μμ μ νμ₯μλ μ΄λ € μλ€λ λ§μ΄ λλ€.
OCP μμΉ μ μ© μ£Όμμ
νμ₯μλ μ΄λ €μκ³ λ³κ²½μλ λ«νκ² νκΈ° μν΄μλ μΆμνλ₯Ό μ μ€κ³ν νμμ±μ΄ μλλ°, μΆμν(μΆμ ν΄λμ€ or μΈν°νμ΄μ€)λ₯Ό μ μν λ μ¬λ¬ κ²½μ°μ μμ λν κ³ λ €μ μμΈ‘μ΄ νμνλ€.
λ³΄ν΅ μ°λ¦¬λ μΆμνλΌλ κ°λ μ λν΄ 'ꡬ체μ μ΄μ§ μμ' μ λμ μλ―Έλ‘ λμ¨νκ² μκ³ λ§ μλ€. νμ§λ§ 'κ·Έλλ λΆμΉ(Grady Booch)'μ μνλ©΄ 'μΆμνλ λ€λ₯Έ λͺ¨λ μ’ λ₯μ κ°μ²΄λ‘λΆν° μλ³λ μ μλ κ°μ²΄μ λ³Έμ§μ μΈ νΉμ§'μ΄λΌκ³ μ μνλ€.
μ¦, μΆμ λ©μλ μ€κ³μμ μ λΉν μΆμν λ 벨μ μ νν¨μΌλ‘μ¨, μ΄λ ν νμμ λν λ³Έμ§μ μΈ μ μλ₯Ό μλΈ ν΄λμ€μ μ νν¨μΌλ‘μ¨ κ΄κ³λ₯Ό μ±λ¦½λκ² νλ κ²μ΄λ€.
λ§μΌ μ΄λ¬ν μΆμνμ λ°λ₯Έ μμ ꡬ쑰λ₯Ό μ²μλΆν° μμνκ² κ΅¬μ±νκ² λλ©΄, λ€μμ λ°°μΈ LSP(리μ€μ½ν μΉν μμΉ) rκ³Ό ISP(μΈν°νμ΄μ€ λΆλ¦¬ μμΉ) μλ°μΌλ‘ μ΄μ΄μ§κ² λλ€.
λν OCPλ DIP(μμ‘΄ μμ μμΉ)μ μ€κ³ κΈ°λ°μ΄ λκΈ°λ νλ€.
λ°λΌμ μ΄λΆλΆμ μ€λ‘μ§ κ°λ°μμ μλμ λ¬λ € μλ€κ³ ν΄λ κ³ΌμΈμ΄ μλλ€. λ§μ κ²½νκ³Ό κ²½λ ₯λ§μ΄ μλμ ν€μΈ μ μλ€.
# μ°Έκ³ μλ£
https://www.nextree.co.kr/p6960/
https://youtu.be/EmnIdUvTRfk
https://youtu.be/fJcZML8ZQAE
https://shinsunyoung.tistory.com/m/18
μ΄ κΈμ΄ μ’μΌμ ¨λ€λ©΄ ꡬλ & μ’μμ
μ¬λ¬λΆμ ꡬλ
κ³Ό μ’μμλ
μ μμκ² ν° νμ΄ λ©λλ€.