1

For my android/java application I came up with the following architecture:

Instead of storing objects as attributes in the classes where they belong to (e.g. view and model stored in the controller), each of my classes only stores a reference to an object named "appgeneral". The appgeneral object then contains all the objects as attributes for the whole application (e.g. view, model, controller, logging, validator etc.), including all data and some general methods such as error reporting. The advantage: When I create new objects, I just need to pass one object reference and store it and then I am able to access all the data and methods from everywhere. No more pain that some operations are not reachable just for design reasons of the application.

On the one hand, I think it is a good idea, but on the other hand, as I am quite new to software development, I am sceptic because I have nobody seen doing it that way.

But are there any serious disadvantages? As I said, I am wondering why not more people do it that way.

Thank you.

Daniel S.
  • 5,811
  • 4
  • 28
  • 69
crazy
  • 57
  • 11
  • Have you read: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Morrison Chang Nov 10 '14 at 15:52
  • I'm sorry to say that you are completely confused by "class" and "object". An object is an instance of a class. You should read some basic OOP tutorials then try again. `Instead of saving objects as attributes in the classes where they belong to (e.g. view and model saved in the controller), each of my classes only saves a reference to an object named "appgeneral".` makes no sense. – Simon Nov 10 '14 at 15:53
  • Because objects should encapsulate their data instead of spreading it to everything in the world. Doing so makes programs much harder to reason about. Your own terminology "in the class where they belong to" is a dead giveaway. – Dave Newton Nov 10 '14 at 15:53
  • Because a good OO architecture tries to decouple classes where possible i.e. reduce dependencies between objects. There is a way of relieving your application code of having to find and initialise object references manually, and that pattern is called 'Inversion of control' IoC or 'Dependency Injection'. – Andrew Fielden Nov 10 '14 at 16:39
  • @AndrewFielden Note, however, that this is a version of IoC, since how this object is initialized may take place in a variety of ways. – Dave Newton Nov 10 '14 at 16:42

1 Answers1

2

For a very small application, your design makes sense, but the larger a program gets, the more complex the code will become, hence a very well designed structure is required to maintain an overview of what does what.

In a bigger project, collecting many references to other objects in one object, where many of the different objects are unrelated, you will quickly loose the overview of what reference does what and is needed where. If you change anything in your code, it will be unclear, even to you as the author, if you need to remove certain references or not.

Moreover, you might create memory issues: you have to keep track of when to release/set null your references in the reference-collecting-object. If you fail to do so, you create a memory leak.

Daniel S.
  • 5,811
  • 4
  • 28
  • 69