first, the definition
Observer mode (Observer), when the state of an object changes, all dependent objects are notified and updated automatically.
# The Observer pattern uses "combination" to combine many observers into the subject. This relationship between objects (the Observer-subject) is not generated by inheritance, but by the way the composition is used at run time. -- multi-use combination, less use inheritance!
second, the application scenario
After an event occurs, a sequence of update operations is performed. The traditional way of programming is to add the processing logic directly after the code of the event, and when the updated logic increases, the code becomes difficult to maintain. This approach is coupled, intrusive, adding new logic to the code that needs to change the subject of the event
# today it's just an observer pattern. # function needs: I need to add a project code to upload a picture function, but upload the picture after the completion of the need to do something. Save picture data to Database # Analysis: If we manipulate the code, the image path is written to the database, the format of the table is modified later, or the other fields are added, we need to be aware of the code that uploads the image, which is not conducive to the uniform maintenance of the code. # question: Can you split the two, upload the image and automatically trigger the code to store the image data, but save the picture data is not written in the logic of uploading the picture? # solution: Upload image is the operation of the library layer, storage image data is the operation of the model layer. Associate the two and call the method to add the stored picture Data Model (Observer) before the upload picture is complete.
# After uploading, the image data Model (viewer) is triggered to save the image data to the data class.
third, the realization process
Two principles
# 1. Closed and open principle for the function implementation, is closed, but the content of the function is open to the modification is closed to the extension when open # 2, multi-use combination, less use of expansion there is an association between classes and classes, and you can borrow each other's methods instead of adding more logical code classes that are not easy to extend too much, too much clutter in the hierarchy
function need: Image upload complete, save picture data to data
1<?PHP2 3 classUpload4 {5 Public functionRegister$obj)6 {7 $this->_observers[] =$obj;8 }9 Ten Public functionTrigger$data=NULL) One { A if(!Empty($this-_observers)) { - foreach($this->_observers as $obj) { - $obj-add_upload (); the } - } - - } + Public functionhtml5upload () - { + #..... A $this-trigger (); at } - } - - classPicture_model - { - Public functionadd_upload () in { - Echo' Picture storage success '; to } + } - the $upload=Newupload (); * $upload->register (NewPicture_model ()); $ $upload->html5upload ();
simple implementation of the above code
Iv. Summary
This pattern can be used when an event (logic) is complete and other associated events (logic) need to be performed.
php--Observer pattern