Answer the question
In order to leave comments, you need to log in
What is a real-life example from the Observer pattern projects?
I read about the "observer", made sketches for fun, but I still don't quite understand how it can be used in projects and where. Can someone describe where you use Observer in projects so that it's not a pattern for the sake of a pattern, but a really beneficial application?
Answer the question
In order to leave comments, you need to log in
Simplified, through this class, automatic printing of new files in the directory is made.
public class MyFileObserver extends Service {
void doprint(Uri uri){
// полезное действие
}
public void handleStart(Intent intent) {
...
Observer o = new Observer();
o.startWatching();
...
}
private class Observer extends FileObserver {
public void onEvent(int event, String file) {
if (file != null && (!file.equals(".probe"))) {
if ((event == FileObserver.CLOSE_WRITE) || (event == FileObserver.MOVED_TO )) {
fo.doprint(uri);
}
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question