简体   繁体   中英

What OOD design pattern am I thinking of?

I am trying to build a class that is only modifiable by the GameController, but accessible to any class. What design pattern should I research to accomplish this?

*I am building this in Java, and the GameController is my Main class. So, I can not create a private instance in the GameController class. Wish it was that simple.

There is no "design pattern" for that. Instead, you simply use Java's access modifiers, by providing public getters but only package-private setters or other mutator methods (assuming the class used by GameController is in the same package).

You can create an interface hierarchy where the top level is read only and the next level adds setters. The class implements the read write interface. The controller gets an instance with the writable interface, all other classed with the read only interface.

but accessible to any class ? i think what you are trying to implement here is every class accessing and sharing same instance of Controller class. you can go with mvc pattern but if you are too late for that and need somehow to share the same instance you can go with single pattern

public class getGame{

private static GameController gameController;

public static GameController  getController(){

if(gameController==null){
gameController = new GameController();
}

return gameController;
}

}

you dont need to worry about creating instance on above class since every field is marked as static.

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