D
D
driverx182019-06-18 23:27:56
Design patterns
driverx18, 2019-06-18 23:27:56

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

1 answer(s)
O
Oleg, 2019-06-18
@402d

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 question

Ask a Question

731 491 924 answers to any question