Skip to content

Instantly share code, notes, and snippets.

@junddao
Last active October 12, 2022 06:59
Show Gist options
  • Save junddao/850522804b4362d9b24ed6c45424c91d to your computer and use it in GitHub Desktop.
Save junddao/850522804b4362d9b24ed6c45424c91d to your computer and use it in GitHub Desktop.
observer pattern sample

observer pattern sample

Created with <3 with dartpad.dev.

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