728x90
반응형
01.옵저버패턴으로 적용
01.1 Subcriber
public interface Subscriber {
void handleMessage(String message);
}
01.2 User
public class User implements Subscriber {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public void handleMessage(String message) {
System.out.println(message);
}
}
01.3 ChatServer
- before
-
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ChatServer { private Map<String, List<String>> messages; public ChatServer() { this.messages = new HashMap<>(); } public void add(String subject, String message) { if (messages.containsKey(subject)) { messages.get(subject).add(message); } else { List<String> messageList = new ArrayList<>(); messageList.add(message); messages.put(subject, messageList); } } public List<String> getMessage(String subject) { return messages.get(subject); } }
- after
- before
-
public class Client { public static void main(String[] args) { ChatServer chatServer = new ChatServer(); User user1 = new User(chatServer); user1.sendMessage("디자인패턴", "이번엔 옵저버 패턴입니다."); user1.sendMessage("롤드컵2021", "LCK 화이팅!"); User user2 = new User(chatServer); System.out.println(user2.getMessage("디자인패턴")); user1.sendMessage("디자인패턴", "예제 코드 보는 중.."); System.out.println(user2.getMessage("디자인패턴")); } }
- after
-
package me.whiteship.designpatterns._03_behavioral_patterns._19_observer._02_after; public class Client { public static void main(String[] args) { ChatServer chatServer = new ChatServer(); User user1 = new User("keesun"); User user2 = new User("whiteship"); chatServer.register("오징어게임", user1); chatServer.register("오징어게임", user2); chatServer.register("디자인패턴", user1); chatServer.sendMessage(user1, "오징어게임", "아.. 이름이 기억났어.. 일남이야.. 오일남"); chatServer.sendMessage(user2, "디자인패턴", "옵저버 패턴으로 만든 채팅"); chatServer.unregister("디자인패턴", user2); chatServer.sendMessage(user2, "디자인패턴", "옵저버 패턴 장, 단점 보는 중"); } }
01.4 client
-
import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ChatServer { private Map<String, List<Subscriber>> subscribers = new HashMap<>(); public void register(String subject, Subscriber subscriber) {// 등록 if (this.subscribers.containsKey(subject)) { this.subscribers.get(subject).add(subscriber); } else { List<Subscriber> list = new ArrayList<>(); list.add(subscriber); this.subscribers.put(subject, list); } } public void unregister(String subject, Subscriber subscriber) {// 해지 if (this.subscribers.containsKey(subject)) { this.subscribers.get(subject).remove(subscriber); } } public void sendMessage(User user, String subject, String message) {//상태변경 기능 if (this.subscribers.containsKey(subject)) { String userMessage = user.getName() + ": " + message; this.subscribers.get(subject).forEach(s -> s.handleMessage(userMessage)); } } }
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2022-07-18-옵저버패턴-3부-장점과단점 (0) | 2022.07.18 |
---|---|
2022-07-18-옵저버패턴-1부-패턴소개 (0) | 2022.07.18 |
2022-07-11-중재자패턴-3부-장점과단점 (0) | 2022.07.18 |
2022-07-11-중재자패턴-2부-패턴적용하기 (0) | 2022.07.18 |
2022-07-11-중재자패턴-1부-패턴소개 (0) | 2022.07.18 |
댓글