Java Q & amp; A: uses the Observer Mode

Source: Internet
Author: User

Java Q & A: using the Observer Mode
A: I want to use the Observer mode in my Java program. Based on this, can some sample code be provided to demonstrate how to do this?
Q: Just as object-oriented programming helps code reuse, design patterns can facilitate design reuse. Indeed, the design pattern allows you to reuse the correct and mature designs. However, more and more voices are being criticized for design patterns recently. Critics point out that inexperienced developers can easily fall into the "pattern trap.
The pattern traps make inexperienced developers lose their way. Therefore, they are not looking for the best possible solutions when dealing with problems, but are focusing on achieving as many design patterns as possible. In some people's opinion, the use of design patterns may inevitably bring about good design. According to this logic, as long as a large number of design patterns are used, there will inevitably be a good design for you! However, in reality, this idea leads to a lot of meaningless designs-even if they adopt multiple design patterns. Unfortunately, the design model cannot guarantee a good design.
To use a design pattern correctly in the design, three conditions must be ensured:
1. Find out your problem
2. Understand this mode
3. Understand how this mode solves your problems
First, condition 1 is the most important. If you cannot fully understand the problem you want to solve, what about the application model?
Also Know condition 2: You must fully understand the mode you want to use. I don't know how it can be used? What's more, I don't even know what a model does. How can I use it?
Finally, if you cannot clearly figure out how the pattern will solve your problem (why is the pattern appropriate), give up on it. Using the pattern itself will fall into the pattern trap.
Readers who are not talking about this issue will surely fall into the pattern trap. However, from the expression of the question, it can easily mislead some developers to understand the design pattern. My understanding of this question is that this reader should be clear about the problems he/she needs to solve and understand the Observer mode. He/she just does not know how to implement it in Java.
Before giving a Java example, we will briefly introduce the Observer mode to help other readers understand it.
In simple terms, the Observer mode allows an object (Observer, Observer) to monitor another object (target, Subject ); it establishes a "publish-subscribe" (publish-subscribe) Relationship between the target and the observer. In Observer mode, the Observer can register with the target, indicating that he wants to receive events from the target. When the target needs to notify the observer of an event, it simply sends the event to each observer.
For example, there is a workbook based on a certain data model. As long as the data model changes, the workbook needs to update the table unit and embedded charts. In this example, the target is the data model, and the observer is the table unit and chart. When the observer receives a notification that the data model has changed, they update themselves.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.