Java Design Patterns: Observer patterns

Source: Internet
Author: User

Package Observer;public class Test {/** * Client Test class * The Observer pattern is generally made up of four parts: * 1 abstract The Observer (the general textbook is called "Subject (abstract Subject Class)"), * inside the general definition of abstract add () , remove (), Notifyall (), and so on, are used to increase the observer, delete the Observer, and inform the observer of its own changes. * 2 specific observer classes (the general textbook is called "ConcreteSubject"), * is the concrete implementation class of the abstract Observer (Subject), which implements the Add (), remove (), Notifyall () and other methods; * 3 Abstract Observer Class (called "Observer" in the general textbook), which generally defines an abstract update () method; * 4 specific observer classes (commonly referred to as "Concreteobserver" in the textbook), He is the concrete implementation of the abstract Observer Class (Observer), which implements the update () method, which is used to accept the specific changes of the observed; * Here's an example: There are two observers Miss Monica Chen and Hu Yifi, they have different observers, and Miss Monica Chen's observers have LV, * Hu Yifi's observers were Zeng and Zhang Wei's two, and when Miss Monica Chen and Hu Yifi changed, they were immediately known to their observers; * @author WL */public static void Main (string[] args) {watched Huyifei =new concertewatched (); Watched Chenmeijia=new concertewatched (); Watcher Lvxiaobu=new concretewatcher ("LV", "Miss Monica Chen"); Watcher Zengxianer=new concretewatcher ("Zeng son", "Hu Yifi"); Watcher Zhangwei=new Concretewatcher ("Zhang Wei", "Goddess"); Chenmeijia.addwatcher (Lvxiaobu); Huyifei.addwatcher (Zengxianer); Huyifei.addwatcher (Zhangwei); Huyifei.notifywatchers ("Good to see the movie!!! "); Chenmeijia.notifywatchers ("Good want to travel AH!!!");}}
Package observer;/** * Abstract Viewer Interface class * @author WL * */public interface Watcher {void Updata (String str);}
Package observer;/** * Abstract Viewer interface * @author WL * */public interface watched {void Addwatcher (Watcher watcher); void Removewa Tcher (Watcher watcher); void Notifywatchers (String str);}
Package observer;/** * Specific observer classes, such as LV Zeng in the example, 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) {this.name=name;this.watcheredname= Watcheredname;} @Overridepublic void Updata (String str) {System.out.println (name+ "Heart secretly happy" Hey!!! The opportunity came, my "+watcheredname+" said she ' "+str+" ' "");}
Package Observer;import Java.util.arraylist;import java.util.list;/** * Specific observer classes, such as Hu Yifi in the example, Miss Monica Chen is a concrete example of the observed person * @author WL * */public class concertewatched implements watched {list<watcher> list=new arraylist<watcher> (); @ overridepublic void Addwatcher (Watcher watcher) {List.add (watcher);} @Overridepublic void Removewatcher (Watcher watcher) {list.remove (watcher);} @Overridepublic void Notifywatchers (String str) {for (watcher watcher:list) {watcher.updata (str);}}}

The output is:

Zeng Heart secretly Happy "Hey!!!" Opportunity came, my Hu Yifi said she ' good want to see the film Ah!!! ’”
Zhang Wei in the heart secretly happy "Hey!!!" Opportunity came, my Goddess said she ' good want to see the movie!!! ’”
Lu in the heart secretly happy "Hey!!!" Opportunity came, my Miss Monica Chen said she ' good want to travel AH!!! ' "

Java Design Patterns: Observer patterns

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.