Observer Mode (OBSERVER) (behavior mode)

Source: Internet
Author: User
Tags abstract current time

Reference Documentation:

1. Design mode-the basis of reusable object-oriented software

2.http://blog.csdn.net/zhshulin/article/details/38708351 (design mode-observer mode (OBSERVER))


Observer Mode (OBSERVER) (behavior mode)

Detailed knowledge of the theory, can refer to the reference document.

Look at the structure of the Observer:


How to collaborate:


If, in the implementation, you need to encapsulate complex update semantics, you can use Mediator (5.5) mode, Changemanager is an instance of the Mediator (5.5) pattern.


Code implementation, case for the case of the clock spoken in the book.

Subject.java:
Package com.rick.designpattern.observer;

Import java.util.ArrayList;
Import java.util.List;

/**
 * Created by MyPC on 2017/6/19.
 *
/Public abstract class Subject {
    private list<observer> observers = new arraylist<observer> ();

    public void Attach (Observer Observer) {
        observers.add (Observer);
    }

    public void Detach (Observer Observer) {
        observers.remove (Observer);
    }

    public void Notify () {for
        (Observer os:observers) {
            os.update (this);
        }
    }
}
Clocktimer.java:
Package com.rick.designpattern.observer;
Import Javax.xml.crypto.Data;
Import Java.text.SimpleDateFormat;

Import Java.util.Date;
 /** * Created by MyPC on 2017/6/19.

    */public class Clocktimer extends Subject {private int hour, minute, second;
    Public Clocktimer () {} public int gethour () {return hour;
    } public int Getminute () {return minute;
    } public int Getsecond () {return second;
                } public void Tick () {New Thread (new Runnable () {@Override public void run () {
                    while (true) {Date date = new Date (System.currenttimemillis ());
                    hour = Date.gethours ();
                    minute = Date.getminutes ();
                    Second = Date.getseconds ();
                    ClockTimer.this.Notify ();
                    try {thread.sleep (1000);
              } catch (Interruptedexception e) {          E.printstacktrace ();
    }}}). Run (); }


}
Observer.java:
Package com.rick.designpattern.observer;

/**
 * Created by MyPC on 2017/6/19.
 *
/Public abstract class Observer {public
    abstract void update (Subject Subject);

}
Analogclock.java:
Package com.rick.designpattern.observer;


/**
 * Created by MyPC on 2017/6/19.
 */Public
class AnalogClock extends Observer implements Widget {
    private clocktimer subject;

    Public AnalogClock (Clocktimer subject) {
        this.subject = subject;
        Subject.attach (this);
    }

    public void Detachsubject () {
        Subject.detach (this);
    }

    @Override public
    Void Draw () {
        System.out.println ("AnalogClock: Current time:" + subject.gethour () + ":" + Subject.getminute () + ":" + Subject.getsecond ());
    }

    @Override public
    void Update (Subject Subject) {
        if (Subject instanceof Clocktimer) {
            draw ();
        }
    }
}
Digitalclock.java:
Package com.rick.designpattern.observer;


/**
 * Created by MyPC on 2017/6/19.
 */Public
class DigitalClock extends Observer implements Widget {
    private clocktimer subject;

    Public DigitalClock (Clocktimer subject) {
        this.subject = subject;
        Subject.attach (this);
    }

    public void Detachsubject () {
        Subject.detach (this);
    }

    @Override public
    Void Draw () {
        System.out.println ("DigitalClock: Current time:" + subject.gethour () + ":" + Subject.getminute () + ":" + Subject.getsecond ());
    }

    @Override public
    void Update (Subject Subject) {
        if (Subject instanceof Clocktimer) {
            draw ();
        }
    }
}
Widget.java:
Package com.rick.designpattern.observer;

/**
 * Created by MyPC on 2017/6/19.
 *
/public interface Widgets {public


    void Draw ();

}
Client.java:
Package com.rick.designpattern.observer;

/**
 * Created by MyPC on 2017/6/19.
 */Public
class Client {public


    static void Main (string[] args) {
        Clocktimer clocktimer = new Clocktimer (); 
  digitalclock DigitalClock = new DigitalClock (clocktimer);
        AnalogClock AnalogClock = new AnalogClock (clocktimer);
        Clocktimer.tick ();
    }
}


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.