|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example | Line # 37 | 24 | 0% | 3 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (1) | |||
| Result | |||
|
1.0
|
org.perfectjpattern.core.behavioral.delegate.TestExample.testCommand
org.perfectjpattern.core.behavioral.delegate.TestExample.testCommand
|
1 PASS | |
| 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.delegate; | |
| 22 | ||
| 23 | import java.util.*; | |
| 24 | ||
| 25 | import org.perfectjpattern.core.api.extras.delegate.*; | |
| 26 | import org.perfectjpattern.core.extras.delegate.*; | |
| 27 | import org.slf4j.*; | |
| 28 | ||
| 29 | /** | |
| 30 | * Startup Main for the Delegate Pattern Example code | |
| 31 | * | |
| 32 | * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> | |
| 33 | * @version $Revision: 1.0 $Date: Apr 5, 2008 10:11:17 PM $ | |
| 34 | */ | |
| 35 | //CHECKSTYLE:OFF | |
| 36 | public final | |
| 37 | class Example | |
| 38 | // CHECKSTYLE:ON | |
| 39 | { | |
| 40 | //------------------------------------------------------------------------ | |
| 41 | // public | |
| 42 | //------------------------------------------------------------------------ | |
| 43 | 1 |
public static void |
| 44 | main(String[] anArguments) | |
| 45 | { | |
| 46 | //--------------------------------------------------------------- | |
| 47 | // Arbitrary types that have a size awareness but do not | |
| 48 | // share a common interface, therefore it could not be possible | |
| 49 | // writing a reusable code to get their sizes in a single way. | |
| 50 | // In this case without using Delegates, different adapter | |
| 51 | // implementations would be required: | |
| 52 | // | |
| 53 | // * String.length() is a function | |
| 54 | // * StringBuilder.length() is another function | |
| 55 | // * List.size() is another function | |
| 56 | //--------------------------------------------------------------- | |
| 57 | 1 | String myString = "Remember String instances are immutable ..."; |
| 58 | 1 | theLogger.debug(myString); |
| 59 | 1 | theLogger.debug("String length is: '" + myString.length() + "'"); |
| 60 | ||
| 61 | 1 | StringBuilder myBuilder = new StringBuilder(); |
| 62 | 1 | myBuilder.append("StringBuilder on the other hand"); |
| 63 | 1 | myBuilder.append("are not immutable"); |
| 64 | 1 | theLogger.debug(myBuilder.toString()); |
| 65 | 1 | theLogger.debug("Builder length is: '" + myBuilder.length() + "'"); |
| 66 | ||
| 67 | 1 | List<Integer> myList = new ArrayList<Integer>(); |
| 68 | 1 | myList.add(Integer.valueOf(0)); |
| 69 | 1 | myList.add(Integer.valueOf(1)); |
| 70 | 1 | myList.add(Integer.valueOf(2)); |
| 71 | 1 | myList.add(Integer.valueOf(3)); |
| 72 | 1 | myList.add(Integer.valueOf(4)); |
| 73 | 1 | theLogger.debug(Arrays.deepToString(myList.toArray())); |
| 74 | 1 | theLogger.debug("List size is: '" + myList.size() + "'"); |
| 75 | ||
| 76 | //--------------------------------------------------------------- | |
| 77 | // Create a delegate that unifies the interface view of all different | |
| 78 | // non-matching types and method signatures | |
| 79 | //--------------------------------------------------------------- | |
| 80 | 1 | IDelegator<ISizeAware> myDelegator = new Delegator<ISizeAware>( |
| 81 | ISizeAware.class); | |
| 82 | ||
| 83 | //--------------------------------------------------------------- | |
| 84 | // Create the delegates i.e. "function pointers" | |
| 85 | //--------------------------------------------------------------- | |
| 86 | 1 | List<ISizeAware> mySizeAwares = new ArrayList<ISizeAware>(); |
| 87 | 1 | mySizeAwares.add(myDelegator.build(myString, "length")); |
| 88 | 1 | mySizeAwares.add(myDelegator.build(myBuilder, "length")); |
| 89 | 1 | mySizeAwares.add(myDelegator.build(myList, "size")); |
| 90 | ||
| 91 | //--------------------------------------------------------------- | |
| 92 | // Use the delegates | |
| 93 | //--------------------------------------------------------------- | |
| 94 | 1 | for (ISizeAware mySizeAware : mySizeAwares) |
| 95 | { | |
| 96 | 3 | theLogger.debug(String.valueOf(mySizeAware.getSize())); |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 100 | //------------------------------------------------------------------------ | |
| 101 | // protected | |
| 102 | //------------------------------------------------------------------------ | |
| 103 | 1 |
protected static void |
| 104 | setLogger(Logger aLogger) | |
| 105 | { | |
| 106 | 1 | theLogger = aLogger; |
| 107 | } | |
| 108 | ||
| 109 | //------------------------------------------------------------------------ | |
| 110 | // members | |
| 111 | //------------------------------------------------------------------------ | |
| 112 | /** | |
| 113 | * Provides logging facilities for this class | |
| 114 | */ | |
| 115 | private static Logger theLogger = LoggerFactory.getLogger(Example.class); | |
| 116 | } | |
|
||||||||||||