Motivation (Motivation)
- In the software build process, we need to establish a "notice dependencies" for certain objects - an object (target object) changes state, all dependent objects (objects observer) will be notified. If such a dependency too closely, the software will not be well to resist change.
- Using object-oriented technology, this dependence may be weakened, and form a stable dependency. Enabling loosely coupled software architecture.
Schema Definition
One kind of inter-defining the object dependencies many relationship (variation) , so that when an object (the Subject) changes state, all dependent on it are notified and updated automatically. - "Design Patterns" GoF
Structure (Structure)
Takeaways
- Abstract object-oriented, the Observer pattern so that we can change the target and independently of the observer, so that the dependencies between the two to achieve loosely coupled.
- When the target sends a notification without specifying the viewer, notification (notification information can carry as parameters) are automatically propagated.
- The viewer to decide whether to subscribe to notifications, audience knew nothing about.
- Observer model is based UI framework in the event of a very common design patterns, it is also an important part of the MVC pattern.
The progress bar under Windows Linux console are different
class FileSplitter { String m_filePath; int m_fileNumber; the ProgressBar * m_progressBar; // 's role: specific notice Controls // replace abstract notification mechanism. Transition completed concrete to abstract / * dependent on implementation details, contrary to the design principles dependent inversion principle, layer module should not depend on low-level modules, both should depend on the abstract now to FIG strip into a GUI, if the program control Taiwan program, it indicates that the program schedule by a black dot? * / Public : FileSplitter ( const String & filePath, int filenumber, the ProgressBar * progressBar): m_filePath (filePath), m_fileNumber (filenumber), m_progressBar (progressBar) { } void Split () { @ 1 large file read // 2. Small batches write to file for ( int I = 0 ; I <m_fileNumber; I ++ ) { // ... a float progressValue = m_fileNumber; progressValue = (I + . 1 ) / progressValue; m_progressBar -> the setValue (progressValue); // update the progress bar } } }; class the MainForm: public Form1 { the TextBox * txtFilePath; the TextBox * txtFileNumber; the ProgressBar* progressBar;//进度条 public: void Button1_Click(){ string filePath = txtFilePath->getText(); int number = atoi(txtFileNumber->getText().c_str()); FileSplitter splitter(filePath, number, progressBar); splitter.split(); } };