-
[Java] ๋์์ธ ํจํด(Mediator ํจํด)Android/Java 2021. 5. 18. 00:14๋ฐ์ํ
Mediator ํจํด์ด๋?
Mediator ๋ง ๊ทธ๋๋ก ์ค์ฌ์๋ก์, ํด๋์ค๊ฐ ๋ค๋ฅธ ํด๋์ค์์ ์ฐ๊ฒฐ์ฑ์ ์ง์ ํ๋ ๊ฒ์ด ์๋ ์ค์ฌ์๋ฅผ ํตํด ์ฐ๊ฒฐํ๋ ํจํด์ ๋๋ค.
ํ์ฌ์ ์๊ธ์๊ฐ ๋น์๋ฅผ ํตํด ๋ค๋ฅธ ์ง์๋ค์๊ฒ ์๊ธ์์ ์๊ฒฌ์ ๋ชจ๋์๊ฒ ์ ๋ฌํ๋ฏ, ํด๋์ค๊ฐ ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋์๊ฒ ์ผ์ผ์ด ์ ๋ฌํ๋ ๊ฒ์ด ์๋ Mediator๊ฐ ์ํํ๋๋ก ๋ง๋๋ ํจํด์ ๋๋ค.
Mediator ํจํด์ ์ค๋ช ํ๊ธฐ ์ ์ Mediator ์ฌ์ฉํ๊ธฐ ์ ์ ์ฝ๋์ ๋ถํธํจ์ ์ ์๊ณ ๋๋ฉด ๋ ์ดํดํ๊ธฐ ์ฝ์ต๋๋ค.
public interface Subject { void isTimetoSubject(String type); }
Subject๋ผ๋ ์ธํฐํ์ด์ค๋ type์ ํด๋นํ๋ ๊ณผ๋ชฉ์ ๋ฃ๋ ์๊ฐ์ด ๋ง๋์ง์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๊ตฌํ๋๋ isTimetoSubject ๋ฉ์๋๋ฅผ ๊ตฌํํ๋๋ก ๊ฐ์ ํ๊ณ ์์ต๋๋ค.
public class Math implements Subject{ @Override public void isTimetoSubject(String type) { if(type.equals("Math")){ System.out.println("It's time to study math"); }else{ System.out.println("It's not time to study math"); } } } public class Korean implements Subject{ @Override public void isTimetoSubject(String type) { if(type.equals("Korean")){ System.out.println("It's time to study korean"); }else{ System.out.println("It's not time to study korean"); } } } public class English implements Subject { @Override public void isTimetoSubject(String type) { if(type.equals("English")){ System.out.println("It's time to study english"); }else{ System.out.println("It's not time to study english"); } } }
๋ค์ ํด๋์ค๋ค์ Subject ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค Math, Korean, English ์ ๋๋ค. ๊ฐ ํด๋์ค๋ isTimetoSubject ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ์๋กญ๊ฒ ์ ์ํ์์ต๋๋ค.
public class TimeTable { String[] types = {"Korean","Math","English"}; int idx = 0; Korean korean = new Korean(); Math math = new Math(); English english = new English(); public void onTimetoChange(){ String type = types[idx]; korean.isTimetoSubject(type); math.isTimetoSubject(type); english.isTimetoSubject(type); idx = (idx+1)%3; } }
TimeTable ํด๋์ค์ ์๊น ์์ฑํ Math, Korean ,English ํด๋์ค๋ฅผ ์์ฑํ์๊ณ , onTimetoChnage ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ํ์์ ๋ฐ๋ผ idx๊ฐ ๋ณํ๊ฒ ๋๊ณ ๊ฐ ํด๋์ค์ isTimetoSubject์ ๊ฒฐ๊ณผ๊ฐ์ด ๋ฌ๋ผ์ง๊ฒ ๋ฉ๋๋ค.
public class Main{ public static void main(String[] args) { TimeTable timeTable = new TimeTable(); timeTable.onTimetoChange(); timeTable.onTimetoChange(); /** * It's time to study korean * It's not time to study math * It's not time to study english * * It's not time to study korean * It's time to study math * It's not time to study english */ } }
๋ค์๊ณผ ๊ฐ์ด TimeTable์ด ๋ชจ๋ Subject๋ฅผ ๊ตฌํํ ํด๋์ค๋ค์ ์ง์ ๋ฉค๋ฒ๋ณ์๋ก ์์ฑํ์ฌ ๊ด๋ฆฌํ๋ค๋ฉด ์๋ก์ด ํด๋์ค ์ถ๊ฐ๋ ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ๋ ๊ฒฝ์ฐ ๋ฒ๊ฑฐ๋ก์ธ ์ ์์ต๋๋ค. ๋ํ TimeTable์ Math, English, Korean ํด๋์ค ๋ชจ๋๋ฅผ import ํ๊ธฐ ๋๋ฌธ์ ํด๋์ค๋ผ๋ฆฌ์ ๊ฒฐํฉ๋๋ ๋์์ง๊ฒ ๋ฉ๋๋ค. ์ด์ ๊ฐ์ ๊ฒฝ์ฐ Mediator ํจํด์ ์ ์ฉํ๋ฉด ๋ฌธ์ ์ ์ ์ํ์ํฌ ์ ์์ต๋๋ค.
Mediator ํจํด ์ฌ์ฉ
public class TimeMediator { ArrayList<Subject> subjects = new ArrayList<>(); public void addSubject(Subject subject){ subjects.add(subject); } public void onChangeSubject(String type){ for(Subject subject : subjects){ subject.isTimetoSubject(type); } } } public class TimeTable { TimeMediator timeMediator; String[] types = {"Korean","Math","English"}; int idx = 0; public TimeTable(TimeMediator timeMediator) { this.timeMediator = timeMediator; } public void onTimetoChange(){ timeMediator.onChangeSubject(types[idx]); idx = (idx+1) % 3; } }
TimeTable ํด๋์ค๋ ์๊น ์ ์ ์์์ ๋ค๋ฅด๊ฒ Subject๋ฅผ ๊ตฌํํ ํด๋์ค๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒ์ด ์๋ TimeMediator ํด๋์ค๋ฅผ ๋ฉค๋ฒ๋ณ์๋ก ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ onTimetoChange ๋ฉ์๋์ ์คํ์ TimeMediator์ onChangeSubject๊ฐ ์ค์ง์ ์ธ ์ญํ ์ ํ๊ณ ์์ต๋๋ค.
TimeMediator ํด๋์ค๋ Subject ์ธํฐํ์ด์ค๋ฅผ ๋ด๋ ArrayList๋ฅผ ๋ฐฐ์ด๋ก ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ addSubject ๋ฉ์๋๋ฅผ ํตํด Subject ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ค์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
Mediator ํจํด์ ์ ์ฉํ๋ฉด ์๋ก์ด Subject ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ํด๋์ค๊ฐ ์ถ๊ฐ๋๋๋ผ๋ ์ ์ฒด์ ์ธ ์ฝ๋์ ๋ณํ์์ด ์๋ก์ด ํด๋์ค์ ๋ํด์๋ง ๊ตฌํํ๋ฉด ๋ฉ๋๋ค. ๋ํ, TimeTable๋ ๊ตฌ์ฒด์ ์ธ ํด๋์ค์ ๋ํด์๋ ์์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ๊ฒฐํฉ๋๋ ๋ฎ์์ง ๊ฑธ ๋ณผ ์ ์์ต๋๋ค.
๊ฒฐ๊ณผ
public class Main{ public static void main(String[] args) { TimeMediator timeMediator = new TimeMediator(); timeMediator.addSubject(new Math()); timeMediator.addSubject(new English()); timeMediator.addSubject(new Korean()); TimeTable timeTable = new TimeTable(timeMediator); timeTable.onTimetoChange(); timeTable.onTimetoChange(); /** * It's not time to study math * It's not time to study english * It's time to study korean * * It's time to study math * It's not time to study english * It's not time to study korean */ } }
- ์ถ๊ฐ๋ก ๊ถ๊ธํ ์ ์ด๋ ์์ ํ ๋ถ๋ถ ์์ผ๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์
๋ฐ์ํ'Android > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋์์ธ ํจํด(Template ํจํด) (0) 2021.05.17 [Java] ๋์์ธ ํจํด(Proxy ํจํด) (0) 2021.05.14 [Java] ๋์์ธ ํจํด(Strategy ํจํด) (0) 2021.05.11 [Java] ๋๋ค์์ด๋? (0) 2021.03.21 [Java] Java Generic(์ ๋ค๋ฆญ) (0) 2021.03.01