0

I am developing a MineSweeper. In that I have 3 packages. 1.frontEnd 2.backEnd 3.mineSweeperControl.

mineSweeperControl contains an class ActionSplicer which implements ActionListener.In frontEnd I have an array of JBUTTONS and an array of ActionSplicer objects, such that splicerobj[i][j] listens to button[i][j].(one to one correspondence)

backEnd contains an array of objects such that object[i][j] has the background details of button[i][j] such as MineValue,isCellEmpty,isCellFlagged etc...

Method doBackgroundAction(i,j){..} is defined in class BackEndManager. In actionPerformed of ActionSplicer, I call doBackgroundAction(i,j) so that the any change in foreground will also affect the background.

Where my problem is?

The doBackGroundAction(i,j) needs to be public since it is called in different package.

But I don't want any method to be public, since it may reduce the flexibility and then any person can change the values of attributes.

I cannot extend the class BackEndManager since I am creating an array of ActionSplicer objects in frontEnd.

Hence I need some sort of guidance about declaring doBackGroundAction(i,j). Whether it is right way to declare the methods as public in some non-avoidable situvation? Or How can I change my design to retain the method with default visibility.

BrendanMcK
  • 14,252
  • 45
  • 54
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

1 Answers1

0

In order to not expose that method as public you could have something like this:

public class BackEndManager {
    protected void doBackgroundAction(int i, int j) { ... }
}

Then whenever you need the method create a subclass of BackEndManager (since protected allows subclasses to access the method):

public class BackEndManagerSubclass extends BackEndManager {
    public void doBackgroundActionNew(int i, int j) {
        doBackgroundAction(i, j);
    }
}

This keeps the initial class intact and lets you expose a new method through the subclass that "hides" the real internals of the BackEndManager class.

Edit: I just saw this line: "I cannot extend the class BackGroundManager since I am creating an array of ActionSplicer objects in frontEnd."

If by BackGroundManager you mean BackEndManager then my idea is not useful. Can you please make this point more clear? From what I understand ActionSplicer is in the package mineSweeperControl and frontEnd is another package so what is the connection with BackEndManager?

Tudor
  • 61,523
  • 12
  • 102
  • 142