Observer - bus articles
Speaking of the bus, I want everyone to be unfamiliar, friends who are sitting on the bus may know that there is a ticket staff (busconductor) on the general bus, especially the noble ticket cart. Busconductor also plays a monitoring role in addition to the passenger (Passenger).
Let's take a look at how this process is implemented?
1. Let's first define the Busconductor interface class:
Public interface busconductor {
Public void getcurrentPassenger (Passenger Passenger); // Get the current passenger situation
}
2. Refine the specific implementation of the Busconductor interface:
Public class concretebusconductor imports busconductor {
Private Vector Vectorbus; // Bus VECTORBUS
Private passenger passenger;
Public Concretebusconductor (Passenger Passenger) {
this.Passenger = passsenger;
}
Public void getCurrentPassenger (Passenger Passenger) {
Vectorbus = passenger.getCurrentPassenger (); // Get the current passenger situation
For (int i = 0; i System.out.println ("There is:" (string) vectorbus.get (i)); } } Public void FindPassengerChange (String Action, String Str) {// Bus passenger change Passenger.SetCurrentPassenger (Action, STR); } Public void observeresult () {// observed Passenger.ShowPassengerInfo (); } } 3, define the passenger (Passenger) interface class: Public interface passenger { Public Abstract Void Attach (busconductor busconductor); // Associate passengers and ticket sellers Public Abstract void showPassengerInfo (); // Pass passenger situation Public Abstract Vector getCurrentPassenger (); // Get the current passenger situation Public Abstract Void SetCurrentPassenger (String Act, String Str); // Sets the current passenger situation } 4. Define the specific implementation of the passenger (Passenger) interface: Public Class ConcretePassenger IMPLEments Passenger { Private list Observerlist; // Observer list Private Vector Vectorbus; // Bus VECTORBUS Public contRetePassenger () { ObserverList (); Vectorbus = new vector (); } Public void attach (busconductor busconductor) {ObserverList.Add (busconductor); // Added bus ticket seller to the observer list } Public void showpassengerinfo () { For (int i = 0; i (Busconductor) ObserverList.get (i)). GetCurrentPassenger (this); // Enables bus ticket sellers to get current passengers } } Public void setCurrentPassenger (String Act, String Str) { IF (Act.Equals ("UP")) {// passenger bus Vectorbus.add (str); // Add the passenger into the bus VECTORBUS } else if (act.equals ("down")) {// passenger get off Vectorbus.remove (STR); // Remove the passenger from the bus VECTORBUS } } Public Vector getCurrentPassenger () {// Get the current passenger situation Return Vectorbus; } } 5, write test classes: Public class test { Public static void main (String [] args) { Passenger Passenger = New ConcretePassenger (); Concretebusconductor Busconductor = New Concretebusconductor (Passenger); Passenger.attach (busconductor); // Contact the bus ticket staff and passengers // The situation observed by the bus ticket seller System.out.println ("" The situation observed by the bus ticket staff: "); Passenger.SetCurrentPassenger ("UP", "Passenger Zhang San"); // Come on passengers Zhang San Passenger.SetCurrentPassenger ("UP", "Passenger Lee Si"); // Boarding Passenger Lee Busconductor.obServeResult (); // The situation observed by the bus ticket seller System.out.println ("" The situation observed by the bus ticket staff: "); Busconductor.FindPassengerchange ("Down", "Passenger Lee Si"); / / Passenger Lee Busconductor.FindPassengerchange ("UP", "Passenger Wang 5"); // Board of passengers Wang Wu Busconductor.obServeResult (); } } 5. Description: A: Definition: Reflecting the dependencies between objects, when the status of an object changes, all objects that depend on its object are notified and automatically updated. B: In this example, the bus ticketing is an observer. When the passenger changes, the bus ticket sellers can get this information in time. C: Observer is used in a timely display of information, such as for stock, taxation, etc.