简体   繁体   中英

OOP Design: getter method (or object) visibility

I'm currently writing an Android Game Engine library. The base class (which gets extended) contains two WSLog objects (a wrapper of the Android Log class methods). The one is called gameEngineLog (which should only be visible in the Game Engine project) and the other is called gameLog (which should be visible in the Game Engine project and the actual Game project).

In the base class you can get both logs with getter methods.

I've read through the Controlling Access to Members of a Class to get an idea of which modifiers should be infront of the getGameEngineLog() to make it only visible in the Game Engine Project, yet none of the modifiers (public, protected, default or private) suit my needs.

How can I make the gameEngineLog object only visible throughout the Game Engine Project yet not the actual Game Project?

Note: All other components (classes) in the Game Engine Project hold a reference the base class (and some are in different Packages). All I want is that those classes have a reference to the gameEngineLog object. Any approach is welcome.

Here is a visual representation:

在此处输入图片说明

Thanks in advance.

As @JB Nizet says, it seems that there should be a different base class for game engine classes and game classes. These classes could still share a common a base class (which would have getGameLog ). At a minimum, there could be just a separate subclass for game engine classes.

If only subclasses of this base class need to access getGameEngineLog , then it could be introduced as a protected member in the subclass for Game Engine classes.

If this is intended to be callable from other classes (but only those in the game engine), and package-private won't do, one approach is that any class which can use this log is given an object that provides access to it from a game engine object.

For example, if access to the log is controlled from a central point, and it has to be impossible for the a badly-implemented game to get access to it (even by a hack), then your project could create a singleton through which all logging is done, and it could create (directly or indirectly) any other objects that can access the log, and give them an object that has the logging functionality.

One way to implement the object that provides access to the log would be for it to be an instance of an inner class of the game engine class with a private constructor (so that only the game engine can create it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM