/*Spring ๊ธฐ๋ณธ ์์กด๋ฌธ๋ฒ*/
package pack;
class Baduk{
AI ag;
Baduk(AI ag){ //์์ฑ์ - ์์กด๊ฐ์ฒด์์ ์ธ๋ถ์์ ์ฃผ์
๋ฐ์.
this.ag = ag;
System.out.println("๋๊ตญ์ ์์ํฉ๋๋ค.");
}
void play() {ag.play();} // ์ฃผ์
๋ฐ์ ai์์์ play ์คํ
void stop() {ag.stop();}
}
//์์กด์ฑ ์ฃผ์
(DI): Baduk์ด ์ง์ AlphaGo๋ BetaGo๋ฅผ ์์ฑํ์ง ์๊ณ , ์ธ๋ถ(main)์์ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฌ๋ฐ์ ์ฌ์ฉ.
interface AI{
abstract void play();
abstract void stop();
}
class AlphaGo implements AI{
public void play() {System.out.println("์ธ๊ณต์ง๋ฅ์ ์ํ๊ณ ์
๋๋ค.");}
public void stop() {System.out.println("์ํ๊ณ ๊ฐ ๊ฒ์์ ์ค๋จ.");}
}
class BetaGo implements AI{
public void play() {System.out.println("์ธ๊ณต์ง๋ฅ์ ๋ฒ ํ๊ณ ์
๋๋ค.");}
public void stop() {System.out.println("๋ฒ ํ๊ณ ๊ฒ์์ ์ค๋จ.");}
} // ๊ฐ์ Interface์ง๋ง ํ๋์ ๋ค๋ฅธ "๋คํ์ฑ ์คํ"
public class Sample {
public static void main(String[] args) {
Baduk b1 = new Baduk(new AlphaGo()); // ์์กด์ฑ Alphago ์ฃผ์
Baduk b2 = new Baduk(new BetaGo()); // ์์กด์ฑ Betago ์ฃผ์
b1.play(); b1.stop(); // ์คํ์ฝ๋
b2.play(); b2.stop();
}
}
/*
class A{}
interface IA{}
interface IB{}
interface IC{}
class B extends A implements IA, IB, IC{}
*/
๐ ์คํ ํ๋ฆ & ๊ฒฐ๊ณผ
Baduk b1 = new Baduk(new AlphaGo());
Baduk ์์ฑ์ ์คํ โ "๋๊ตญ์ ์์ํฉ๋๋ค." ์ถ๋ ฅ.
๋ด๋ถ์ ์ผ๋ก ag = new AlphaGo() ์ ์ฅ.
b1.play();
AlphaGo.play() ์คํ โ "์ธ๊ณต์ง๋ฅ์ ์ํ๊ณ ์ ๋๋ค." ์ถ๋ ฅ.
b1.stop();
AlphaGo.stop() ์คํ โ "์ํ๊ณ ๊ฐ ๊ฒ์์ ์ค๋จ." ์ถ๋ ฅ.
Baduk b2 = new Baduk(new BetaGo());
"๋๊ตญ์ ์์ํฉ๋๋ค." ์ถ๋ ฅ.
๋ด๋ถ์ ์ผ๋ก ag = new BetaGo() ์ ์ฅ.
b2.play();
"์ธ๊ณต์ง๋ฅ์ ๋ฒ ํ๊ณ ์ ๋๋ค." ์ถ๋ ฅ.
b2.stop();
"๋ฒ ํ๊ณ ๊ฒ์์ ์ค๋จ." ์ถ๋ ฅ.
์คํ๋ง๋ถํธ - ์คํ๋ง ํ๋ ์์ํฌ๋ฅผ ๋ ์ฝ๊ณ ๋น ๋ฅด๊ฒ ์ด์ฉํ ์์๋๋ก ๋ง๋ ๋๊ตฌ. ๋น ๋ฅด๊ฒ ์คํ๋ง ํ๋ก์ ํธ๋ฅผ ์ค์ . ์์กด์ฑ ์ธํธ๋ผ๊ณ ๋ถ๋ฆฌ๋ ์คํํฐ๋ฅผ ์ด์ฉํด ์์กด์ฑ์ ์ฝ๊ฒ ์ฌ์ฉํ๊ณ ๊ด๋ฆฌ๊ฐ๋ฅ.
1. ์คํ๋ง๋ถํธ๋ ์คํ๋ง์ฝ์ด์ ์คํ๋ง MVC์ ๋ชจ๋ ๊ธฐ๋ฅ์ ์๋์ผ๋ก ๋ก๋ํ๋ฏ๋ก ์๋์ผ๋ก ๊ฐ๋ฐํ๊ฒฝ ๊ตฌ์ฑํ ํ์ X
2. ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์
์ ํฐ์บฃ๊ณผ ๊ฐ์ WAS์์ ๋ฐฐํฌ
3. ์๋ฐ์ฝ๋๋ ํด๋์ค A์์ B ๊ฐ์ฒด๋ฅผ ์ฐ๊ณ ์ถ์ ๊ฒฝ์ฐ ์ง์ ์์ฑ. ์คํ๋ง์ ๊ฒฝ์ฐ ์ง์ ์์ฑ์ด ์๋ ์คํ๋ง ์ปจํ
์ด๋์์ ๊ฐ์ฒด๋ฅผ ์ฃผ์
๋ฐ์ ์ฌ์ฉ. -> Ioc/DI ๊ฐ๋
(์ค์ํจ!!)
// B๋ผ๋ ํด๋์ค
class B {
public void doSomething() {
System.out.println("B๊ฐ ์ผ์ ํฉ๋๋ค!");
}
}
// A๋ผ๋ ํด๋์ค
class A {
private B b;
public A() {
// A ์์์ B๋ฅผ ์ง์ ์์ฑํจ
this.b = new B();
}
public void action() {
b.doSomething();
}
}
// ์คํ
public class Main {
public static void main(String[] args) {
A a = new A(); // A๋ฅผ ๋ง๋ค๋ฉด A ๋ด๋ถ์์ B๋ ์์ฑ๋จ
a.action();
}
}
A๊ฐ B๋ฅผ ์ง์ newํด์ ์์ฑ. A๋ ํญ์ B๋ง ์จ์ผํจ
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component // ์คํ๋ง์ด ๊ด๋ฆฌํ๋ Bean
class B {
public void doSomething() {
System.out.println("B๊ฐ ์ผ์ ํฉ๋๋ค!");
}
}
@Component // ์คํ๋ง์ด ๊ด๋ฆฌํ๋ Bean
class A {
private final B b;
// ์์ฑ์ ์ฃผ์
(๊ถ์ฅ ๋ฐฉ์)
@Autowired
public A(B b) {
this.b = b; // ์คํ๋ง ์ปจํ
์ด๋๊ฐ B ๊ฐ์ฒด๋ฅผ ๋์ ๋ฃ์ด์ค
}
public void action() {
b.doSomething();
}
}
-----------------------
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public CommandLineRunner run(A a) { // ์คํ๋ง์ด A๋ฅผ ์ฃผ์
return args -> {
a.action(); // "B๊ฐ ์ผ์ ํฉ๋๋ค!"
};
}
}
new๋ฅผ ์ฌ์ฉํ์ง์๊ณ @Component๋ก ๋ฑ๋กํด๋๋ฉด ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๊ด๋ฆฌ
A๋ B๋ฅผ ์ง์ ๋ง๋ค์ง์๊ณ ์ธ๋ถ์์ ๋ฐ์์ DI ์ฌ์ฉ.
B๋์ C๋ผ๋ ํด๋์ค๋ฅผ ์ฐ๊ณ ์ถ์ผ๋ฉด ์คํ๋ง ์ค์ ๋ง ๋ฐ๊พธ๊ณ A์ ์ฝ๋๋ ๊ทธ๋๋ก ์ ์ง๊ฐ ๊ฐ๋ฅํ๋ค.
IoC - Inversion of Control ์ ์ด์ ์ญ์ .
DI - Dependency Injection ์์กด์ฑ ์ฃผ์
@Component // ํด๋์ค MyBean ๋น์ผ๋ก ๋ฑ๋ก
// ์คํ๋ง ์ปจํ
์ด๋์์ ์ด ํด๋์ค๋ฅผ ๊ด๋ฆฌ
public class MyBean {
}
AOP - Aspect Oriented Programming
๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ - ํ๋ก๊ทธ๋๋ฐ์ ๋ํ ๊ด์ฌ์ ํต์ฌ๊ด์ , ๋ถ๊ฐ๊ด์ ์ผ๋ก ๋๋์ด์ ๊ด์ฌ๊ธฐ์ค์ผ๋ก ๋ชจ๋ํํ๋๊ฒ์ ์๋ฏธํ๋ค. ์์งํ ๋ญ๋ง์ธ์ง ์์๋ฟ์...
์ฌ์ด ์๋ก ๊ณ์ข์ด์ฒด, ๊ณ ๊ฐ๊ด๋ฆฌํ๋ ํ๋ก๊ทธ๋จ์ด ์์ ๋ ๊ฐ ํ๋ก๊ทธ๋จ์๋ ๋ก๊น
์กฐ์ง, ์ง๊ธ๊น์ง ๋ฒ์ด์ง์ผ์ ๊ด๋ฆฌํ๊ธฐ์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ๋ก์ง ํฌํจ. ์ด๋ ํต์ฌ๊ด์ ์ ๊ณ์ข์ด์ฒด, ๊ณ ๊ฐ ๊ด๋ฆฌ ๋ก์ง์ด๊ณ ๋ถ๊ฐ ๊ด์ ์ ๋ก๊น
, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ๋ก์ง์ด๋ค.
๋ฐ๋ผ์ ํ๋ก๊ทธ๋๋จธ๋ ํต์ฌ ๊ด์ ์ฝ๋์๋ง ์ง์คํ๊ณ ํ๋ก๊ทธ๋จ์ ๋ณ๊ฒฝ๊ณผ ํ์ฅ์ ์ ์ฐํ๊ฒ ๋์์ด ๊ฐ๋ฅ.
Portable Service Attraction - ์คํ๋ง์์ ์ ๊ณตํ๋ ๋ค์ํ ๊ธฐ์ ๋ค์ ์ถ์ํ ํด์ ๊ฐ๋ฐ์๊ฐ ์ฝ๊ฒ ์ฌ์ฉํ๋ ์ธํฐํ์ด์ค
ํ์ค์ ๋ฆฌ
IoC - ๊ฐ์ฒด์ ์์ฑ๊ณผ ๊ด๋ฆฌ๋ฅผ ๊ฐ๋ฐ์๊ฐ ํ๋๊ฒ์ด ์๋๋ผ ํ๋ ์์ํฌ๊ฐ ๋์ ํ๋๊ฒ
DI - ์ธ๋ถ์์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ๋๊ฒ
AOP - ํ๋ก๊ทธ๋๋ฐ ํ ๋ ํต์ฌ ๊ด์ ๊ณผ ๋ถ๊ฐ ๊ด์ ์ ๋๋์ด ๊ฐ๋ฐํ๋๊ฒ
PSA - ์ด๋ ๊ธฐ์ ์ ์ฌ์ฉํ๋ ์ผ๊ด๋ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋๋ก ํ๋๊ฒ