ํฌ์คํธ ์ ๋ชฉ ๊ทธ๋๋ก Spring ๊ธฐ์ด๋ฅผ ๋ค์น๊ธฐ ์ํด ์์ฑํ๋ TIL ์ฑ๊ฒฉ์ ์คํ๋ง ๊ธฐ์ด ์์ฝ ์๋ฆฌ์ฆ์
๋๋ค.
Java๋ ์๋๋ฐ Spring์ ๋ญ์ง ๋ชจ๋ฅด๊ฒ ๋ ์ด๋ณด์๋ค์๊ฒ ์ ์ฉํ๋๋ก ์์ ๋ง ์ ๋ฆฌํ์์ต๋๋ค.
Spring 101 ์๋ฆฌ์ฆ
Spring 101 - #1 ์๋ฐ์ ์ ์ฐจ์ /๊ตฌ์กฐ์ ํ๋ก๊ทธ๋๋ฐ
Spring 101 - #2 ์๋ฐ ํค์๋์ OOP ํน์ฑ 4๊ฐ์ง
Spring 101 - #3 ๊ฐ์ฒด์งํฅ์ค๊ณ 5์์น SOLID
Spring 101 - #4 ์คํ๋ง ๋์์ธ ํจํด Spring Design Pattern
์๋ฆฌ | OOP ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ |
์๋ฆฌ ๋๊ตฌ | ๊ฐ์ฒด์งํฅ 4๋ ํน์ฑ (Encapsulation, Abstraction, Extend, Polymorphysim ์บก์์ถ๋ค) |
์๋ฆฌ ๋๊ตฌ ์ฌ์ฉ๋ฒ | ๊ฐ์ฒด์งํฅ ์ค๊ณ ์์น (SOLID - SRP, OCP, LSP, ISP, DIP) |
๋ ์ํผ | Design Pattern ์ค๊ณ ํจํด |
#์คํ๋ง ํ๋ ์์ํฌ = OOP ํ๋ ์์ํฌ
#๋ํ์ ์ธ ์คํ๋ง ๋์์ธํจํด
- Adapter Pattern
- Proxy Pattern
- Decorator Pattern
- Singleton Pattern
- Template Method Pattern
- Factory Method Pattern
- Strategy Pattern
- Template callback Pattern
#Class diagram, Sequence diagram ๋ณด๋ ํด - Amateras UML
- Amateras UML ๋ฐฐํฌ์ฌ์ดํธ: http://sourceforge.jp/projects/amateras/releases > AmaterasUML.zip ํ์ผ ๋ค์ด๋ก๋
- ์์ถํด์ ํ jar ํ์ผ๋ค์ STS์ plugin์ ๋ณต์ฌ
- STS ์ฌ์คํ -> ์ค์น ์๋ฃ
๋ผ๊ณ ์ฑ
์ ๋์์๋๋ฐ ๋๋ ์ ๋ ๊ฒ ํด๋ ์ค์น๊ฐ ์๋ผ์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ค์นํ๋ค.
์ค์น ๋ฐฉ๋ฒ์ ์ด ๋ธ๋ก๊ทธ ๋ค๋ฅธ ํฌ์คํธ (Amateras UML ๋งฅ๋ถ Eclipsed์ ์ค์นํ๊ธฐ)์ ์ ๋ฆฌ ํ๋ค.
(์ค์นํ๊ณ ๋๋ ์ง์ง ๋๋ฌด ์ข๋ค ใ
ใ
โค๏ธ)
#Adapter Pattern
:ํธ์ถ๋นํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ชฝ์ ์ฝ๋์ ๋์๋๋๋ก ์ค๊ฐ์ adapter ๋ณํ๊ธฐ๋ฅผ ํตํด ํธ์ถํ๋ ํจํด.
- ๊ฐ์ฒด๋ฅผ Service์ ์์ฑ์ผ๋ก ๋ง๋ค์ด์ ์ฐธ์กฐํ๋ค.
#Proxy Pattern
: ์ ์ด ํ๋ฆ์ ์กฐ์ ํ๊ธฐ ์ํด ์ค๊ฐ์ ๋๋ฆฌ์๋ฅผ ๋๋ ํจํด. "์๋์ฐจ - ํ์ด์ด" ๊ด๊ณ
- Proxy(๋๋ฆฌ์)๋ IService์ interfacef๋ฅผ ๊ตฌํ + ์ค์ ์๋น์ค์ ๊ฐ์ ์ด๋ฆ์ method๋ฅผ ๊ตฌํํ๋ค.
- ์ค์ ์๋น์ค์ ๋ํ ์ฐธ์กฐ๋ณ์๋ฅผ ๊ฐ๋๋ค (IService service = new Service();) - ํฉ์ฑ!
- ์ค์ ์๋น์ค์ ๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง method๋ฅผ client์ ๋ฐํํ๋ค (Proxy์ ๋ฉ์๋๋ abstract)
- ์ค์ ์๋น์ค ์ ํ์ ๋ณ๋ ๋ก์ง์ ์คํํ ์ ์๋ค. ๊ฐ์ ์ ์ดํ์ง ์๋๋ค. (Decorator์์ ์ฐจ์ด)
package proxyPattern;
public class Proxy implements IService {
IService service1;
public String runSomething() {
System.out.println("ํธ์ถ์ ๋ํ ํ๋ฆ ์ ์ด๊ฐ ์ฃผ๋ชฉ์ , ๋ฐํ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ์ ๋ฌ");
service1 = new Service();
return service1.runSomething();
}
}
Proxy class์์ IService ๊ฐ์ฒด๋ฅผ ๋ง๋ ํ, ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ return ํ๋ค
package proxyPattern;
public class ClientWithProxy {
public static void main(String[] args) {
// ํ๋ก์๋ฅผ ์ด์ฉํ ํธ์ถ
IService proxy = new Proxy();
System.out.println(proxy.runSomething());
}
}
Client์์๋ Proxy ๊ฐ์ฒด๋ฅผ ํตํด์ ์๋น์ค๋ฅผ ์ด์ฉํ๋ค
# Decorator Pattern
: ๋ฉ์๋ ํธ์ถ์ ๋ฐํ๊ฐ์ ๋ณํ๋ฅผ ์ฃผ๊ธฐ ์ํด ์ค๊ฐ์ decorator๋ฅผ ๋๋ ํจํด
- ๊ตฌํ ๋ฐฉ๋ฒ์ Proxy์ ๊ฐ์ผ๋, ๋จ Client๊ฐ ์ต์ข ์ ์ผ๋ก ๋๋ ค๋ฐ๋ ๋ฐํ๊ฐ์ ์ฅ์์ ๋ง์ ํ๋ค.
#Singleton Pattern
: ์ธ์คํด์ค ํ๋๋ง ๋ง๋ค์ด ์ฌ์ฉํ๊ธฐ ์ํ ํจํด. (e.g. ์ปค๋ฅ์ ํ, ์ค๋ ๋ ํ, ๋๋ฐ์ด์ค ์ค์ ๊ฐ์ฒด)
- Private Generator
- Static Variable
- Static Method
- ์ฐ๊ธฐ ๊ฐ๋ฅํ ์์ฑ์ ๊ฐ์ง ์๋๋ค.
package singletonPattern;
public class Singleton {
static Singleton singletonObject; // ์ ์ ์ฐธ์กฐ ๋ณ์
private Singleton() {
}; // private ์์ฑ์
// ๊ฐ์ฒด ๋ฐํ ์ ์ ๋ฉ์๋
public static Singleton getInstance() {
if (singletonObject == null) {
singletonObject = new Singleton();
}
return singletonObject;
}
}
๊ณต์ ๊ฐ์ฒด์ด๋ฏ๋ก ์์ฑ์ ๋๋๋ก ๊ฐ์ง ์๊ฒ ํ๋ ๊ฒ์ด ์ ์์ด๋ค. (์ฝ๊ธฐ ์ ์ฉ์ ๊ด์ฐฎ์.)
#Template Method Pattern
: ๋์ผํ ๋ถ๋ถ์ ์์ ํด๋์ค๋ก, ๋ฌ๋ผ์ง๋ ๋ถ๋ถ๋ง ํ์ ํด๋์ค๋ก ๋ถํ ํ๋ ์ค๊ณ
Animal Template์๋์ ๋ณผํธ ๊ฐ์์ง์ ํคํฐ ๊ณ ์์ด.
- ์์ ํด๋์ค: ๊ณตํต ๋ก์ง์ ์ํํ๋ ํ ํ๋ฆฟ method(Override method) + ์ ํ์ ์ผ๋ก overrideํ๋ Hook method๋ฅผ ๋๋ค
#Factory Method Pattern
: Override๋ method๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ํจํด
๊ฐ์ฒด๋ฅผ ์์ฑ ๋ฐํํ๋ method. ๊ฐ์์ง์ ๊ณ ์์ด์ ๊ฐ์์ Toy.
- Abstract Class๊ฐ 2๊ฐ
package factoryMethodPattern;
public class Cat extends Animal {
// ์ถ์ ํฉํฐ๋ฆฌ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
@Override
AnimalToy getToy() {
return new CatToy();
}
}
Override๋ method๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค
#Strategy Patternโญโญโญโญโญ
: ๋์์ธ ํจํด์ ๊ฝ์ธ Strategy Pattern! Client๊ฐ Strategy๋ฅผ ์์ฑํด ์ด๋ฅผ ์คํํ Context์ ์ฃผ์ ํ๋ ํจํด
๊ตฌ์ฑ์์ - Strategy, Context, Client
- ์ ๋ต ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ๋ต ๊ฐ์ฒด Strategy - interface
- ์ ๋ต ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ Context (์ฌ์ฉ์/์๋น์) - runContext()
- ์ ๋ต ๊ฐ์ฒด๋ฅผ ์์ฑํด์ Context์ ์ฃผ์ ํ๋ Client (์ 3์ / ์ ๋ต ๊ฐ์ฒด์ ๊ณต๊ธ์) - strategy ๊ฐ์ฒด, Context ๊ฐ์ฒด ๋ชจ๋ ์์ฑ! runContext๋ฅผ ์คํํ๋ค
#Template Callback Patternโญโญโญโญโญ
: Spring DI์์ ์ฌ์ฉํ๋ ํจํด. Strategy Pattern + ์ต๋ช ๋ด๋ถ ํด๋์ค๋ฅผ ํ์ฉํ๋ค
package templateCallbackPattern;
public class Client {
public static void main(String[] args) {
Soldier rambo = new Soldier();
rambo.runContext(new Strategy() {
@Override
public void runStrategy() {
System.out.println("์ด! ์ด์ด์ข
์ด ์ด! ์ด!");
}
});
System.out.println();
}
}
Refactoring : Context ์์ Strategy ๊ฐ์ฒด ์์ฑ์ ๋ฃ๋๋ค.โญโญโญโญโญ
package templateCallbackPatternRefactoring;
public class Soldier {
void runContext(String weaponSound) {
System.out.println("์ ํฌ ์์");
executeWeapon(weaponSound).runStrategy();
System.out.println("์ ํฌ ์ข
๋ฃ");
}
private Strategy executeWeapon(final String weaponSound) {
return new Strategy() {
@Override
public void runStrategy() {
System.out.println(weaponSound);
}
};
}
}
#Strategy & Template Callback & Refactored Template Callback - ๋ฐ๋์ ๊ธฐ์ตํ ๊ฒ
์ด์ธ์๋ Spring MVC - Frong Controller Pattern, MVC Pattern, etc ๋ฑ์ด ์๋ค.
#์ฐธ์กฐ
[์ฑ ] ์คํ๋ง ์ ๋ฌธ์ ์ํ ์๋ฐ ๊ฐ์ฒด ์งํฅ์ ์๋ฆฌ์ ์ดํด, ๊น์ข ๋ฏผ, ์ํค๋ถ์ค
'๐ป Software Engineering > Java Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Springย Bootย Batchย -ย ๋ณ๋ ฌย ์ฒ๋ฆฌ (0) | 2021.11.28 |
---|---|
Spring 101 - #3 ๊ฐ์ฒด์งํฅ์ค๊ณ 5์์น SOLID (0) | 2020.07.23 |
Spring 101 - #2 ์๋ฐ ํค์๋์ OOP ํน์ฑ 4๊ฐ์ง (0) | 2020.07.19 |
Spring 101 - #1 ์๋ฐ์ ์ ์ฐจ์ /๊ตฌ์กฐ์ ํ๋ก๊ทธ๋๋ฐ (0) | 2020.07.13 |