Results 1 to 7 of 7

Thread: Classes, and where to put code

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Question Classes, and where to put code

    Lets say I have a chess game in development, I have all the game logic worked out but not the graphics.

    ===

    Would it be better OOP practice to put the code for drawing each chess piece within the various chesspiece classes and pass a Graphics object to the class for drawing on...

    Code:
    //pseudocode
    for each piece on chessboard
    piece.DrawSelf(chessboard.Graphics)
    next piece
    Or...

    Have a single central drawing routine and pass each chess piece to it in turn?

    Code:
    //pseudocode
    for each piece on chessboard
    DrawPiece(piece)
    next piece
    ===

    If it is the latter, how would you get all the classes info into the drawing function, since much of the classes would be private or protected.

    This would be an applicable question if instead of graphics, it was file input/output or several other things.

    how would you go about it?
    Last edited by wossname; Mar 19th, 2004 at 03:13 PM.
    I don't live here any more.

  2. #2
    New Member
    Join Date
    Mar 2004
    Location
    Indianapolis, IN
    Posts
    6
    The first method is better, OOP is basically three things

    1) Encapsulation
    2) Polymorphism
    3) Inheritance

    The second option you list above in which you have a method called DrawPiece that knows how to draw each piece is a C api, the data is not encapulated because DrawPiece will have to have access to the underlying data of the piece, there is no polymorphism or inheritance needed either, just make 'Piece' a struct with all it's data public, that's tantamount what your second approach suggests.

    If you use the OOP way (first method) then you can introduce a new piece (yea, so chess is due for a change after all these years) without changing your architecture, the non OOP way would force you to go into your architecture (DrawPiece) and add another case to your 'c' api.

  3. #3
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    Investigate MVC (Model, View, Controller).

  4. #4

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Thanks for explaining, MyGeneration.
    I don't live here any more.

  5. #5
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    You guys need to investigate MVC. MyGeneration's idea is not a hot way of doing it. MVC is the pattern initially used in Java and was so successful that now M$ are using it in their ASP.NET architecture.

    Trust me Read up on this one before you go putting your display code in your business objects....

  6. #6

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    OK, I will.

    Ta.
    I don't live here any more.

  7. #7
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    May I suggest reading up on the grasp patterns as well.

    Aspecially the Expert! I have a poster of the grasp patterns on my wall... it is of great help when assigning responsibilities to classes... I use vb.net


    kind regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width