Simple Application of observer Mode

Source: Internet
Author: User

One-Observer Mode

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

2. Java instance

package PostOfficeInstance;public interface ISubscribe {public void registered(Postoffice postoffice);public void unregistered(Postoffice postoffice);public void getNewspaper();}package PostOfficeInstance;import java.util.ArrayList;public class Postoffice {private ArrayList<ISubscribe>SubscribeList=new ArrayList<ISubscribe>();private boolean received;public void registeredSubscribe(ISubscribe subscribe) {SubscribeList.add(subscribe);}public void unregisteredSubscribe(ISubscribe subscribe) {if(subscribe!=null) SubscribeList.remove(subscribe);}public void setReceived(boolean received) {this.received=received;}public boolean getReceived() {return received;}public void sendNewspaper() {if(this.getReceived()==false) return;for(ISubscribe subscribe:SubscribeList) {subscribe.getNewspaper();}}}package PostOfficeInstance;public class Subscriber implements ISubscribe{private String mName;private String mNewspaperName;public Subscriber(String Name,String NewspaperName) {this.mName=Name;this.mNewspaperName=NewspaperName;}public void setName(String name) {this.mName=name;}public final String getName() {return mName;}public void setNewspaperName(String name) {this.mNewspaperName=name;}public final String getNewspaperName() {return this.mNewspaperName;}@Overridepublic void registered(Postoffice postoffice) {// TODO Auto-generated method stubpostoffice.registeredSubscribe(this);}@Overridepublic void unregistered(Postoffice postoffice) {// TODO Auto-generated method stubpostoffice.unregisteredSubscribe(this);}@Overridepublic void getNewspaper() {// TODO Auto-generated method stubSystem.out.println("I am "+this.mName);System.out.println("I received "+this.mNewspaperName);}}package PostOfficeInstance;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPostoffice postoffice=new Postoffice();Subscriber zhangsan=new Subscriber("zhangsan","Newspaper A");Subscriber lisi=new Subscriber("lisi","Newspaper B");Subscriber wangwu=new Subscriber("wangwu","Newspaper c");postoffice.registeredSubscribe(zhangsan);postoffice.registeredSubscribe(lisi);postoffice.registeredSubscribe(wangwu);postoffice.setReceived(true);postoffice.sendNewspaper();}}

 

 

Test Results

I am zhangsanI received Newspaper AI am lisiI received Newspaper BI am wangwuI received Newspaper c

 

 

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.