Advice on the proper use of encapsulation in Java

I am creating a small puzzle game as a hobby project, but now the project has reached the point that there is quite a lot of code (about 1,500 lines). Although I tried to prevent this, the code became dirty. I definitely want to clear the code and make it more convenient and understandable while I still can.

There are 3 classes in my game that handle puzzle pieces:

class PieceController{
    private arrayOfPieces;
    private selectedPiece;
    //actions like select a piece, dropa piece
}

class Piece{
    private pieceID
    private ArrayOfPieceStates
    //handles the initial creation of pieceStates and returns the current state
}

class PieceState{
    private stateDimensions
    // the particular rotation of a piece, aware of it dimensions.
}

Probably, this structure should be redesigned as a whole, but suppose that it is now approx.

Problem: There is also a JPanel that takes care of the graphics, and he needs to know about the PieceState of the current puzzle piece to draw it. the panel drawing method then gets the dimensions with the type request:

PuzzleController.getPiece().getState().getDimensions() 

, , . , break;

, " " :

PieceController.drawPiece(drawingInfo) // called by the graphics Panel
Piece.drawPiece(drawingInfo) // called by PieceController
PieceState.drawState(drawingInfo) // called by Piece, now here we are aware of piece dimensions so drawing can take place.

( , drawingInfo acutal code - , .)

, drawPiece - , , . , .

: puzzlePiece . : puzzlePiece state ( , , ).

, PieceState (, ), "".

, , , , . : ? , ?

+3
2

a PieceDrawer, , Piece. :

  • : , , . PieceContoller/PuzzleController. , PieceState Piece ( ). PuzzleController , , .
  • + : . , . PieceDrawer GamePanel (JPanel). GamePanel PuzzleController, PieceDrawer.

- PieceDrawer Pieces. , , , , , . GamePanel PieceDrawer, , . ( PieceDrawer , , )...

+3

PieceState, , Piece. DrawPiece PieceState, , .

+1

Source: https://habr.com/ru/post/1759479/


All Articles