-2

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!

  • the question is not clear enough as long as I don't know what you have done exactly however I can give you some guidelines to achieve your requirements to design a modular components! – Muhammad Soliman Dec 27 '17 at 17:03
  • @msoliman I have not done anything yet, I am asking so that I do not have to then go back and delete all I have written. I tried using reflection and custom configuration files but saw I was going nowhere, and that I could likely do everything in Java. Using public getters, setters and methods would be a solution, but I'm wondering if there is a better way of going about the problem. Thanks for your comment! – nanoandrew4 Dec 27 '17 at 17:06
  • Hello and welcome to Stack Overflow. This is a good question, but not a good question for SO because it's very broad. You may get downvotes but do not be discouraged: this site is very helpful for certain kinds of questions (and hostile to other kinds). Please spend some time reading the [help pages](https://stackoverflow.com/help) and other good questions to understand what this site is for. – DavidS Dec 27 '17 at 17:37
  • FYI your question is similar to https://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen and https://stackoverflow.com/questions/14399929/should-i-use-public-or-private-variables#14399960. Note that these questions received 7 and 16 answers (which is actually evidence of a problem: broad, high-level questions tend to get long, opinionated answers, which isn't bad in itself, but usually inappropriate for SO). I recommend you read these questions and all the answers for more perspectives. – DavidS Dec 27 '17 at 17:42
  • @DavidS Thank you for the links! The only problem I see with the answers given is they focus on OO design instead of modular design. But I do see how this question is a bit subjective, which as the help pages suggest is not what SO is for. – nanoandrew4 Dec 27 '17 at 17:54

1 Answers1

0

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
  • 21,644
  • 6
  • 109
  • 75
  • I updated my question to hopefully provide more detail on my question, but just to be clear: say I have a my core module (the one that runs everything) and auxiliary modules x and y. Another developer comes along and wants to develop module z, but needs to run a method from module x and get some variables from module y. In order to facilitate this, should I just make everything publicly accessible, so that everything in modules x and y is accesible, or should I make them adjust the access modifiers in other packages as they see fit? – nanoandrew4 Dec 27 '17 at 17:33
  • Make the necessary ones as public and tweak later in the future to give more access to them. if you have multiple clients as you said, some of them should have public properties while the other client or module needs different public properties, use inheritance or composition and disclose for each one his own class, @nanoandrew4 is it clear or u r still confused – Muhammad Soliman Dec 27 '17 at 17:38
  • Your comment is clear, thank you for the quick response. – nanoandrew4 Dec 27 '17 at 17:42
  • @nanoandrew4 please don't forget to rate my answer and mark as correct If it helped you – Muhammad Soliman Dec 27 '17 at 17:45
  • I rated your answer but will wait to see if others have other answers before marking this one as correct. – nanoandrew4 Dec 27 '17 at 17:50
  • that's fine, enjoy coding – Muhammad Soliman Dec 27 '17 at 17:51