View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: Model.java,v 1.3 2005/08/26 18:10:59 mat007 Exp $
29   */
30  
31  package jtge.sample.toss;
32  
33  import jtge.engine.command.ICommand;
34  import jtge.engine.data.Context;
35  import jtge.engine.data.IContext;
36  import jtge.engine.handler.Executor;
37  import jtge.engine.handler.IConsumer;
38  
39  /***
40   * This class encapsulates the model within a facade.
41   * <p>
42   * The model implements the backend of the game logics : to execute toss orders and to publish win or loss results to a
43   * registered view.
44   *
45   * @author Mathieu Champlon
46   * @version $Revision: 1.3 $ $Date: 2005/08/26 18:10:59 $
47   */
48  public class Model implements IConsumer
49  {
50      /***
51       * The consumer handling the commands.
52       */
53      private IConsumer consumer;
54      /***
55       * The moderator context.
56       */
57      private IContext context;
58  
59      /***
60       * Create a model.
61       *
62       * @param consumer the consumer handling results
63       */
64      public Model( final IConsumer consumer )
65      {
66          // create a coin which is the only game object
67          final Coin coin = new Coin();
68          // create a context and put the coin in it
69          this.context = new Context();
70          this.context.put( Coin.class, coin );
71          // also add the consumer to the context
72          this.context.put( IConsumer.class, consumer );
73          // prepare the execution of orders
74          this.consumer = new Executor( context );
75      }
76  
77      /***
78       * {@inheritDoc}
79       */
80      public final void handle( final ICommand command )
81      {
82          // forward the orders to the executor
83          consumer.handle( command );
84      }
85  }