View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // Example.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.behavioral.observer;
22  
23  import java.text.*;
24  import java.util.*;
25  
26  import org.perfectjpattern.core.api.behavioral.observer.*;
27  
28  /**
29   * Startup Main for the Observer Pattern Example code
30   *
31   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
32   * @version $Revision: 1.0 $Date: Apr 5, 2008 10:11:17 PM $
33   */
34  //CHECKSTYLE:OFF
35  public final
36  class Example
37  // CHECKSTYLE:ON
38  {
39      //------------------------------------------------------------------------
40      // public
41      //------------------------------------------------------------------------
42      @SuppressWarnings("unchecked")
43      public static void 
44      main(String[] anArguments)
45      throws ParseException
46      {
47          //---------------------------------------------------------------
48          // Create Clock Timer subject
49          //---------------------------------------------------------------
50          ISubject<Date> myClockTimer = new Subject<Date>();
51          
52          //---------------------------------------------------------------
53          // Create and attach concrete observers
54          //---------------------------------------------------------------
55          IClock myAnalog =  new AnalogClock();        
56          myClockTimer.attach(myAnalog, new DigitalClock());
57          
58          //---------------------------------------------------------------
59          // AntiqueClock demonstrates the use of ObserverProxy for cases
60          // where the Observer handling source code is not accessible and 
61          // thus can not be abstract coupled to PerfectJPattern's Observer
62          // In this case we have AntiqueClock that does not implement
63          // IObserver and has a "displayTime" static method 
64          //---------------------------------------------------------------
65          IObserver<Date> myAntiqueClock = new ObserverProxy<Date>(
66              AntiqueClock.class, "displayTime", Date.class); 
67          myClockTimer.attach(myAntiqueClock);
68          
69          //---------------------------------------------------------------
70          // Notify observers but make sure the date is predictable, this is
71          // good for testability
72          //---------------------------------------------------------------
73          Date myFirstDate = DATE_FORMAT.parse("14.09.1975 08:00:00");        
74          myClockTimer.notifyObservers(myFirstDate);
75  
76          //---------------------------------------------------------------
77          // Optionally detach observers
78          //---------------------------------------------------------------
79          myClockTimer.detach(myAnalog);
80  
81          //---------------------------------------------------------------
82          // Notify observers
83          //---------------------------------------------------------------
84          Date mySecondDate = DATE_FORMAT.parse("25.05.2008 15:56:00");
85          myClockTimer.notifyObservers(mySecondDate);
86      }
87      
88      //------------------------------------------------------------------------
89      // members
90      //------------------------------------------------------------------------
91      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
92          "dd.MM.yyyy HH:mm:ss");
93  }