I can't quite see how you're doing MVC here. I see a view, and I see a
Game class which looks a bit like it might be a model? Where's the
controller?

Generally, in MVC, the view registers its interest in the state of the
model via the Observer pattern. Thus, the model has no knowledge of
the view, but simply provides an interface that the view implements
that does the view-specific stuff. Thus, the model would never need to
have any imports related to the view.

You can similarly separate out the platform-specific aspects of the
controller, delegating them to the view. The view handles mapping from
UI-specific event (like, "this button was clicked") to a generic event
which is passed to the controller. Then the only platform-specific
stuff would be any platform-specific stuff the controller might do for
a particular application, if any. (This can then be abstracted out
into a "platform" layer).

Generally, your model does not need to know about either the View or
the Controller (except implicitly via the registered observers). The
controller needs a reference to both the model and the view.

The view doesn't need a reference to the controller, if you make the
controller an observer of the view. Or the view can have a reference
to the controller. I usually end up giving the view a reference to the
controller, as it simplifies initializing different subviews.

As to your second question -- you don't ask a question. You'd like to
do it. You can do it -- and usually should do it. So do it! Are you
wondering how to do it with your own view classes? You can do that,
too, see the examples in the platform SDK, especially the custom views
demo.

On Apr 8, 8:19 am, dbray <[email protected]> wrote:
> Hello all,
> My background is in C, but I'm relatively new to Java and to Android.
> I have a question as to whether I'm implementing code in the best way:
>
> As a learning exercise in Java and in meeting the Model-View-
> Controller pattern, I wrote a simple game. It plays out on a 2D grid
> owned by the game class, and is drawn to the display by an extended
> View class. I have it working, but I'm wondering after the fact if I'm
> going about it the right way. Here's a simplified version of what I
> have:
>
> public class GameClass {
>
>         // game is played out on this 2D field
>         private final int[][] mGameGrid;
>
>         public int[][] getGameGrid() {
>                 return mGameGrid;
>         }
>
> }
>
> public class TileView extends View {
>
>         // reference to what i want the view to draw
>         private int[][] mGrid;
>
>         // accept reference to some external grid
>         public void setGrid(int[][] externalGrid) {
>                 mGrid = externalGrid;
>         }
>
>         @Override
>         protected void onDraw(Canvas canvas) {
>                 // reads mGrid contents to draw the array to the canvas
>         }
>
> }
>
> public class GameMVC extends Activity {
>
>         private TileView mGameView;
>         private GameClass mGame;
>
>         /** Called when the activity is first created. */
>         @Override
>         public void onCreate(Bundle savedInstanceState) {
>                 super.onCreate(savedInstanceState);
>
>                 setContentView(R.layout.game_layout);
>
>                 mGame = new GameClass();
>
>                 mGameView = (TileView) findViewById(R.id.GameTileView);
>                 mGameView.setGrid(mGame.getGameGrid());
>         }
>
>         @Override
>         public boolean onKeyDown(int keyCode, KeyEvent event) {
>                 // which updates the game and triggers screen redraws
>         }
>
> }
>
> Let me add, I am trying to meet these 2 goals as well:
> 1. I am hoping to keep the game class completely independent of the
> android platform, in the spirit of fully keeping the M-V-C model (i.e.
> no import Android.anything in this class)
> 2. I would like define the view as a resource (I understand this is
> more efficient) and so inflate it via
> setContentView(R.id.TileView)
>
> So, I'm wondering, are there better ways to connect the game to the
> View? And, are my goals reasonable/appropriate?
> Comments welcome, thanks.
>
> I learn a lot from reading threads here, thanks much everyone for
> contributing.
> Best

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to