Access to variables and methods of a module in a multi-module program

java modularity

39 观看


10 作者的声誉

I have a modular program I am writing that aims to have modules that are fully functional as standalone units in conjunction with the core program.

This entails the program not breaking at all when a new module is "plugged in" or removed, and requiring no change to any other code for the program to compile and execute with or without some module.

My question is, should I have public getters and setters for all variables in all modules, and make all methods public, so that if a developer needs to access or change a variable (for example) in some module while developing their own module, they can do so without having to change the access modifiers, or should I reconsider my design?

Thank you in advance!

作者: nanoandrew4 的来源 发布者: 2017 年 12 月 27 日

回应 1


8194 作者的声誉


I will try to answer based on my understanding of your question as long as I need to help you. if you still need to ask anything, please leave a comment otherwise don't forget to rate my answer.

Access modifiers (public, private, etc) were built to achieve abstraction and encapsulations within your classes, both of them are key concepts of object oriented programming. so you have to analyze your requirements what you should reveal or disclose to the outside world especially if you want to ship your code and be used in different systems and you don't need other developers to change some properties which are used internally only for example something like database connection string.

  • Modular where you define your public functionalities through interfaces and implement those interfaces.

  • Also, one principle it came to my mind where you should favor composition over inheritance to to avoid lots of code changes and breaks and dependencies.

  • Dependency Injection frameworks helps you to inject your modules, for example Spring framework came with dependency injection.

  • All of these principles will achieve Open-close and Liscov substitution principles that target to make it easy and open to scale and extend your code easily and still keep your system close for modification.

Now it should come to design patterns where you should architect your components together, still it is based in your requirements where you can use Factory Method for example in case you have one factory building multiple types of objects. Strategy where you need to change the behavior at runtime based on user activaties, ..etc.

the answer even should be more longer than this however I tried to gave a glimpse where you should search and continue from here.

作者: Muhammad Soliman 发布者: 2017 年 12 月 27 日