package Observer;
public class Test {
* client Test category
* The observer model generally consists of four parts :
* 1 Summary observer ( Textbooks are called general “Subject( The subject of abstract classes )”),
* It generally defines abstract add(),remove(),notifyAll() Other methods ,
* For adding observers , Delete observer , Inform the observer of his own changes .
* 2 Detailed class of the observed ( It's called... In general textbooks “ConcreteSubject( Detailed topic classes )”),
* It's the abstract observed (Subject) The detailed implementation class of , It's realized add(),remove(),notifyAll() Other methods ;
* 3 Abstract Observer class ( It's called “Observer”), It generally defines an abstract update() Method .
* 4 Detailed observer classes ( It's called “ConcreteObserver”), He's an abstract Observer class (Observer) Detailed implementation of , It has achieved update() Method ,
* Used to accept detailed changes from the observed ;
* Here's an example : There are two subjects, Chen Meijia and Hu Yifei , They have different observers . Chen Meijia's observer is LV Xiaobu ,
* Hu Yifei's observers are Zeng Xianer and Zhang Wei , When Chen Meijia and Hu Yifei have any changes , Immediately they will be told by their observers that they know ;
* @author wl
public static void main(String[] args) {
Watched HuYiFei=new ConcerteWatched();
Watched ChenMeiJia=new ConcerteWatched(); Watcher LvXiaoBu=new ConcreteWatcher(" Lu Xiaobu "," Chen Meijia ");
Watcher ZengXianEr=new ConcreteWatcher(" Zeng Xianer "," Hu Yifei ");
Watcher ZhangWei=new ConcreteWatcher(" Zhang Wei "," The goddess "); ChenMeiJia.addWatcher(LvXiaoBu);
HuYiFei.addWatcher(ZhangWei); HuYiFei.notifyWatchers(" I really want to see a movie ! .! "); ChenMeiJia.notifyWatchers(" I really want to travel !!!"); } }

package Observer;
* Abstract observer interface class
* @author wl
public interface Watcher {
void updata(String str);
package Observer;
* The abstract observable interface
* @author wl
public interface Watched {
void addWatcher(Watcher watcher);
void removeWatcher(Watcher watcher);
void notifyWatchers(String str);
package Observer;
* Detailed observer classes , Take LV Xiaobu in the example . Zeng Xianer , Zhang Wei is an example of the observer
* @author wl
public class ConcreteWatcher implements Watcher {
String watcheredName;
String name; public ConcreteWatcher(String name,String watcheredName){;
public void updata(String str) {
System.out.println(name+" I'm secretly happy “ Hey !!. The opportunity came , my "+watcheredName+" Say she ‘"+str+"’”");
} }
package Observer;
import java.util.ArrayList;
import java.util.List;
* Detailed class of the observed , Take Hu Yifei in the example , Chen Meijia is a detailed example of the observed
* @author wl
public class ConcerteWatched implements Watched {
List<Watcher> list=new ArrayList<Watcher>(); @Override
public void addWatcher(Watcher watcher) {
} @Override
public void removeWatcher(Watcher watcher) {
} @Override
public void notifyWatchers(String str) {
for(Watcher watcher:list){
} }

Output is :

Zeng xian'er was secretly happy “ Hey !!

The opportunity came , My hu Yifei said that she ‘ I really want to see a movie !


Zhang Wei was secretly happy “ Hey !


The opportunity came , My goddess said she ‘ I really want to see a movie !!.’”

LV Xiaobu was secretly happy “ Hey !

. A chance , My Ms. Chen said , she ‘ I really want to travel !!!’”

Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .

java Design patterns : More about the observer model

  1. java Design patterns -- Observer mode (Observer)

    java Design patterns -- Observer mode (Observer) java Design patterns -- Observer mode (Observer) Definition of observer pattern : Defines a one-to-many dependency between objects . When the state of an object changes , All that depends on it ...

  2. JAVA Design patterns Observer mode

    purpose Observer mode (Observer) The observer pattern defines a one to many dependency , Have multiple observer objects listen to a topic object at the same time . This topic object changes as the state changes , All observer objects will be notified , So that they can update themselves automatically . view ...

  3. Java Design patterns -- Observer mode to listener

    The observer pattern is the behavior pattern of an object . It's also called publishing - A subscription model . Model - View mode . Source - Monitor mode . Abstract theme role : Theme roles store all references to observer objects in a collection , Each topic can have any number of observers . Abstract themes provide a ...

  4. Java Design patterns の Observer mode ( Push pull model )

    Catalog : One . The observer defines Two . The structure of the observer pattern ( Push mode implementation ) 3、 ... and . Push model and pull model ( Pull mode implementation ) Four .JAVA Support for the observer model 5、 ... and . Use JAVA Support for the observer model ( With push mode implementation example ) One . The observer ...

  5. Java Design patterns - Observer mode

    Definition The observer pattern belongs to the object behavior pattern . Define one to many dependencies between objects , So when an object changes state , Objects that depend on it are notified and automatically updated . advantage 1.  Abstract coupling between subject and observer . No matter what object mainly implements a specific ...

  6. my Java Design patterns - Observer mode

    I believe everyone has seen < Joy and grey wolf >, It's about grey wolf and sheep " struggle ", And the result of every time is that the wolf flies into the sky , With a sentence " I'll be back ......". Feel for the big wolf ...

  7. java Design patterns - Observer mode learning

    Recently I learned about the observer pattern in design patterns , Record the learning achievements here . Observer mode , Personal understanding : It's a one to many model , A subject does something , Other subjects can be observed . It's just that the subject can decide who will observe him , And what you can do for others ...

  8. Java Design patterns —— Observer mode ( Event monitoring )

    Recently in to see Tomcat and Spring Source code , Registered all kinds of Listener, The event is executed when it is triggered , Here we use the observer pattern in the design pattern . lead -GUI Event listening in Think about learning before Java Of GUI programming ...

  9. JAVA Design patterns — The observer pattern and Reactor Reactor mode

    Observed ( The theme ) Interface Define the subject object interface /** Abstract theme role : When the subject object changes state , All observer objects will be notified It's also called event object */ public interface Subject { // Add a ...

  10. Java Design patterns ---- Detailed explanation of the observer model

    [ Statement ] Welcome to reprint , But please keep the original source of the article →_→ Life one : Source of the article : ...

Random recommendation

  1. [Python note ] Sequence ( One ) Indexes 、 Fragmentation

    Python contain 6 Intraspecific sequence : list . Tuples . character string .Unicode character string .buffer object .xrange object . These sequences support common operations : Indexes The index is from 0 Start counting : When index value is negative , From the last element ( ...

  2. OpenCV Text rotation correction based on Fourier transform

    Fourier transform can be used to transform an image from time domain to frequency domain , For the text of the branch , There must be some characteristics in its frequency spectrum , When the image rotates , Its spectrum also rotates synchronously , So find out the dip angle of this feature , You can rotate the image back . Let's do a Fourier analysis of the original image first ...

  3. To relive WCF To build a simple WCF( One )(1) Through the console and IIS Boarding Services

    One . Understand what is WCFWCF Namely .NET The integration of various distributed technologies under the platform , And provides a set of unified programming interface Two .WCF The definition of WCF(Windows Communication Foundation) It's Microsoft building for ...

  4. Examples of cross site scripting (AppScan Scan results )

    The recent work requires solving the problem web The vulnerability of the project , Scanning for vulnerabilities is useful AppScan Tools , Many of them are about cross site scripting . Now let's share this . Original article , Reprint please indicate ------------------ ...

  5. dell vfoglight

    vFoglight in the light of RH/Vmware/Hyper-v/Vsphere Environment related software package download link ...

  6. Oracle“ It's not equal to the sign ” And Nulls The situation of ( turn )

    Today I suddenly found out that ,Oracle Medium “ It's not equal to the operator ” Is to ignore null Of . such as , Inquire about comm Not equal to 500 The record of , I'll take it for granted where comm != 500 It is expected that the return contains null It's not equal to 500 Of ...

  7. About ie Of h5 Refresh and ctrl+5 Refresh And the problem of icon refresh

    I'm doing a form validation recently , Trigger an error when the form loses focus . But ..... firefox Auburn There's no problem with refreshing , And in the ie edge use f5 When refreshing Can remember the previous focus Cause confusion . The problem is the solution : < ...

  8. Java8 Optional class

    summary up to now , The famous NullPointerException It is caused by Java The most common cause of application failure . In the past , To solve the null pointer exception ,Google The company is famous for Guava The project introduced Optional class ,Guav ...

  9. Effective Java The third edition ——68. Follow the generally accepted naming convention

    Tips The source code address in the book : Be careful , Some of the code in the book is based on Java 9 API Medium , the ...

  10. Mysql select Statement to set the default value

    1. Without setting a default value : SELECT, user_name, role, adm_regionid, region_name , create_time FROM us ...