π‘ νμ¬λ ν¨ν΄ (Facade Patter)
μ΄λ€ μλΈμμ€ν μ μΌλ ¨μ μΈν°νμ΄μ€μ λν ν΅ν©λ μΈν°νμ΄μ€λ₯Ό μ 곡νλ€. νΌμ¬λμμ κ³ μμ€ μΈν°νμ΄μ€λ₯Ό μ μνκΈ° λλ¬Έμ μλΈμμ€ν μ λ μ½κ² μ¬μ©ν μ μλ€.
β μμ : νμ¨μ΄ν°
νμ¨μ΄ν°μλ μ°ν, μ€ν¬λ¦°, μ‘°λͺ , νλ‘μ ν°, νμ½ κΈ°κ³ λ±μ΄ μ‘΄μ¬νλ€.
ν΄λμ€κ° λ§€μ° λ§λ€. μ΄ ν΄λμ€λ€μ μλ‘ λ³΅μ‘νκ² μ°κ²°λμ΄ μκ³ , μ λλ‘ μ°λ €λ©΄ λ§μ λΉμ©μ΄ λ λ€.
μνλ₯Ό λ³΄λ €λ©΄
popconPopper.on(); // νμ½ κΈ°κ³λ₯Ό μΌ λ€.
popconPopper.pop(); // νμ½ νκΈ΄λ€.
light.dim(20); // μ‘°λͺ
μ 20% λ‘ μ‘°μ νλ€.
screen.down(); // μ€ν¬λ¦°μ λ΄λ¦°λ€
amp.on(); // μ°νλ₯Ό μΌ λ€
streamingPlayer.play(); // νλ μ΄μ΄λ₯Ό μ€νμν¨λ€
// κΈ°ν λμ (νλ μ΄μ΄ μΌμμ μ§, μ‘°λͺ
μ‘°μ , λ³Όλ₯¨ μ‘°μ λ±)
μνλ₯Ό λκ³ μ 리νλ €λ©΄, μμ λμλ€μ μ λΆ μμμΌλ‘ ν΄μΌν κΉ? νμ¨μ΄ν° μμ€ν μ κΈ°λ₯ μΆκ°λ± μ κ·Έλ μ΄λ λλ€λ©΄?
νμ¨μ΄ν°λ‘ μνλ₯Ό 보λ κ²μ΄ μλλΌ, TV μμ²μ΄λ λΌλμ€ μ²μ·¨λ₯Ό νλ€λ©΄ μ΄λ κ² λ³΅μ‘ν κ³Όμ μ ν΄μΌνλκ°?
μ΄λ΄λ νμ¬λ ν¨ν΄μ μ΄μ©νμ.
μΈν°νμ΄μ€λ₯Ό λ¨μν μν€κΈ° μν΄ μΈν°νμ΄μ€λ₯Ό λ³κ²½νλ€.
(1) νμ¨μ΄ν°λ₯Ό ꡬμ±νλ κ°μ²΄λ€
/** μ°ν */
public class Amplifier {
String description;
Tuner tuner;
StreamingPlayer player;
public Amplifier(String description) {
this.description = description;
}
public void on() { System.out.println(description + " μ μ on"); }
public void off() { System.out.println(description + " μ μ off"); }
public void setStereoSound() { System.out.println(description + " μ€ν
λ μ€ λͺ¨λ on"); }
public void setSurroundSound() { System.out.println(description + " μλΌμ΄λ λͺ¨λ on"); }
public void setVolume(int level) { System.out.println(description + " λ³Όλ₯¨ μ€μ " + level);}
public void setTuner(Tuner tuner) {
System.out.println(description + " νλ μ€μ " + tuner);
this.tuner = tuner;
}
public void setStreamingPlayer(StreamingPlayer player) {
System.out.println(description + " set μ€νΈλ¦¬λ° νλ μ΄μ΄ " + player);
this.player = player;
}
@Override
public String toString() {
return "Amplifier{" + "description='" + description + '\'' + '}';
}
}
/** CD νλ μ΄μ΄ */
public class CdPlayer {
String description;
int currentTrack;
Amplifier amplifier;
String title;
public CdPlayer(String description, Amplifier amplifier) {
this.description = description;
this.amplifier = amplifier;
}
public void on() { System.out.println(description + " on"); }
public void off() { System.out.println(description + " off"); }
public void eject() {
title = null;
System.out.println(description + " eject");
}
public void play(String title) {
this.title = title;
currentTrack = 0;
System.out.println(description + " playing \"" + title + "\"");
}
public void play(int track) {
if (title == null) {
System.out.println(description + " μ¬μ λΆκ° " + currentTrack + " cd μμ");
} else {
currentTrack = track;
System.out.println(description + " μ¬μ μ€ " + currentTrack);
}
}
public void stop() {
currentTrack = 0;
System.out.println(description + " μ μ§!!");
}
public void pause() { System.out.println(description + " μΌμμ μ§ \"" + title + "\""); }
@Override
public String toString() {
return "CdPlayer{" + "description='" + description + '\'' + '}';
}
}
/** νμ½ κΈ°κ³ */
public class PopcornPopper {
String description;
public PopcornPopper(String description) {
this.description = description;
}
public void on() { System.out.println(description + " on"); }
public void off() { System.out.println(description + " off"); }
public void pop() { System.out.println(description + " νμ½ νκΈ°κΈ°!!"); }
@Override
public String toString() {
return "PopcornPopper{" + "description='" + description + '\'' + '}';
}
}
/** νλ‘μ ν° */
public class Projector {
String description;
StreamingPlayer player;
public Projector(String description, StreamingPlayer player) {
this.description = description;
this.player = player;
}
public void on() { System.out.println(description + " μ μ on"); }
public void off() { System.out.println(description + " μ μ off"); }
public void wideScreenMode() { System.out.println(description + " μμ΄λ μ€ν¬λ¦° λͺ¨λ (16x9)"); }
public void tvMode() { System.out.println(description + " tv λͺ¨λ (4x3)"); }
@Override
public String toString() {
return "Projector{" + "description='" + description + '\'' + '}';
}
}
/** μ€ν¬λ¦° */
public class Screen {
String description;
public Screen(String description) {
this.description = description;
}
public void up() { System.out.println(description + " μ¬λ¦¬κΈ°!"); }
public void down() { System.out.println(description + " λ΄λ¦¬κΈ°!"); }
@Override
public String toString() {
return "Screen{" + "description='" + description + '\'' + '}';
}
}
/** μ¬μ νλ μ΄μ΄ */
public class StreamingPlayer {
String description;
int currentChapter;
Amplifier amplifier;
String movie;
public StreamingPlayer(String description, Amplifier amplifier) {
this.description = description;
this.amplifier = amplifier;
}
public void on() { System.out.println(description + " μ μ on"); }
public void off() { System.out.println(description + " μ μ off"); }
public void play(String movie) {
this.movie = movie;
currentChapter = 0;
System.out.println(description + " playing \"" + movie + "\"");
}
public void stop() {
currentChapter = 0;
System.out.println(description + " μ μ§ \"" + movie + "\"");
}
public void pause() {
System.out.println(description + " μΌμμ€μ§ \"" + movie + "\"");
}
public void setTwoChannelAudio() { System.out.println(description + " set 2ch μ€ν
λ μ€ μ€λμ€"); }
public void setSurroundAudio() { System.out.println(description + " set μλΌμ΄λ μ€λμ€"); }
@Override
public String toString() {
return "StreamingPlayer{" + "description='" + description + '\'' + '}';
}
}
/** μ‘°λͺ
*/
public class TheaterLights {
String description;
public TheaterLights(String description) {
this.description = description;
}
public void on() { System.out.println(description + " μ μ on"); }
public void off() { System.out.println(description + " μ μ off"); }
public void dim(int level) { System.out.println(description + " μ‘°μ to " + level + " %"); }
@Override
public String toString() {
return "TheaterLights{" + "description='" + description + '\'' + '}';
}
}
/** νλ */
public class Tuner {
String description;
Amplifier amplifier;
double frequency;
public Tuner(String description, Amplifier amplifier) {
this.description = description;
this.amplifier = amplifier;
}
public void on() { System.out.println(description + " μ μ on"); }
public void off() { System.out.println(description + " μ μ off"); }
public void setFrequency(double frequency) {
System.out.println(description + " μ£Όνμ μ€μ " + frequency);
this.frequency = frequency;
}
public void setAm() { System.out.println(description + " set AM mode"); }
public void setFm() { System.out.println(description + " set FM mode"); }
@Override
public String toString() {
return "Tuner{" + "description='" + description + '\'' + '}';
}
}
(2) νμ¬λ λ§λ€κΈ°
public class HomeTheaterFacade {
Amplifier amplifier;
CdPlayer cdPlayer;
PopcornPopper popcornPopper;
Projector projector;
Screen screen;
StreamingPlayer streamingPlayer;
TheaterLights theaterLights;
Tuner tuner;
// constructor
public HomeTheaterFacade(Amplifier amplifier, CdPlayer cdPlayer,
PopcornPopper popcornPopper, Projector projector, Screen screen,
StreamingPlayer streamingPlayer, TheaterLights theaterLights, Tuner tuner) {
this.amplifier = amplifier;
this.cdPlayer = cdPlayer;
this.popcornPopper = popcornPopper;
this.projector = projector;
this.screen = screen;
this.streamingPlayer = streamingPlayer;
this.theaterLights = theaterLights;
this.tuner = tuner;
}
public void watchMovie(String movie) {
System.out.println("===== μν 보기 =====");
popcornPopper.on();
popcornPopper.pop();
theaterLights.dim(10);
screen.down();
projector.on();
projector.wideScreenMode();
amplifier.on();
amplifier.setStreamingPlayer(streamingPlayer);
amplifier.setSurroundSound();
amplifier.setVolume(5);
streamingPlayer.on();
streamingPlayer.play(movie);
}
public void endMovie() {
System.out.println("===== μν λ =====");
popcornPopper.off();
theaterLights.on();
screen.up();
projector.off();
amplifier.off();
streamingPlayer.stop();
streamingPlayer.off();
}
public void listenToRadio(double frequency) {
System.out.println("===== λΌλμ€ λ£κΈ° =====");
tuner.on();
tuner.setFrequency(frequency);
amplifier.on();
amplifier.setVolume(6);
amplifier.setTuner(tuner);
}
public void endRadio() {
System.out.println("===== λΌλμ€ μ’
λ£ =====");
tuner.off();
amplifier.off();
}
}
(3) μ€ν
@Test
void homeTheaterPlayMovie() {
Amplifier amplifier = new Amplifier("μ°ν");
Tuner tuner = new Tuner("Am/Fm νλ", amplifier);
StreamingPlayer streamingPlayer = new StreamingPlayer("μ€νΈλ¦¬λ° νλ μ΄μ΄", amplifier);
CdPlayer cdPlayer = new CdPlayer("CD νλ μ΄μ΄", amplifier);
Projector projector = new Projector("νλ‘μ ν°", streamingPlayer);
TheaterLights theaterLights = new TheaterLights("μ‘°λͺ
λΌμ΄νΈ");
Screen screen = new Screen("μ€ν¬λ¦°");
PopcornPopper popcornPopper = new PopcornPopper("νμ½ νν!");
// νμ¬λ μμ±
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(amplifier, cdPlayer, popcornPopper, projector,
screen, streamingPlayer, theaterLights, tuner);
// μν보기
homeTheaterFacade.watchMovie("κΈ°μμΆ©");
homeTheaterFacade.endMovie();
}
@Test
void homeTheaterPlayRadio() {
Amplifier amplifier = new Amplifier("골λ μ°ν");
Tuner tuner = new Tuner("λΌλμ€ νλ", amplifier);
StreamingPlayer streamingPlayer = new StreamingPlayer("μ¬μ νλ μ΄μ΄", amplifier);
CdPlayer cdPlayer = new CdPlayer("CD player", amplifier);
Projector projector = new Projector("μ½€ν©νΈ νλ‘μ ν°", streamingPlayer);
TheaterLights theaterLights = new TheaterLights("ν΄λμ© μ‘°λͺ
");
Screen screen = new Screen("ν΄λμ© μ€ν¬λ¦°");
PopcornPopper popcornPopper = new PopcornPopper("νμ½!");
// νμ¬λ μμ±
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(amplifier, cdPlayer, popcornPopper, projector,
screen, streamingPlayer, theaterLights, tuner);
// λΌλμ€ λ£κΈ°
homeTheaterFacade.listenToRadio(107.7);
homeTheaterFacade.endRadio();
}
(4) κ²°κ³Ό νμΈ
===== μν 보기 =====
νμ½ νν! on
νμ½ νν! νμ½ νκΈ°κΈ°!!
μ‘°λͺ
λΌμ΄νΈ μ‘°μ to 10 %
μ€ν¬λ¦° λ΄λ¦¬κΈ°!
νλ‘μ ν° μ μ on
νλ‘μ ν° μμ΄λ μ€ν¬λ¦° λͺ¨λ (16x9)
μ°ν μ μ on
μ°ν set μ€νΈλ¦¬λ° νλ μ΄μ΄ StreamingPlayer{description='μ€νΈλ¦¬λ° νλ μ΄μ΄'}
μ°ν μλΌμ΄λ λͺ¨λ on
μ°ν λ³Όλ₯¨ μ€μ 5
μ€νΈλ¦¬λ° νλ μ΄μ΄ μ μ on
μ€νΈλ¦¬λ° νλ μ΄μ΄ playing "κΈ°μμΆ©"
===== μν λ =====
νμ½ νν! off
μ‘°λͺ
λΌμ΄νΈ μ μ on
μ€ν¬λ¦° μ¬λ¦¬κΈ°!
νλ‘μ ν° μ μ off
μ°ν μ μ off
μ€νΈλ¦¬λ° νλ μ΄μ΄ μ μ§ "κΈ°μμΆ©"
μ€νΈλ¦¬λ° νλ μ΄μ΄ μ μ off
===== λΌλμ€ λ£κΈ° =====
λΌλμ€ νλ μ μ on
λΌλμ€ νλ μ£Όνμ μ€μ 107.7
골λ μ°ν μ μ on
골λ μ°ν λ³Όλ₯¨ μ€μ 6
골λ μ°ν νλ μ€μ Tuner{description='λΌλμ€ νλ'}
===== λΌλμ€ μ’
λ£ =====
λΌλμ€ νλ μ μ off
골λ μ°ν μ μ off
νμ¬λλ₯Ό λ§λ€μ΄ μλΈν΄λμ€λ€μ ν΅ν©μν¨ κ³Όμ μ κ·Έλ¦ΌμΌλ‘ νννλ©΄ λ€μκ³Ό κ°λ€.
νμ¬λ ν΄λμ€λ μλΈ ν΄λμ€λ€μ μΊ‘μν νλ κ²μ΄ μλλ€. κ·Έλ₯ μλΈ ν΄λμ€λ€μ κΈ°λ₯μ μ¬μ©ν μ μλ κ°λ¨ν μΈν°νμ΄μ€λ₯Ό μ 곡νλ κ² λΏμ΄λ€. λ°λΌμ ν΄λΌμ΄μΈνΈμμ νΉμ μλΈμμ€ν ν΄λμ€λ₯Ό κ·Έλ₯ μ¬μ©ν μλ μλ€.
νμ¬λλ ν¨ν΄μ μ¬μ©νλ©΄ μΈν°νμ΄μ€λ₯Ό λ¨μνμν¬ λΏ μλλΌ ν΄λΌμ΄μΈνΈ ꡬνκ³Ό μλΈμμ€ν μ λΆλ¦¬μν¬ μ μλ€. μ΄ λλ¬Έμ νμ¨μ΄ν°μ κΈ°κ³κ° λ°λμ΄λ, ν΄λΌμ΄μΈνΈλ μμ νμ§μκ³ νμ¬λλ§ μμ ν¨μΌλ‘μ¨ κΈ°κ³ κ΅νμ΄ κ°λ₯ν΄μ§λ€.
β μ΅μ μ§μ μμΉ
볡μ‘νκ³ λ§μ μλΈ ν΄λμ€λ€ (νμ¨μ΄ν°μ κ°μ’ κΈ°κΈ°λ€) μ λ¨μννκ³ ν΅ν©ν ν΄λμ€λ₯Ό λ§λ€μ΄ νμ¬λ ν¨ν΄μ μμ±νλ€.
μ΄μ ν΄λΌμ΄μΈνΈμ μλΈ μμ€ν μ΄ μλ‘ κΈ΄λ°νκ² μ°κ²°λμ§ μμλ λκ³ , μ΅μ μ§μ μμΉμ μ€μνλλ° λμμ μ€λ€.
π μ΅μ μ§μ μμΉ
μ λ§ μΉν μΉκ΅¬νκ³ λ§ μκΈ°νλΌ!
μ΄λ€ κ°μ²΄λ κ·Έ κ°μ²΄μ μνΈμμ©νλ ν΄λμ€μ κ°μμ μ£Όμν΄μΌνλ©°, μ΄λ€μμΌλ‘ μνΈμμ©νλμ§λ μ£Όμλ₯Ό κΈ°μΈμ¬μΌ νλ€λ μλ―Έμ΄λ€.
μ¦, μ΄λ€ λ©μλμμλ μ§ μλμ λ€ μ’ λ₯μ κ°μ²΄ λ©μλλ§ νΈμΆνμ!
- κ°μ²΄ μ체
- λ©μλμ 맀κ°λ³μλ‘ μ λ¬λ κ°μ²΄
- κ·Έ λ©μλμμ μμ±νκ±°λ μΈμ€ν΄μ€λ₯Ό λ§λ κ°μ²΄
- κ·Έ κ°μ²΄μ μνλ ꡬμ±μμ
Without the Principle
public float getTemp() {
// stationμΌλ‘λΆν° ThermometerλΌλ κ°μ²΄λ₯Ό λ°μλ€μ
Thermometer thermometer = station.getThermometer();
// κ·Έ κ°μ²΄μ getTemperature()λ©μλλ₯Ό μ§μ νΈμΆ
return thermometer.getTemperature();
}
With the Principle
public float getTemp() {
// Station ν΄λμ€μ thermometerμ μμ²μ ν΄μ£Όλ λ©μλλ₯Ό μΆκ°
// μ΄λ κ²νλ©΄ μμ‘΄ν΄μΌνλ ν΄λμ€μ κ°μλ₯Ό μ€μΌμ μλ€.
return station.getTemperature();
}
λ©μλ νΈμΆμ λ°μ΄λ리 μμμ μ μ§νλλ‘ νμ.
public class Car {
// μ΄ ν΄λμ€μ ꡬμ±μμ, μ΄ κ΅¬μ±μμμ λ©μλλ νΈμΆ OK
Engine engine;
// λ€λ₯Έ μΈμ€ν΄μ€ λ³μλ€...
public Car() { // Constructor
// μΈμ€ν΄μ€ λ³μλ€ μ΄κΈ°ν
}
// 맀κ°λ³μλ‘ μ λ¬λ κ°μ²΄(Key)μ λ©μλλ νΈμΆ OK
public void start(Key key) {
// μλ‘μ΄ κ°μ²΄ μμ±. μ΄ κ°μ²΄μ λ©μλλ νΈμΆ OK
Doors doors = new Doors();
// 맀κ°λ³μλ‘ μ λ¬λ κ°μ²΄(Key)μ λ©μλλ νΈμΆ OK
boolean authorized = key.turns();
if(authorized) {
engine.start(); // ꡬμ±μμμ κ°μ²΄μ λ©μλλ νΈμΆ OK
updateDashboardDisplay(); // κ°μ²΄ λ΄μ λ‘컬 λ©μλ νΈμΆ OK
doors.lock(); // μ§μ μμ±νκ±°λ μΈμ€ν΄μ€λ₯Ό λ§λ κ°μ²΄μ λ©μλ νΈμΆ OK
}
}
public void updateDashboardDisplay() {
// μ
λ°μ΄νΈ display λ΄μ©
}
}
β UML Diaglam
μ°Έκ³
ν€λ νΌμ€νΈ λμμΈ ν¨ν΄ (Head First Design Patterns) - μλ¦ ν리먼,μ리μλ² μ€ λ‘μ¨,μΌμ΄μ μμλΌ,λ²νΈ λ² μ΄μΈ