headfirst designPattern ์ ์ฐธ๊ณ ํ๊ณ ํ์ตํ์์ต๋๋ค.
์๋ ๋ธ๋ก๊ทธ & ๊นํ์ ์ฐธ๊ณ ํ์์ต๋๋ค.
gmlwjd9405.github.io
OmarElGabry/DesignPatterns
quanke/design-pattern-java-source-code
yaboong.github.io
https://github.com/bo-yoon/DesignPattern
ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด์ด๋ ๋ง ๊ทธ๋๋ก ํ ํ๋ฆฟ, ํ๋ก๊ทธ๋จ์ ๊ณจ๊ฒฉ์ ๋งํ๋ค. ์ฌ๋ฌ ์ฌ๋์ด ๋์์ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ๋์ค์ ์ทจํฉํ๋ค๋ณด๋ฉด ์๋ก ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ๋ ๋ถ๋ถ์ด๋, ๋ณํ์ง ์์ ์๋ ๋ถ๋ถ ๊ฐ์ ๊ฒฝ์ฐ์๋ ๋ฏธ๋ฆฌ ์ ์ ํด๋๋ ๊ฒ์ด ํ์ํ๋ค. ๊ทธ๋ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด์ด๋ค.
์๋ฅผ ๋ค์ด ResponseDTO ๊ฐ์ ๊ฒฝ์ฐ ๊ณตํต ์๋ต ๋ถ๋ถ ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ฏธ๋ฆฌ ์ ์ ํด๋๊ณ ์์ํ๋ค.
ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด์๋ ํํฌ ๋ผ๋ ๊ฒ์ ์ฌ์ฉํ๋๋ฐ, ํํฌ๋ ์ถ์ํด๋ ์ค์์ ๊ฐ๋จํ ๊ธฐ๋ณธ์ ์ธ ๋ด์ฉ์ด ๊ตฌํ๋์ด ์๊ฑฐ๋, ์๋ฌด ์ฝ๋๋ ๊ตฌํ ๋์ง ์๋ ์ฝ๋๋ฅผ ๋งํ๋ค. ๊ทธ๋์ ์ถ์ ํด๋ ์ค๋ฅผ ์์ ๋ฐ์ ํ์ ํด๋์ค๋ ์ฌ๊ธฐ์ ์ถ๊ฐ์ ์ธ ์ฝ๋๋ฅผ ๊ตฌํํ๋ค.
package templeteMethod;
public abstract class AbstractDoit {
protected abstract void primitive();
protected abstract void hook();
public void templeteMethod() {
primitive();
hook();
}
}
package templeteMethod;
public class WeekDayDoit extends AbstractDoit {
@Override
protected void primitive() {
System.out.println("์ง๊ฐ์ ์ ์๊ธฐ ");
System.out.println("๊ฒ์ ํ ํ ");
}
@Override
protected void hook() {
System.out.println("๊ทธ์ ์ ํด๊ทผ ๋จผ์ ");
}
}
package templeteMethod;
public class Main {
public static void main(String [] args) {
AbstractDoit todo = new WeekDayDoit();
todo.templeteMethod();
}
}
์์ค ์ฝ๋
package factoryMethod;
public class StrawberryJuice {
public void flavor() {
System.out.println("๋ธ๊ธฐ ๋ง ์ฅฌ์ค");
}
}
public abstract class DrinkFactory {
abstract StrawberryJuice maker(String juice);
}
package factoryMethod;
public class JuiceFactory extends DrinkFactory{
public Juice maker(String juice) {
if(juice.equals("STRAWBERRY")) {
return new StrawberryJuice();
}
return null;
}
}
ํฉํ ๋ฆฌ ํด๋์ค์ ์์ฑํ๋ ๋ฉ์๋๊ฐ ์ฌ๋ฌ ๊ฐ์ด๋ค. ์ฆ, ํ๋์ ํฉํ ๋ฆฌ๋ ๊ฐ๊ณ์ ์ธํธ ๋งค๋ด๋ผ๊ณ ์๊ฐํ๋ฉด๋๋ค.
๋ด๊ฐ ์ํ๋ ๊ฒ์ผ๋ก ์์กด์ฑ์ ์ฃผ์ ํด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
package abstFactory;
public interface BeverageFactory {
public Beverage maker(String kind);
}
package abstFactory;
public class CoffeeFactory implements BeverageFactory {
@Override
public Beverage maker(String kind) {
kind = kind.toUpperCase();
if(kind.equals("AMERICANO")) {
return new Americano();
}
return null;
}
}
package abstFactory;
public class JuiceFactory implements BeverageFactory {
@Override
public Beverage maker(String kind) {
kind = kind.toUpperCase();
if(kind.equals("STRAWBERRY")) {
return new StrawberryJuice();
}else if(kind.equals("BANANA")){
return new BananaJuice();
}
return null;
}
}
package abstFactory;
public interface Beverage {
public void flavor();
}
package abstFactory;
public abstract class Coffee implements Beverage{
}
package abstFactory;
public class Americano extends Coffee {
@Override
public void flavor() {
System.out.println("์๋ฉ๋ฆฌ์นด๋
ธ~ ์ข์ ์ข์ ์ข์~");
}
}
package abstFactory;
public abstract class Juice implements Beverage {
}
package abstFactory;
public class BananaJuice extends Juice{
@Override
public void flavor() {
System.out.println("๋ฐ๋๋ ๋ง ์ฃผ์ค!");
}
}
package abstFactory;
public class BananaJuice extends Juice{
@Override
public void flavor() {
System.out.println("๋ฐ๋๋ ๋ง ์ฃผ์ค!");
}
}
package abstFactory;
public class Main {
public static void main(String [] args) {
BeverageFactory fac = new CoffeeFactory();
Beverage coffee = fac.maker("americano");
coffee.flavor();
fac = new JuiceFactory();
Beverage juice = fac.maker("banana");
juice.flavor();
}
}
์๋ฉ๋ฆฌ์นด๋
ธ~ ์ข์ ์ข์ ์ข์~
๋ฐ๋๋ ๋ง ์ฃผ์ค!