Head First Design Patterns Reading Notes and C ++ code (1): Observer (Observer) Mode
Su WeiminHttp://www.gisdev.cn/Http://blog.csdn.net/suen/Date: 2008-9-18
All copyrights reserved. If you need to reprint the information, contact the author and indicate the source in a conspicuous position.
Head First Design Patterns is a classic book on Design Patterns written by Kathy sield and Bert Bates. I recently found this book to review the Design Patterns. This book is characterized by software design principles and design patterns based on actual examples. It is very vivid and worth reading. However, the Code in this book is Java, which is inconvenient for the Corder that we developed with C series code. Therefore, in the course of learning, I gradually changed the code of this book into C ++ code in VS2008 development tools, which is attached to the back of Reading Notes, hoping to help you.
The first chapter of this book uses a Duck example to illustrate the basic principles of OO design: (1) Identify potential changes in applications and separate them, do not mix it with the code that does not need to be changed; (2) programming for the interface, rather than for the implementation.
The second chapter focuses on the Observer (Observer) mode. It is used as an example of an Internet Meteorological Observatory. For the physical data obtained from the Meteorological Station, we use three billboards to display the current situation (temperature, humidity, pressure), meteorological statistics, and weather forecasts respectively. The main requirement of this system is that when the weather station data is updated, the three bulletin boards can be updated simultaneously. This system may change in the future by adding more types of boards, and the existing boards may also be abolished.
This is a typical application scenario of the observer mode. The observer mode defines one-to-multiple dependencies between objects. In this way, when an object changes its state, all its dependent persons will receive notifications and update them automatically. Similar application scenarios include publishing houses and subscribers. The class diagram of the observer mode is as follows:
Http://www.gisdev.cn/space/upload/2008/09/18/75174069164.jpg
The design class diagram of the weather station example is as follows:
Http://www.gisdev.cn/space/upload/ 2008/09/18 /79690789415.jpg
Attachment:It is my c ++ code based on vs2008 development tool.
You can download it at the following URL:
Http://www.gisdev.cn/showtopic-109.aspx