๋ชฉ์ฐจ
์ด๋ค ์ํฉ์์ ์ฐ์ผ๊น?
๊ธ์์๋ ๊ธ๊ผด, ํฌ๊ธฐ, ์๊น, ๋ด์ฉ ๋ฑ์ ์์ฑ์ด ์๋ค.
์ฐ๋ฆฌ๊ฐ ๋ฌธ์๋ฅผ ์์ฑํ ๋ ๋ณดํต ๊ธ๊ผด๊ณผ ํฌ๊ธฐ๋ ์ผ๊ด๋๊ฒ ๋ง์ถ๊ธฐ๋๋ฌธ์ ๋ณ๊ฒฝํ ์ผ์ด ๊ฑฐ์ ์๋ค!
ํ์ง๋ง ๋ด์ฉ๊ณผ ์๊น์ ์์๋๋๋ก ๋ณํ๋ ์์ฑ์ด๋ค.
๊ธ์ ๊ฐ์ฒด๋ ๊ธ๊ผด ๊ฐ์ฒด + ํฌ๊ธฐ ๊ฐ์ฒด + ์๊น ๊ฐ์ฒด + ๋ด์ฉ ๊ฐ์ฒด์ ์กฐํฉ์ผ๋ก ๋ง๋ค์ด ์ง๋คํ๋ฉด
์์ฃผ ๋ณํ์ง ์๋ ๊ธ๊ผด ๊ฐ์ฒด์ ํฌ๊ธฐ ๊ฐ์ฒด๋ฅผ ๋งค๋ฒ ์์ฑํ ํ์๊ฐ ์์๊น?
ํ๋ ๋ง๋ค์ด ๋๊ณ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํด๋ ๋์ง ์์๊น?
ํ๋ผ์ด์จ์ดํธ ํจํด์ด๋?
๊ฐ์ฒด๋ฅผ ๊ฐ๋ณ๊ฒ ๋ง๋ค์ด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ค์ด๋ ํจํด์ด๋ค.
์์ฃผ ๋ณํ๋ ์์ฑ๊ณผ ์์ฃผ ๋ณํ์ง ์๋ ์์ฑ์ ๋ถ๋ฆฌํ๊ณ ์ฌ์ฌ์ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ค์ธ๋ค!
๋ถํ์ํ ์ธ์คํด์ค ์์ฑ์ ์ต์ํํ๊ธฐ ๋๋ฌธ์
ํ์ํ ๋๋ง๋ค ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋๋ง๋ค ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ๊ฒ ์ฌ์ฉํ๊ณ
๋น ๋ฅธ ํ๋ก๊ทธ๋จ ์์ฑ์ด ๊ฐ๋ฅํด์ง๋ค
์ฅ์
๋จ์
๊ตฌํ์ฝ๋
๊ธฐ์กด์ฝ๋
public class Character {
private char value;
private String color;
private String fontFamily;
private int fontSize;
}
public class Client {
public static void main(String[] args) {
Character c1 = new Character('h', "white", "Nanum", 12);
Character c2 = new Character('e', "white", "Nanum", 12);
Character c3 = new Character('l', "white", "Nanum", 12);
Character c4 = new Character('l', "white", "Nanum", 12);
Character c5 = new Character('o', "white", "Nanum", 12);
}
}
๋ณ๊ฒฝ์ฝ๋
public class ๐Character {
private char value;
private String color;
private ๐Font font;
}
----------------------------------------
public final class ๐Font {
final String family;
final int size;
}
----------------------------------------
public class FontFactory {
private Map<String, ๐Font> cache = new HashMap<>();
//์ผ์ข
์ ์บ์์ญํ ์ ์ํํ๋ค!
public Font getFont(String font) {
if (cache.containsKey(font)) {
//์บ์์ ์กด์ฌํ๋ฉด ํด๋น ๊ฐ์ฒด๋ฅผ ๋ฐํ
return cache.get(font);
} else {
//์บ์์ ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑ
String[] split = font.split(":");
Font newFont = new Font(split[0], Integer.parseInt(split[1]));
cache.put(font, newFont);
return newFont;
}
}
}
public class Client {
public static void main(String[] args) {
FontFactory fontFactory = new FontFactory();
Character c1 = new Character('h', "white", fontFactory.getFont("nanum:12"));
Character c2 = new Character('e', "white", fontFactory.getFont("nanum:12"));
Character c3 = new Character('l', "white", fontFactory.getFont("nanum:12"));
}
}
'๐ฐ Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Proxy Pattern | ํ๋ก์ ํจํด (0) | 2022.08.16 |
---|---|
Facade Pattern | ํผ์ฌ๋ ํจํด (0) | 2022.08.16 |
Decorator Pattern | ๋ฐ์ฝ๋ ์ดํฐ ํจํด (0) | 2022.08.16 |
Composite Pattern | ์ปดํฌ์ง ํจํด (0) | 2022.08.15 |
Bridge Pattern | ๋ธ๋ฆฟ์ง ํจํด (0) | 2022.08.15 |