I am developing a puzzle game in java with swing. Purpose of the game to find the all the correct pairs on the board without ur lifes reaching 0 , preety simple etc.

  1. I have a class My_Button which extends JButton.
  2. My game consists of 6 type of Tiles (square , circle etc)
  3. All these type of tiles extend My_Button class and implements my_Actions interf.
  4. my_actions interface has some methods, not crucial for the question.
  5. I have my_board class which holds arraylist with buttons , initializes buttons, fills the array , checks for validity between pairs , delete pairs etc. It contains all the game functions in a way.

Now my main problem is the actionListener implementation and logic. In my ActionListener in simple words i add the button that got clicked to a temp array and when 2 buttons got added to this array i do certain actions. for example: (pseudo)


!check_game_state() runs if's and updates all the info of the game like: tries,score,check_valid_pair,delete etc.!

Now my thinking and my question laid out:

First case: If i make every Tile class implement actionListener like the example above ill have to make my_board singleton to be able to have acess to all this variables outside of the my_board class.

Second case: even if i implement actionListener inside the my_board class and add the actionlistener through anonymous classes to my button after i add them to the board it gives error ofc because the scope gets weird.

Third case: full static class and variables, its really bad programming so id like to avoid.


--How i can reconstruct my project to manage these problem without singletons/static ?

--How i can avoid putting the check_game_state() inside the ActionListener which also is a bad practice?

I checked infinite number of qestions on this forum , google , stackoverflow but nothing really helps on the specifics. I have developed simmilar games before but i always follow the singleton tecqniques and id like to change that.

  • \$\begingroup\$ Is the ActionListener receiving communication from the buttons via direct reference eg: button1.actionListenerReference.addPair(this)? And you are trying to decrease this tight coupling without using singletons or statics? If I am understanding correctly I think the architectural approach I would recommend is using a pub/sub event driven system. You will be able to communicate via events between the button and action listener without tight coupling. Let me know if I'm understanding correctly or if I have made some misinterpretations/wrong assumptions. \$\endgroup\$ Commented Jan 9, 2020 at 14:40

1 Answer 1


i would suggest that you do some work on your design - it is a very good idea to rethink your design to make better (less bugs) 🐜 , better to test & maintain and even adding new features.

refactoring into dynamic context

You can move your whole static code into a dynamic context once you start your static main method

public class Game {

    //static context
    public static void main(String[] args){
        Game myGame = new Game();

    //dynamic context
    public void executeDynamicMethod(){


planning tools

furthermore make a plan what entities should be in your game and what relations they have. Best practice for these plans is usually to use UML diagrams

enter image description here

Use this (or a better suitable) Model to create your classes.

Design hints

Lastly you should try to seperate concerns. Most likely you have a game entity, a so called game model and a GUI to display that game. keep these two things separate as far as you can!


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .