Created with <3 with dartpad.dev.
Last active
October 12, 2022 06:59
-
-
Save junddao/850522804b4362d9b24ed6c45424c91d to your computer and use it in GitHub Desktop.
observer pattern sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
Miro miro = Miro(); | |
Student jason = Jason(); | |
Student bera = Bera(); | |
miro.subscribe(jason); | |
miro.subscribe(bera); | |
miro.speak(); | |
} | |
abstract class Teacher{ | |
void notify(String msg){} | |
void subscribe(Student s){} | |
void unsubscribe(Student s){} | |
} | |
class Miro extends Teacher{ | |
List<Student> students = []; | |
@override | |
void notify(String msg){ | |
for(var s in students){ | |
s.update(msg); | |
} | |
} | |
@override | |
void subscribe(Student s){ | |
students.add(s); | |
} | |
@override | |
void unsubscribe(Student s){ | |
students.remove(s); | |
} | |
void speak(){ | |
notify('와랏'); | |
} | |
} | |
abstract class Student{ | |
void update(String msg); | |
} | |
class Jason extends Student{ | |
@override | |
void update(String msg){ | |
print('jason: $msg'); | |
} | |
} | |
class Bera extends Student{ | |
@override | |
void update(String msg){ | |
print('Bera : $msg'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment