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.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      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          String myString = "Remember String instances are immutable ...";
58          theLogger.debug(myString);
59          theLogger.debug("String length is: '" + myString.length() + "'");
60          
61          StringBuilder myBuilder = new StringBuilder();
62          myBuilder.append("StringBuilder on the other hand");
63          myBuilder.append("are not immutable");
64          theLogger.debug(myBuilder.toString());
65          theLogger.debug("Builder length is: '" + myBuilder.length() + "'");
66          
67          List<Integer> myList = new ArrayList<Integer>();
68          myList.add(Integer.valueOf(0));
69          myList.add(Integer.valueOf(1));
70          myList.add(Integer.valueOf(2));
71          myList.add(Integer.valueOf(3));
72          myList.add(Integer.valueOf(4));
73          theLogger.debug(Arrays.deepToString(myList.toArray()));
74          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          IDelegator<ISizeAware> myDelegator = new Delegator<ISizeAware>(
81              ISizeAware.class);
82          
83          //---------------------------------------------------------------
84          // Create the delegates i.e. "function pointers"
85          //---------------------------------------------------------------
86          List<ISizeAware> mySizeAwares = new ArrayList<ISizeAware>();
87          mySizeAwares.add(myDelegator.build(myString, "length"));
88          mySizeAwares.add(myDelegator.build(myBuilder, "length"));
89          mySizeAwares.add(myDelegator.build(myList, "size"));
90          
91          //---------------------------------------------------------------
92          // Use the delegates
93          //---------------------------------------------------------------
94          for (ISizeAware mySizeAware : mySizeAwares)
95          {
96              theLogger.debug(String.valueOf(mySizeAware.getSize()));
97          }
98      }
99      
100     //------------------------------------------------------------------------
101     // protected
102     //------------------------------------------------------------------------
103     protected static void
104     setLogger(Logger aLogger)
105     {
106         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 }