1 //---------------------------------------------------------------------- 2 // 3 // PerfectJPattern: "Design patterns are good but components are better!" 4 // Observer.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 org.apache.commons.lang.*; 24 import org.perfectjpattern.core.api.behavioral.observer.*; 25 import org.perfectjpattern.core.api.extras.delegate.*; 26 import org.perfectjpattern.core.extras.delegate.*; 27 28 29 /** 30 * Implementation of <code>IObserver</code> interface that acts as a Proxy to 31 * any plain non-Observer class type. Makes possible to have any given class 32 * type to Observe more than one type of Subject without <code>update</code> 33 * method name and signature clash. 34 * 35 * @see IObserver 36 * 37 * @param <E> Type of event data that this <code>IObserver</code> subscribes 38 * to. 39 * 40 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> 41 * @version $Revision: 1.0 $ $Date: Jul 1, 2007 5:20:12 AM $ 42 */ 43 @SuppressWarnings("unchecked") 44 public final 45 class ObserverProxy<E> 46 implements IObserver<E> 47 { 48 //------------------------------------------------------------------------ 49 // public 50 //------------------------------------------------------------------------ 51 /** 52 * Constructs <code>ObserverProxy</code> from a target instance 53 * 54 * @param anObject Target Object that will handle the notification message 55 * @param aMethodName Target method implemented in anObject that will 56 * handle notification messages 57 * @param aParameters Type of parameters passed in notification events e.g. 58 * changed state etc 59 */ 60 public 61 ObserverProxy(Object anObject, String aMethodName, Class... aParameters) 62 { 63 Validate.notNull(anObject, "'anObject' must not be null"); 64 Validate.notNull(aMethodName, "'aMethodName' must not be null"); 65 Validate.isTrue(aMethodName.length() > 0, 66 "'aMethodName' must not be empty"); 67 Validate.notNull(aParameters, "'aParameters' must not be null"); 68 69 // assumes that return type is void 70 Class myReturnType = void.class; 71 72 // create the delegate interface based on the DynamicDelegator 73 DynamicDelegator myDelegator = new DynamicDelegator(myReturnType, 74 aParameters); 75 76 theDelegate = myDelegator.build(anObject, aMethodName); 77 } 78 79 //------------------------------------------------------------------------ 80 /** 81 * Constructs <code>ObserverProxy</code> from a target static class 82 * 83 * @param aClass Target helper static class that will handle the 84 * notification message 85 * @param aMethodName Target static method implemented in aClass that will 86 * handle notification messages 87 * @param aParameters Type of parameters passed in notification events e.g. 88 * changed state etc 89 */ 90 public 91 ObserverProxy(Class aClass, String aMethodName, Class... aParameters) 92 { 93 Validate.notNull(aClass, "'aClass' must not be null"); 94 Validate.notNull(aMethodName, "'aMethodName' must not be null"); 95 Validate.isTrue(aMethodName.length() > 0, 96 "'aMethodName' must not be empty"); 97 Validate.notNull(aParameters, "'aParameters' must not be null"); 98 99 // assumes that return type is void 100 Class myReturnType = void.class; 101 102 // create the delegate interface based on the DynamicDelegator 103 DynamicDelegator myDelegator = new DynamicDelegator(myReturnType, 104 aParameters); 105 106 theDelegate = myDelegator.build(aClass, aMethodName); 107 } 108 109 //------------------------------------------------------------------------ 110 /** 111 * {@inheritDoc} 112 */ 113 public void 114 update(E anEventData) 115 { 116 theDelegate.invoke(anEventData); 117 } 118 119 //------------------------------------------------------------------------ 120 // members 121 //------------------------------------------------------------------------ 122 private final IDelegate theDelegate; 123 }